Multimobile Development: Building Applications for any Smartphone
Timing out POST-ing with HttpWebRequest
Messages   Related Types
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

Reply to this message...
Vote that this is a GOOD answer...
 
Really good experience at the Apple Store
MonoDroid – looking *awesome*
 
    
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]

Reply to this message...
Vote that this is a GOOD answer...
 
First volume of Multimobile Development nearly ready to go to press
A mention on Developing for the iPhone and Android: The pros and cons
 
    
Tom Kaiser
Try closing the request stream.

"Sean McRae" <Click here to reveal e-mail address> wrote in message
news:OpThGYV5BHA.568@tkmsftngp05...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
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]

Reply to this message...
Vote that this is a GOOD answer...
 
First chapters of Multimobile Development book now available on Apress Alpha program
iPad
 
    
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
Reply to this message...
Vote that this is a GOOD answer...
 
New book project – Multimobile Development: Building Applications for any Smartphone
Dive into HTML5
 
 
System.Collections.Specialized.NameValueCollection
System.Convert
System.EventArgs
System.IO.StreamReader
System.IO.StreamWriter
System.Net.HttpWebRequest
System.Net.HttpWebResponse
System.Net.WebClient
System.Net.WebProxy
System.Net.WebRequest
System.Net.WebResponse
System.Object
System.Text.Encoding
System.Windows.Forms.MessageBox
System.Windows.Forms.MessageBoxButtons
System.Windows.Forms.MessageBoxIcon




Multimobile Development: Building Applications for any Smartphone
Ad
BootFX
Reliable and powerful .NET application framework.
iOS, Android and Windows Phone Development Training and Consultancy
Hosted by RackSRV Communications
 
Multimobile Development: Building Applications for any Smartphone
Copyright © AMX Software Ltd 2008-2010. Portions copyright © Matthew Baxter-Reynolds 2001-2010. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - 4.0.30129.1734