The underlying connection was closed
You will encounter this problem if your machine is sitting behind a firewall proxy and you try to invoke the GetResponse method of the WebRequest class (WebRequest.GetResponse()). I have seen many posts on the issue but none worked. Many solutions suggest editing the machine.config file, but to no avail. I have gone to such broad tangents looking for a solution to this problem and found a very simple and effective solution and it all lies in including just a few lines in your web.config file.
The underlying connection was closed: Unable to connect to the remote server.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Net.WebException: The underlying connection was closed: Unable to connect to the remote server.
Just include the following lines in your web.config file.
<proxy proxyaddress="http://your.proxy.server:80" bypassonlocal="true"/>
This should fix the problem and put an end to a previously long lasting headache.
That’s all folks and happy programming