Problems with HttpWebRequest

G

Guest

I have a very wired problem requesting one specific url from within my
application.

I have struggeled with this for 5 hours now, and searched google withour any
luck, so i hope that someone are able to help me.

Here is the code that i am using:

Dim url As String = "MyIP"
Dim httpRequest As HttpWebRequest = CType(WebRequest.Create(url),
HttpWebRequest)
Dim httpResponse As HttpWebResponse = CType(httpRequest.GetResponse,
HttpWebResponse)

The erro that i get is the following:

"The underlying connection was closed: An unexpected error occurred on a
receive."

Then i downloaded HTTP Analyzer to see what was going on behind the scenes.

When requesting the url from within IE, the content is returned to the
browser windows without any problems, but when the request is made using a
HttpWebRequest strange things happen.

When i monitor what i happening in HTTP Analyser is see that the request is
made from my asp.net program, and the content and a status 200 is returned,
BUT about 500 miliseconds later another request is made to the resource (i
can see that in the monitor program) and this time no content is returned.
This is probabaly why the the framework see this as a malformed response, and
i get the error above.

I have tested the code with 10-15 other addresses with success. The content
is returned, and the resoruce is only requested 1 time. Has anyone seen this
before ?

What is a little bit special about the resource that i request is that the
content is returned with no header info at all, only raw text, but why should
this mean that the resource is accessed two times, when i only call
httpRequest.GetResponse one time.

The resource that i access is a simple url hosted on a Apace server which
retuns a text string with no header info at all.

I hope that someone can help me on this, as i think i have tested for
everything.

thanks,
Oliver
 
S

Scott Allen

Does the initial request cause a redirect to a default page?
You might want to set AllowAutoRedirect to false and see what happens.
I know you said the server does not return a header, but a redirect
would cause 2 requests fromt the client.
 
G

Guest

Thank you for reading my post.

I have tried this and it still requests the url two times, and gives me the
same error.

I have even tried setting header information in my request to the same as
when the url is requested from within my browser, but nothing helps.

I was wondering if the HttpWebResponse does not like responses with no
header info at all, and then requests the resource again ? If this is so,
what are my options then ?

Thanks,
Oliver
 
G

Guest

I now see that the url returns a HTTP/1.0 200 Simple Response, where as other
urls that i have tried with the HttpWebRequest returns a HTTP/1.1 200 OK
response. Could this 'Simple Response' be the problem ?
 
G

Guest

I have tried forcing the http protocol to Version 1.0 with
httpRequest.ProtocolVersion = HttpVersion.Version10

This gives the same error, and the url is still reqested 2 times.
 
K

Kevin Spencer

I have neverheard of a "200 Simple" response, unless you meant a "simple"
200 response. In either case, it means OK. It certainly does sound offhand
like a redirect is occurring, but if you have set AutoRedirect to false,
that should not happen. One thing you can do is to log the headers and the
Response Stream (as a string) to get more detailed information about what is
going on.

I am also working on a project using HttpWebRequest and HttpWebResponse. One
of the URLs I had trouble with was looking for a cookie that would have been
set with the previous Response. I discovered that the Response.Cookies
collection is not auto-populated, and had to write my own code to get the
cookie from the previous Response, and insert it into the Request.Cookies
collection.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Big things are made up of
lots of little things.
 
G

Guest

I gave up using the HttpWebRequest class and ordered PowerTCP Sockets for
..NET from Dart, and now i works as expected.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top