S
samtai
I need to do a synchronous POST to a web server with a
string variable and receive back the response into a
second string variable. It seems that with .NET I find
myself needing to use lots of specialized classes to
accomplish the same thing in VB6. It's getting to the
point where it looks rather kludgy and may be difficult to
maintain in the future. So then, is there a better, more
efficient way to do this?
Here's the VB6 code:
Dim httpPost As New MSXML2.XMLHTTP
With httpPost
.open("POST", "www.someURL.com", False)
.send(strOutgoing)
strIncoming = .responseText
End With
Here's the .NET code:
'is there a better way to read a string into a byte array
than this?
Dim PostEncoding As New System.Text.ASCIIEncoding
Dim PostByteArray As Byte() = PostEncoding.GetBytes
(strOutgoing)
Dim httpPost As HttpWebRequest = CType(WebRequest.Create
("www.someURL.com"), HttpWebRequest)
With httpPost
.Method = "POST"
.ContentLength = strOutgoing.Length
Dim PostStream As System.IO.Stream = .GetRequestStream
()
PostStream.Write(PostByteArray, 0,
PostByteArray.Length)
Dim PostResponse As WebResponse = .GetResponse()
Dim PostResponseStream As System.IO.Stream =
PostResponse.GetResponseStream()
Dim PostStreamReader As New System.IO.StreamReader
(PostResponseStream)
strIncoming = PostStreamReader.ReadToEnd()
End With
string variable and receive back the response into a
second string variable. It seems that with .NET I find
myself needing to use lots of specialized classes to
accomplish the same thing in VB6. It's getting to the
point where it looks rather kludgy and may be difficult to
maintain in the future. So then, is there a better, more
efficient way to do this?
Here's the VB6 code:
Dim httpPost As New MSXML2.XMLHTTP
With httpPost
.open("POST", "www.someURL.com", False)
.send(strOutgoing)
strIncoming = .responseText
End With
Here's the .NET code:
'is there a better way to read a string into a byte array
than this?
Dim PostEncoding As New System.Text.ASCIIEncoding
Dim PostByteArray As Byte() = PostEncoding.GetBytes
(strOutgoing)
Dim httpPost As HttpWebRequest = CType(WebRequest.Create
("www.someURL.com"), HttpWebRequest)
With httpPost
.Method = "POST"
.ContentLength = strOutgoing.Length
Dim PostStream As System.IO.Stream = .GetRequestStream
()
PostStream.Write(PostByteArray, 0,
PostByteArray.Length)
Dim PostResponse As WebResponse = .GetResponse()
Dim PostResponseStream As System.IO.Stream =
PostResponse.GetResponseStream()
Dim PostStreamReader As New System.IO.StreamReader
(PostResponseStream)
strIncoming = PostStreamReader.ReadToEnd()
End With