HttpWebRequest fails on some computers (weird...)
Hi all,
I got a strange problem with HttpWebRequest code, failing on some computers, but i can't reproduce it on either of my 3 different computers here. Following is the code snippet causing the error (on some machine configurations):
| |
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://someurl.tld/somefile.txt");/* --> optional block */ request.KeepAlive = false; request.ProtocolVersion = HttpVersion.Version10; request.Proxy = WebProxy.GetDefaultProxy(); request.AllowAutoRedirect = true; request.MaximumAutomaticRedirections = 10; request.Timeout = (int) new TimeSpan(0, 0, 60).TotalMilliseconds; request.UserAgent = "Mozilla/3.0 (compatible; MyBrowser/1.0)"; /* <-- optional block end */ WebResponse response = request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream());
|
I don't know where exactly the code fails, as i can't reproduce the bug (?) on my machines. It fails in both cases, with or without the middle part of code marked with comments as optional (i found this on the web as a solution to a similar problem).
Of course the URL is reachable with the browser on the failing machines.
Seems very strange to me that this only happens on some configurations, but i wasn't abled to find a common thing with the failing machines (Doesn't seem to be related to OS version or .NET version).
Would be nice if anyone around here could help me out.
You need to find out where it failing, and what exception is actually occuring.
Just saying that it is failing is not enough information.
Sorry, i forgot to include this piece:
I suppose the GetResponse() call is the exact location.
The exception thrown is reported to say:
The underlying connection was closed: Unable to connect to the remote server.
at System.Net.HttpWebRequest.CheckFinalStatus()
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.HttpWebRequest.GetResponse()
at MyNamespace.MyClass.MyFunc()
Strange to me because the connection from browser is reported to me to work on the same machine at the same time.
Any ideas?
Are these machines behind a proxy server? Internet Explorer either uses custom settings entered via Internet Options or will automatically try to detect settings. Whereas, .NET Framework, by default, will try to directly connect to the server.
It fails and works on both kind of machines (with and without proxies in use). Additionally the proxy settings of IE are taken care of in the middle piece of code (marked as optional, in one version of the code). So I think I can say that proxy settings don't affect this behaviour.
It's really a nasty and strange bug, and I already tried all the easy answers, I too would give to such a problem question.
Thanks so far, any more ideas?
WebProxy.GetDefaultProxy() to get IE proxy information only works for custom entered proxy details with Internet Explorer. If they are automatic in anyway such as Automatically detect, or Use automatic configuration script then .NET will not be able to detect the proxy settings.
Do you have to log onto the proxy? There's nothing in the code that passes any credentials to the proxy.
I know about that issue, but as stated above, the code fails even on (still only on some, not on all) computers without proxys.
I already tried to isolate the error in respect to internet connection method, proxy usage, OS version, .NET version but without any kind of pattern visible (seems to fail or work on any kind of combination of these parameters).
Could you please try enabling network tracing through configuration and then post the log for the failed request? This will allow us to see the network traffic. You can enable network tracing by adding the following to your application configuration file:
<configuration>
<system.diagnostics>
<sources>
<source name="System.Net">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
<source name="System.Net.Sockets">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
</sources>
<switches>
<add name="System.Net" value="Verbose" />
<add name="System.Net.Sockets" value="Verbose" />
</switches>
<sharedListeners>
<add name="System.Net"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="System.Net.log"
/>
</sharedListeners>
<trace autoflush="true" />
</system.diagnostics>
</configuration>
You may want to edit the file to remove any sensitive data before posting.
Daniel Roth
This was true in v1.1 of .NET but for v2.0 we have added full support for automatic proxy detection and configuration scripts. Basically, if IE can detect the proxy, than System.Net should be able to detect the proxy too. If you ever see that this is not the case in v2.0, than it is a bug and we would love to hear about it.
Daniel Roth
System.Net
hi,
i m getting the bug of which you are eager to hear about. I intend to develop an application in .netcf which will access a http but it fails to take the default settings( of IE ). and i have ensure all the steps that have been said about. infact it gives the " 407 Proxy Authentication Error ". Can u solve it ? Here's my sample code:
Dim objWebRequest As WebRequest
Dim objWebResponse As WebResponse'
Dim objWebProxy As WebProxy
objWebRequest = WebRequest.Create("http://www.google.com")
objWebRequest.Proxy = WebProxy.GetDefaultProxy
objWebResponse = objWebRequest.GetResponse ...........
the same code works if the proxy server is specified along with credentials. but i need to consider the scenario whr the user doesn't know abt the IE settings.
hoping for a response
Hi Sandeepy,
Can you please post the exact code that you are having problems authenticating to the proxy with?
Mike Flasko
I'm having a similar problem, but im running my smart device application on my laptop and there is no proxy server i need to connect to to use a POST method.
it fails on finishGetResponse() on the stack trace.
could you tell me where to problem could possibly be.
thanks
I am having a similar issue with a false positive on specific URLs. These are URLs that I can reach via IE, but when using HttpWebRequest it returns a "
The remote server returned an error: (500) Internal Server Error." and doesn't even reach the section of code that checks that statuscode. I have isolated a group of URLs that the consistenly happens with - but I cannot figure out what to change in my code in order to avoid this issue. One example URL is
http://www.host1.exxonmobil.com/psims/psims.aspx and my code is, with input being the URL string. It is all within a try/catch, and creates an exception at "res = (
HttpWebResponse)req.GetResponse();"HttpWebRequest
req;HttpWebResponse res;Uri inputUri = new Uri(input.ToString());req = (
HttpWebRequest)WebRequest.Create(inputUri.ToString());req.UserAgent =
@"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1";req.Timeout = 5000;
req.AllowAutoRedirect =
true;res = (
HttpWebResponse)req.GetResponse();if (res.StatusCode == HttpStatusCode.OK){
bValidity =
true;}
Any Assistance would be appreciate - I am not sure what the next step should be. Thank you!!
Shawn
Hi anybody!
i've got such an issue at the moment and so i ask for un update for this thread:
Here is my code snippet
WebRequest ObjReq;
ObjReq=WebRequest.Create(url);
...
In fact on some systems, where the automatic detection is selected, my webrequest produes an error.
What do i have to do, to make my request run on such systems? You wrote, that .NET don't be able to get proxyinformation in this configuration situation. Should be enough
to set the proxy with ObjReq.Proxy=null; to null?
Hope for help and so far
may the source be with you.
OliWan