Help! How to HTTP Post in Dot Net??

  • Thread starter Thread starter Ed
  • Start date Start date
E

Ed

I'm trying to port one of my applications from VB6 to Dot Net.

In my VB6 app I do an HTTP Post of data to a website and receive a
response. I want to essentially do the same thing in VB Dot Net.

This is my original VB6 source code:

WinHttpReq.Open "POST", "https://www.abcdefg.com/ProcessInquiry.asp",
False
WinHttpReq.SetRequestHeader "Content-Type",
"application/x-www-form-urlencoded"
WinHttpReq.Send sSendText
sResponseText = WinHttpReq.ResponseText

What is the equivalent in dotnet? I need to be able to send sSendText
and get a result sResponseText.

Thanks in advance.
ed
 
Hi Ed,

The .NET counterpart is the WebRequest class. Its interface differs from
what you had in VB6 but the concept is pretty much the same - specify
method, add headers and cookies and issue the request. To obtain the
response as text, you should use the HttpResponseStream class to read
response bytes which will be then converted to a string with the
System.Text.Encoding.GetString method.
 
Thanks Dmitriy,

What is the equivalent for this line? WebRequest doesn't seem to have
an interface to send data.
WinHttpReq.Send sSendText

ed

Dmitriy Lapshin said:
Hi Ed,

The .NET counterpart is the WebRequest class. Its interface differs from
what you had in VB6 but the concept is pretty much the same - specify
method, add headers and cookies and issue the request. To obtain the
response as text, you should use the HttpResponseStream class to read
response bytes which will be then converted to a string with the
System.Text.Encoding.GetString method.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Ed said:
I'm trying to port one of my applications from VB6 to Dot Net.

In my VB6 app I do an HTTP Post of data to a website and receive a
response. I want to essentially do the same thing in VB Dot Net.

This is my original VB6 source code:

WinHttpReq.Open "POST", "https://www.abcdefg.com/ProcessInquiry.asp",
False
WinHttpReq.SetRequestHeader "Content-Type",
"application/x-www-form-urlencoded"
WinHttpReq.Send sSendText
sResponseText = WinHttpReq.ResponseText

What is the equivalent in dotnet? I need to be able to send sSendText
and get a result sResponseText.

Thanks in advance.
ed
 
The request body is transferred through the GetRequestStream:

Dim formBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(sText)
request.ContentLength = formBytes.Length

Dim requestData As Stream = request.GetRequestStream()

Try
requestData.Write(formBytes, 0, CInt(request.ContentLength))
Finally
requestData.Close()
End Try

P.S. There are syntax errors possible in the example above - I'm a C# guy
after all :-)

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Ed said:
Thanks Dmitriy,

What is the equivalent for this line? WebRequest doesn't seem to have
an interface to send data.
WinHttpReq.Send sSendText

ed

"Dmitriy Lapshin [C# / .NET MVP]" <[email protected]> wrote
in message news: said:
Hi Ed,

The .NET counterpart is the WebRequest class. Its interface differs from
what you had in VB6 but the concept is pretty much the same - specify
method, add headers and cookies and issue the request. To obtain the
response as text, you should use the HttpResponseStream class to read
response bytes which will be then converted to a string with the
System.Text.Encoding.GetString method.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Ed said:
I'm trying to port one of my applications from VB6 to Dot Net.

In my VB6 app I do an HTTP Post of data to a website and receive a
response. I want to essentially do the same thing in VB Dot Net.

This is my original VB6 source code:

WinHttpReq.Open "POST", "https://www.abcdefg.com/ProcessInquiry.asp",
False
WinHttpReq.SetRequestHeader "Content-Type",
"application/x-www-form-urlencoded"
WinHttpReq.Send sSendText
sResponseText = WinHttpReq.ResponseText

What is the equivalent in dotnet? I need to be able to send sSendText
and get a result sResponseText.

Thanks in advance.
ed
 
Back
Top