httputility.urlencode(doc.outerxml) as a http xml post

C

CindyH

Hi
I'm not sure whether I should send this as a new message or use the one I've
been using but...
I'm using vb.net 2.0 -
My problem is I need to send
something like this: 'dim encodedstring = "test=" +
httputility.urlencode(doc.outerxml)' as a http xml post and then need to
receive it on other end for a test.
It needs to be sent as a stream.
I need some sample code on how to post it and then receive the request on
other end so I get the information from the xml.
Been playing around with all kinds of code and not getting anywhere.
Thanks,
Cindy
 
M

Michel Posseth [MCP]

Maybe you could explain the task a bit more ?

You want to send XML data with a post command , this you can not do without
encoding the XML data
this task is simple

But then you brought up streaming and i got confused , on HTTP everything is
text this doens`t mean you cannot send binary`s cause you can with encoding
and decoding ( BASE 64 or ROT 13 or even a custom protocol as long as the
outcome is a text format that doesn`t interfere with HTML tags )


hth

Michel
 
M

Michel Posseth [MCP]

Okay

well i hope i understand this right ,,, but here is my implementation

Client code ( VB executable )

Imports System.Net
Imports System.Web
Imports System.Xml
Imports System
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim url As String = "http://localhost/Default.aspx"
Dim objDoc As New System.Xml.XmlDocument
objDoc.Load("c:\sample.xml")
MsgBox(SendAndReceive(url, objDoc.OuterXml))
End Sub

Public Function SendAndReceive(ByVal Url As String, ByVal vxml As
String) As String
Dim ResultHTML As String = String.Empty
Try
Dim myrequest As System.Net.WebRequest =
System.Net.WebRequest.Create(Url)
myrequest.Method = "POST"
myrequest.ContentType = "text/xml"

Dim byteArray As Byte() = System.Text.Encoding.UTF8.GetBytes(vxml)
myrequest.ContentLength = byteArray.Length
Dim dataStream As System.IO.Stream = myrequest.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()

Dim myresponse As System.Net.WebResponse = myrequest.GetResponse()
Dim Reader As New IO.StreamReader(myresponse.GetResponseStream)
ResultHTML = Reader.ReadToEnd()
Catch ex As Exception

End Try
Return ResultHTML
End Function



End Class


Server code ( default.aspx )

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Response.Clear()
Dim Reader As New IO.StreamReader(Request.InputStream)
Dim result As String = Reader.ReadToEnd()
Response.Write(result)
End Sub

End Class



i hope above makes sence :)

ofcourse in my example i just send the xml file and display the response of
the default.aspx just to see if the xml had arrived


but i guess this is what you wanted ??

HTH

Michel Posseth [MCP]
 
C

Cindy H

Yea, your code is almost exactly like how I orginally had mine when I was
first asked to do this, but then the client added this "key name" deal to
the project and said he wanted it sent as a single key name. I didn't know
what he was talking about then someone mentioned that he probably was
refering to this:
Dim encodedstring = "test=" + httputility.urlencode(doc.outerxml)'.
I asked him if this was correct and he said yes, so trying to come up with
the code to send and read the request on other end..


Michel Posseth said:
Okay

well i hope i understand this right ,,, but here is my implementation

Client code ( VB executable )

Imports System.Net
Imports System.Web
Imports System.Xml
Imports System
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim url As String = "http://localhost/Default.aspx"
Dim objDoc As New System.Xml.XmlDocument
objDoc.Load("c:\sample.xml")
MsgBox(SendAndReceive(url, objDoc.OuterXml))
End Sub

Public Function SendAndReceive(ByVal Url As String, ByVal vxml As
String) As String
Dim ResultHTML As String = String.Empty
Try
Dim myrequest As System.Net.WebRequest =
System.Net.WebRequest.Create(Url)
myrequest.Method = "POST"
myrequest.ContentType = "text/xml"

Dim byteArray As Byte() =
System.Text.Encoding.UTF8.GetBytes(vxml)
myrequest.ContentLength = byteArray.Length
Dim dataStream As System.IO.Stream =
myrequest.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()

Dim myresponse As System.Net.WebResponse =
myrequest.GetResponse()
Dim Reader As New IO.StreamReader(myresponse.GetResponseStream)
ResultHTML = Reader.ReadToEnd()
Catch ex As Exception

End Try
Return ResultHTML
End Function



End Class


Server code ( default.aspx )

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Response.Clear()
Dim Reader As New IO.StreamReader(Request.InputStream)
Dim result As String = Reader.ReadToEnd()
Response.Write(result)
End Sub

End Class



i hope above makes sence :)

ofcourse in my example i just send the xml file and display the response
of
the default.aspx just to see if the xml had arrived


but i guess this is what you wanted ??

HTH

Michel Posseth [MCP]








CindyH said:
well the deal is that I need the post sent in key word - like
test=doc.outerxml
 
M

Michel Posseth

Cindy H schreef:
Yea, your code is almost exactly like how I orginally had mine when I was
first asked to do this, but then the client added this "key name" deal to
the project and said he wanted it sent as a single key name. I didn't know
what he was talking about then someone mentioned that he probably was
refering to this:
Dim encodedstring = "test=" + httputility.urlencode(doc.outerxml)'.
I asked him if this was correct and he said yes, so trying to come up with
the code to send and read the request on other end..


Michel Posseth said:
Okay

well i hope i understand this right ,,, but here is my implementation

Client code ( VB executable )

Imports System.Net
Imports System.Web
Imports System.Xml
Imports System
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim url As String = "http://localhost/Default.aspx"
Dim objDoc As New System.Xml.XmlDocument
objDoc.Load("c:\sample.xml")
MsgBox(SendAndReceive(url, objDoc.OuterXml))
End Sub

Public Function SendAndReceive(ByVal Url As String, ByVal vxml As
String) As String
Dim ResultHTML As String = String.Empty
Try
Dim myrequest As System.Net.WebRequest =
System.Net.WebRequest.Create(Url)
myrequest.Method = "POST"
myrequest.ContentType = "text/xml"

Dim byteArray As Byte() =
System.Text.Encoding.UTF8.GetBytes(vxml)
myrequest.ContentLength = byteArray.Length
Dim dataStream As System.IO.Stream =
myrequest.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()

Dim myresponse As System.Net.WebResponse =
myrequest.GetResponse()
Dim Reader As New IO.StreamReader(myresponse.GetResponseStream)
ResultHTML = Reader.ReadToEnd()
Catch ex As Exception

End Try
Return ResultHTML
End Function



End Class


Server code ( default.aspx )

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Response.Clear()
Dim Reader As New IO.StreamReader(Request.InputStream)
Dim result As String = Reader.ReadToEnd()
Response.Write(result)
End Sub

End Class



i hope above makes sence :)

ofcourse in my example i just send the xml file and display the response
of
the default.aspx just to see if the xml had arrived


but i guess this is what you wanted ??

HTH

Michel Posseth [MCP]








CindyH said:
well the deal is that I need the post sent in key word - like
test=doc.outerxml

Maybe you could explain the task a bit more ?

You want to send XML data with a post command , this you can not do
without encoding the XML data
this task is simple

But then you brought up streaming and i got confused , on HTTP
everything
is text this doens`t mean you cannot send binary`s cause you can with
encoding and decoding ( BASE 64 or ROT 13 or even a custom protocol
as
long as the outcome is a text format that doesn`t interfere with HTML
gs )


hth

Michel

"CindyH" <[email protected]> schreef in bericht
Hi
I'm not sure whether I should send this as a new message or use the
one
I've been using but...
I'm using vb.net 2.0 -
My problem is I need to send
something like this: 'dim encodedstring = "test=" +
httputility.urlencode(doc.outerxml)' as a http xml post and then need
to
receive it on other end for a test.
It needs to be sent as a stream.
I need some sample code on how to post it and then receive the request
on
other end so I get the information from the xml.
Been playing around with all kinds of code and not getting anywhere.
Thanks,
Cindy
okay ....

the code i showed is the most common when sending XML over the wire

however you can also send it as an encoded string ,,,, as plain XML is
AFAIK impossible when using key value pairs

a simple solution would be

exe code

Imports System.Net
Imports System.Web
Imports System.Xml
Imports System
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim url As String = "http://localhost/Default.aspx"
Dim objDoc As New System.Xml.XmlDocument
objDoc.Load("c:\sample.xml")
MsgBox(SendAndReceive(url, objDoc.OuterXml))
End Sub

Public Function SendAndReceive(ByVal Url As String, ByVal vxml As
String) As String
Dim result As String = String.Empty
Try
Dim wc As New WebClient

Dim postdata As New Specialized.NameValueCollection

postdata.Add("Test", HttpUtility.UrlEncode(vxml))
wc.UploadValues(Url, "POST", postdata)
result =
System.Text.Encoding.UTF8.GetString(wc.UploadValues(Url, "POST", postdata))
Catch ex As Exception

End Try
Return result


End Function
End Class


server code

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Response.Clear()


Response.Write(HttpUtility.UrlDecode(Request.Form("Test")))




End Sub

End Class


as you see you have now the problem that you need to decode the data on
the server side


hth

michel
 
C

CindyH

Yep, that's right - I have to decode on other side.
Big pain - I just figured out how to do this, don't know if it's the best
way or not.
The guy tested it from his box and it seems to be working.
Thanks for all your input and taking the time to send me this code!



Michel Posseth said:
Cindy H schreef:
Yea, your code is almost exactly like how I orginally had mine when I was
first asked to do this, but then the client added this "key name" deal to
the project and said he wanted it sent as a single key name. I didn't
know what he was talking about then someone mentioned that he probably
was refering to this:
Dim encodedstring = "test=" + httputility.urlencode(doc.outerxml)'.
I asked him if this was correct and he said yes, so trying to come up
with the code to send and read the request on other end..


Michel Posseth said:
Okay

well i hope i understand this right ,,, but here is my implementation

Client code ( VB executable )

Imports System.Net
Imports System.Web
Imports System.Xml
Imports System
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim url As String = "http://localhost/Default.aspx"
Dim objDoc As New System.Xml.XmlDocument
objDoc.Load("c:\sample.xml")
MsgBox(SendAndReceive(url, objDoc.OuterXml))
End Sub

Public Function SendAndReceive(ByVal Url As String, ByVal vxml As
String) As String
Dim ResultHTML As String = String.Empty
Try
Dim myrequest As System.Net.WebRequest =
System.Net.WebRequest.Create(Url)
myrequest.Method = "POST"
myrequest.ContentType = "text/xml"

Dim byteArray As Byte() =
System.Text.Encoding.UTF8.GetBytes(vxml)
myrequest.ContentLength = byteArray.Length
Dim dataStream As System.IO.Stream =
myrequest.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()

Dim myresponse As System.Net.WebResponse =
myrequest.GetResponse()
Dim Reader As New
IO.StreamReader(myresponse.GetResponseStream)
ResultHTML = Reader.ReadToEnd()
Catch ex As Exception

End Try
Return ResultHTML
End Function



End Class


Server code ( default.aspx )

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Response.Clear()
Dim Reader As New IO.StreamReader(Request.InputStream)
Dim result As String = Reader.ReadToEnd()
Response.Write(result)
End Sub

End Class



i hope above makes sence :)

ofcourse in my example i just send the xml file and display the response
of
the default.aspx just to see if the xml had arrived


but i guess this is what you wanted ??

HTH

Michel Posseth [MCP]








:

well the deal is that I need the post sent in key word - like
test=doc.outerxml

Maybe you could explain the task a bit more ?

You want to send XML data with a post command , this you can not do
without encoding the XML data
this task is simple

But then you brought up streaming and i got confused , on HTTP
everything
is text this doens`t mean you cannot send binary`s cause you can with
encoding and decoding ( BASE 64 or ROT 13 or even a custom protocol
as
long as the outcome is a text format that doesn`t interfere with HTML
gs )


hth

Michel

"CindyH" <[email protected]> schreef in bericht
Hi
I'm not sure whether I should send this as a new message or use the
one
I've been using but...
I'm using vb.net 2.0 -
My problem is I need to send
something like this: 'dim encodedstring = "test=" +
httputility.urlencode(doc.outerxml)' as a http xml post and then need
to
receive it on other end for a test.
It needs to be sent as a stream.
I need some sample code on how to post it and then receive the
request on
other end so I get the information from the xml.
Been playing around with all kinds of code and not getting anywhere.
Thanks,
Cindy
okay ....

the code i showed is the most common when sending XML over the wire

however you can also send it as an encoded string ,,,, as plain XML is
AFAIK impossible when using key value pairs

a simple solution would be

exe code

Imports System.Net
Imports System.Web
Imports System.Xml
Imports System
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim url As String = "http://localhost/Default.aspx"
Dim objDoc As New System.Xml.XmlDocument
objDoc.Load("c:\sample.xml")
MsgBox(SendAndReceive(url, objDoc.OuterXml))
End Sub

Public Function SendAndReceive(ByVal Url As String, ByVal vxml As
String) As String
Dim result As String = String.Empty
Try
Dim wc As New WebClient

Dim postdata As New Specialized.NameValueCollection

postdata.Add("Test", HttpUtility.UrlEncode(vxml))
wc.UploadValues(Url, "POST", postdata)
result =
System.Text.Encoding.UTF8.GetString(wc.UploadValues(Url, "POST",
postdata))
Catch ex As Exception

End Try
Return result


End Function
End Class


server code

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Response.Clear()


Response.Write(HttpUtility.UrlDecode(Request.Form("Test")))




End Sub

End Class


as you see you have now the problem that you need to decode the data on
the server side


hth

michel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top