This message was discovered on microsoft.public.dotnet.languages.vb.
| Sean McRae |
Hi all. I'm timing out POST-ing data via HttpWebRequest. From a packet analyzer capture it looks like the underlying framework is not responding to a "100 Continue" message from the remote server. This is my first use of the HttpWebRequest class so it's probably something simple. Here's the code
Dim strResult As String Dim strXmlRequest As String Dim myWriter As StreamWriter
strXmlRequest = "XML omitted for clarity"
Dim objRequest As HttpWebRequest = WebRequest.Create(API_URI)
Try
objRequest.Method = "POST" objRequest.ContentLength = strXmlRequest.Length objRequest.ContentType = "application/xml" objRequest.UserAgent = "agent name omitted" objRequest.Headers.Add("Authorization", _ "Basic " & Convert.ToBase64String(Encoding.ASCII.GetBytes("credentials omitted")))
myWriter = New StreamWriter(objRequest.GetRequestStream()) myWriter.Write(strXmlRequest)
Dim objResponse As HttpWebResponse = objRequest.GetResponse()
The GetResponse method never releases and a timeout exception is thrown. I've validated that the target site is available and responding to identical requests via a separate utility.
Thanks in advance...
- Sean
|
|
|
| |
|
| |
| |
| Mark Hoffman |
Sean,
Are you running this behind a proxy server? If so, you'll need to set the Proxy property to a WebProxy object. I have code that is virtually identical to yours and it works fine, but only once I add the Proxy.
Mark
"Sean McRae" <Click here to reveal e-mail address> wrote in message news:OpThGYV5BHA.568@tkmsftngp05... [Original message clipped]
|
|
|
| |
|
| |
|
| | |
| |
| Jeff Rhodes |
I just strugged with this recently as well. If you are not sending parameters, then you set the ContentLength to 0. In that case, using WebClient is easier. With parameters, I've found the UploadParameters method of WebClient to be the simplest. Here's some sample code. The first two are different implementions for a POST with no parameters. The third sets parameters.
Imports System.Net Imports System.IO
Private Sub submitPost_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles submitPost.Click If httpPostAddress.Text <> "" Then Try Dim postRequest As WebRequest Dim postResponse As WebResponse Dim responseStream As Stream Dim responseReader As StreamReader Dim responseText As String
postRequest = WebRequest.Create(httpPostAddress.Text) With postRequest .ContentLength = 0 .Method = "POST" .ContentType = "application/x-www-form-urlencoded" End With postResponse = postRequest.GetResponse() responseStream = postResponse.GetResponseStream() responseReader = New StreamReader(responseStream) responseText = responseReader.ReadToEnd postReturnValue.Text = responseText responseReader.Close() responseStream.Close() Catch ex As Exception MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, _ MessageBoxIcon.Exclamation) End Try End If End Sub
Private Sub submitPostWebClient_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles submitPostWebClient.Click
If httpPostAddress.Text <> "" Then Dim webClientID As WebClient = New WebClient() Dim responseStream As Stream = webClientID.OpenRead(httpPostAddress.Text) Dim responseReader As New StreamReader(responseStream) Dim responseText As String = responseReader.ReadToEnd
postReturnValue.Text = responseText responseStream.Close() responseReader.Close() End If End Sub
Private Sub submitPostParameters_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles submitPostParameters.Click
If emailAddress.Text <> "" Then Dim webAddress As String = urlWithParameters.Text Dim webClientID As WebClient = New WebClient() Dim parameterTable As New System.Collections.Specialized.NameValueCollection()
With parameterTable .Add("email", emailAddress.Text) .Add("name", nameBox.Text) .Add("location", locationBox.Text) End With
Dim responseArray As Byte() = webClientID.UploadValues(webAddress, "POST", _ parameterTable) postReturnValue.Text = System.Text.Encoding.ASCII.GetString(responseArray) End If End Sub
I hope this is helpful.
-- Jeff Rhodes Author of "VBTrain.NetT: Creating Computer and Web Based Training with Visual Basic® .NET" www.vbtrain.net
"Sean McRae" <Click here to reveal e-mail address> wrote in message news:OpThGYV5BHA.568@tkmsftngp05... [Original message clipped]
|
|
|
| |
|
| |
|
| |
| Christopher Robin |
Sean,
i had the same problem. You can find what you are after on the following link.
http://www.experts-exchange.com/Web/Web_Languages/XML/Q_20931704.html
In brief its the following code you need
Function PostToRSXML(ByVal url As String, ByVal strXML As String) As String Dim result As String = "" Dim myWriter As StreamWriter
Dim objRequest As HttpWebRequest = WebRequest.Create(url) objRequest.Method = "POST" objRequest.ContentLength = strXML.Length objRequest.ContentType = "application/x-www-form-urlencoded"
Try myWriter = New StreamWriter(objRequest.GetRequestStream()) myWriter.Write(strXML) Catch e As Exception Return e.Message Finally myWriter.Close() End Try
Dim objResponse As HttpWebResponse = objRequest.GetResponse() Dim sr As StreamReader sr = New StreamReader(objResponse.GetResponseStream()) result = sr.ReadToEnd() sr.Close()
Return result End Function
|
|
|
| |
|
| |
|
|
|
|
|