Hughng Posted July 30, 2003 Posted July 30, 2003 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. Quote
*Gurus* Derek Stone Posted July 31, 2003 *Gurus* Posted July 31, 2003 You'll want to use HttpWebRequest, not HttpWebResponse. Use WebRequest.Create() to initialize an instance of HttpWebRequest. Quote Posting Guidelines
Hughng Posted July 31, 2003 Author Posted July 31, 2003 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 ========================= Quote
Hughng Posted July 31, 2003 Author Posted July 31, 2003 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 Quote
*Gurus* Derek Stone Posted July 31, 2003 *Gurus* Posted July 31, 2003 You need to pass an absolute URI to the WebRequest.Create() method, not just the host's domain or IP. 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. Quote Posting Guidelines
Hughng Posted August 1, 2003 Author Posted August 1, 2003 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? Quote
*Gurus* Derek Stone Posted August 1, 2003 *Gurus* Posted August 1, 2003 HttpWebRequest allows the developer to create specific requests. WebClient is far more generic and may just work for you. Quote Posting Guidelines
Hughng Posted August 5, 2003 Author Posted August 5, 2003 Thank you very much. Webclient work fine for me in this case. Thank you very much for all of your help Derek Stone. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.