Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

Posted

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

=========================

Posted

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

  • *Gurus*
Posted

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.

Posted

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?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...