HttpWebRequest.GetResponse() always times out

  • Thread starter Thread starter Bob L.
  • Start date Start date
B

Bob L.

Greetings,

I have the following function which does a simple HTTP POST and reads the
results. I had this working a couple of weeks ago, but I obviously made some
change that now causes any URL to return a timeout (the
HttpWebRequest.Timeout period expires). I've tested this on 3 different
machines with the same result. Can anyone spot the one thing that I keep
missing?

Many thanks,
Bob L.


Private Function GetResponse(ByVal sMyURL As String) As String
Dim oRequest As HttpWebRequest
Dim oWriter As StreamWriter
Dim oResponse As HttpWebResponse
Dim oReader As StreamReader

oRequest = WebRequest.Create(sMyURL)
oRequest.Method = "POST"
oRequest.ContentLength = sMyURL.Length
oRequest.ContentType = "application/x-www-form-urlencoded"

oWriter = New StreamWriter(oRequest.GetRequestStream())
oWriter.Write(sMyURL)

oResponse = oRequest.GetResponse() '***** Timeout occurs here

oReader = New StreamReader(oResponse.GetResponseStream())
GetResponse = oReader.ReadToEnd

oReader.Close()
oWriter.Close()
End Function
 
paste the url into a browser address bar and see if it brings back anything.
i got a buck that says it don't.
 
Alvin,

Thanks for the guess, but that's one of the first things I tried. The
problem I'm having is with any URL, and using the browser always works fine.
I've tried multiple sites, and even pages on sites that I have developed
myself that are currently in production. The 3 machines I tested this on are
in 3 different states around the country, 3 different ISPs, etc. In each
case, Internet access works perfect, except for my attempt to access it via
code.

- Bob

Alvin Bruney said:
paste the url into a browser address bar and see if it brings back anything.
i got a buck that says it don't.

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
Bob L. said:
Greetings,

I have the following function which does a simple HTTP POST and reads the
results. I had this working a couple of weeks ago, but I obviously made
some
change that now causes any URL to return a timeout (the
HttpWebRequest.Timeout period expires). I've tested this on 3 different
machines with the same result. Can anyone spot the one thing that I keep
missing?

Many thanks,
Bob L.


Private Function GetResponse(ByVal sMyURL As String) As String
Dim oRequest As HttpWebRequest
Dim oWriter As StreamWriter
Dim oResponse As HttpWebResponse
Dim oReader As StreamReader

oRequest = WebRequest.Create(sMyURL)
oRequest.Method = "POST"
oRequest.ContentLength = sMyURL.Length
oRequest.ContentType = "application/x-www-form-urlencoded"

oWriter = New StreamWriter(oRequest.GetRequestStream())
oWriter.Write(sMyURL)

oResponse = oRequest.GetResponse() '***** Timeout occurs here

oReader = New StreamReader(oResponse.GetResponseStream())
GetResponse = oReader.ReadToEnd

oReader.Close()
oWriter.Close()
End Function
 
Bob said:
Greetings,

I have the following function which does a simple HTTP POST and reads
the results. I had this working a couple of weeks ago, but I
obviously made some change that now causes any URL to return a
timeout (the HttpWebRequest.Timeout period expires). I've tested this
on 3 different machines with the same result. Can anyone spot the one
thing that I keep missing?

Many thanks,
Bob L.


Private Function GetResponse(ByVal sMyURL As String) As String
Dim oRequest As HttpWebRequest
Dim oWriter As StreamWriter
Dim oResponse As HttpWebResponse
Dim oReader As StreamReader

oRequest = WebRequest.Create(sMyURL)
oRequest.Method = "POST"
oRequest.ContentLength = sMyURL.Length
oRequest.ContentType = "application/x-www-form-urlencoded"

oWriter = New StreamWriter(oRequest.GetRequestStream())
oWriter.Write(sMyURL)

oResponse = oRequest.GetResponse() '***** Timeout occurs
here

oReader = New StreamReader(oResponse.GetResponseStream())
GetResponse = oReader.ReadToEnd

oReader.Close()
oWriter.Close()
End Function

1. You transmit your data using UTF-8 encoding (by using the default
StreamWriter constructor), but specify the Content-Length to be the string
length. That's only true if you submit 7 bit ASCII characters. It is better
to encode the data manually using System.Text.Encoding, which will allow you
to extract the real Content-Length.

2. Why do you send the URL to the server? Is that a meaningful request?

Cheers,
 
More important - url is not application/x-www-form-urlencoded type, so the
server should get back to you with 400 Bad Request, or just timeout if you
don't send it enough data.

Jerry
 
Hello People...

I posted the same problem 2 days ago. I could not resolve the problem
yet.

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&th=88a48d08dc9f95c9&rnum=3

However, I found out some strange behaviour. If I call a plain aspx
file without any code to execute (inside test1.aspx). I had no
problems at all

oRequest = WebRequest.Create("http://www.xxx.com/test1.aspx)
oRequest.Timeout= 10000
oResponse = oRequest.GetResponse() '***** Works

as soon I place some asp code within test1.aspx (mixed with HTML Code)
then the timout error occurs. Also a 500 Error Message occured when I
did not set the timeout at all.

If I cut off the same asp code and past it into a seperate
test1.aspx.vb file, everything worked fine.

I tried it on two different servers and had the same result. Another
strange thing is that the whole thing worked for some time but not
anymore.

Unfortunatelly I do not know in detail the processing of asp.net when
asp code it mixed with HTML or if it is seperated in a *.vb file.

I am realy keen to find a solution, otherwise my boss kicks my *ss.

Thanks for helping

Michael :)
 
Thanks, feroze, that did it. I was closing the stream, but I was doing it in
the wrong location.

BTW (Joerg), this solution is to post the URL to a different server
according the the vendor's specifications for performing a transaction.
Thanks also for the encoding tips.

- Bob

Feroze said:
You need to close the request stream:

--
feroze
http://weblogs.asp.net/feroze_daud
============

Remove "user" from the email address to reply to the author.

This posting is provided "AS IS" with no warranties, and confers no rights

Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 
Back
Top