WebResponse.GetResponse Error

J

Jonathan

Calling the System.Net.WebResponse.GetResponse method (see
code below) results in the following error:

System.Net.WebException: The underlying connection was
closed: An unexpected error occurred on a receive.
at(System.Net.HttpWebRequest.CheckFinalStatus())
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult
asyncResult)
at(System.Net.HttpWebRequest.GetResponse())

The URL works ok in IE on the same PC.

Does anyone know what would cause this problem, what other
checks I could try or any other method to do the same
thing as System.Net.WebResponse.GetResponse?

Thank you in advance.

Private Function HasWebConnection() As Boolean
Dim bResult As Boolean
Try
bResult = True
Dim URI As New System.Uri("http://www.microsoft.com")
Dim TestRequest As Net.WebRequest =
Net.WebRequest.Create(URI)
TestRequest.Timeout = Threading.Timeout.Infinite
Dim TestResponse As Net.WebResponse =
TestRequest.GetResponse
Catch ex As Exception
Console.WriteLine(ex.ToString)
bResult = False
End Try
Return bResult
End Function
 
T

Tian Min Huang

Hi,

Thanks for your post. I tested your code, and it works properly on my side.
Now I'd like to share the following information with you:

1. Since IE works properly, I'd like you to check connection setting in IE
options, especially, the proxy setting. You can use WebProxy to set HTTP
proxy for the WebRequest class.

WebProxy Class
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemnetwebproxyclasstopic.asp

2. I recommend you use the sample in KB article 303436 and check whether or
not it works on your side.
SAMPLE: Visual C# .NET Networking Classes HTTP Internet Client
http://support.microsoft.com/?id=303436

I look forward to your result.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

Hi,

Thank you for your informative reply.

When I checked the proxy settings in IE I found that under
the dial-up connection settings the proxy was disabled but
under LAN Settings that the proxy was incorrectly enabled.
Since I am using a dial-up connection I did not think that
this would make any difference (It even states "These
settings will not apply to any dial-up or VPN
connections.") but as a test, I disabled them anyway and
much to my surprise the code worked. I also tried
disconnecting the network cable but it made no difference.
The code always attempted to use the proxy settings under
LAN Settings regardless of the fact that the connection to
the Internet was via a dial-up connection.

This raises another question. Many of my application users
will have laptops that they will connect to a LAN when
they are in the office and to the Internet via dial-up
connections at home. I was hoping just to use the settings
in IE and not have the user set up special settings in my
application. Is there a way of checking which are the
appropriate proxy settings to use.

Thank you,

Jonathan
 
J

Jonathan

Hi,

Sorry for the double post. I forgot provide my alias.

Thank you for your informative reply.

When I checked the proxy settings in IE I found that under
the dial-up connection settings the proxy was disabled but
under LAN Settings that the proxy was incorrectly enabled.
Since I am using a dial-up connection I did not think that
this would make any difference (It even states "These
settings will not apply to any dial-up or VPN
connections.") but as a test I disabled them anyway and
much to my surprise the code worked. I also tried
disconnecting the network cable but it made no difference.
The code always attempted to use the proxy settings under
LAN Settings regardless of the fact that the connection to
the Internet was via a dial-up connection.

This raises another question. Many of my application users
will have laptops that they will connect to a LAN when
they are in the office and to the Internet via dial-up
connections at home. I was hoping just to use the settings
in IE and not have the user set up special settings in my
application. Is there a way of checking which are the
appropriate proxy settings to use.

Thank you,

Jonathan
 
T

Tian Min Huang

Hi Jonathan,

Thanks for your update. Generally speaking, the WebRequest will get the
proxy information from the settings in IE. If it is using DUN, you can add
the following code to clear the proxy:

Dim myProxy = New Net.WebProxy
TestRequest.Proxy = myProxy

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
T

Tian Min Huang

Hi Jonathan,

Thanks for your feedback.
Based on my experience, WebRequest only gets the proxy information from IE
settings and does not check what the active connection is.
detect that the user is has a DUN connection and then how do I know if they
don't have proxy settings on the DUN.

Though you can call GetAdaptersInfo() from you .NET managed application via
P/Invoke to check whether the computer connects via LAN or DUN, I strongly
recommend you catch the exception and try using WebRequest without proxy to
work around this problem.

I look forward to your feedback.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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

Top