HttpWebResponse Header construction

Hughng

Active member
Joined
Jun 24, 2003
Messages
42
I am trying to create an POST method using HttpWebResponse from VB.NET.

The original code is in C++ with the following setup:
==================================
CInternetSession sess("CAutoTestComm", 1, INTERNET_OPEN_TYPE_DIRECT);

pHC=sess.GetHttpConnection(server, 0, port);

pHF=pHC->OpenRequest("POST", "/boundxml");
==================================

This code will generate the header below. How to I translate this code into VB.NET using HTTPWebresponse.

<!---------READ HEADER-------->
POST /boundxml HTTP/1.1
User-Agent: CAutoTestComm
Host: 198.xxx.yyy.zzz:8080
Content-Length: 212
Cache-Control: no-cache

Thank you very much for your help.
 
Youll want to use HttpWebRequest, not HttpWebResponse. Use WebRequest.Create() to initialize an instance of HttpWebRequest.
 
Thank you for your answer. Here what I did but it does not work. I keep having the exception "connection closed ..." right at the line create the streamwritter.

=======================
Private Function PostResult(ByVal resultmsg As String) As Boolean
Dim sw As StreamWriter
Dim sr As StreamReader
Dim myresponse As Net.HttpWebResponse
Dim myrequest As Net.HttpWebRequest = CType(Net.WebRequest.Create(m_server), Net.HttpWebRequest)

Try
myrequest.Method = "POST"
myrequest.ContentType = "/boundxml"
myrequest.ContentLength = resultmsg.Length

sw = New StreamWriter(myrequest.GetRequestStream())
sw.Write(resultmsg)
sw.Close()

myresponse = myrequest.GetResponse

If myresponse.StatusCode <> HttpStatusCode.OK Then
MsgBox("Server return: " & myresponse.StatusDescription)
sr = New StreamReader(myresponse.GetResponseStream())
m_lastStatus = sr.ReadToEnd()
Return False
End If
End If

Catch Exp As Exception
MsgBox("Exception: " & Exp.Message)
m_lastStatus = "Can not connect to server"
Return False
End Try

Return True

End Function
=========================
 
Same question:
How do I specify a target script on the http server. For example, I want to runt he script that parse my post request. It is under it is call "boundxml".
Should I put "/boundxml" into the contenttype or something where should I put it in to get the rigth response from the server?
Thank you
 
You need to pass an absolute URI to the WebRequest.Create() method, not just the hosts domain or IP.

Code:
Dim request As HttpWebRequest = WebRequest.Create("http://127.0.0.1:8080/boundxml")

Also, you should leave the ContentType property blank and set the UserAgent property.
 
Thank you very much for the info. I am just wondering another thing about the Webclient class. Instead using Httpwebrequest, can I just use the Webclient and upload method to achieve the samething?
How is it difference between Httpwebrequest and Webclient classes?
 
HttpWebRequest allows the developer to create specific requests. WebClient is far more generic and may just work for you.
 
Thank you very much. Webclient work fine for me in this case. Thank you very much for all of your help Derek Stone.
 
Back
Top