Error: "Unable to connect to the remote server" when using GetResponse or EndGetResponse


the title says it all really - I'm certain the code's right, after scouring the internet for hours trying to solve this, but I can't get away from the error.
I'm basically trying to save a webpage to file, given a url.
So is it something to do with my internet setup or firewall?
Incidentally, when I try to add a web reference in visual studio, it gives the same error but still brings the webpage up.

Any ideas? Cheers all.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Post your code. It could be a firewall issue, but if you are accessing the Internet otherwise it is unlikely.
shifty_mcAuthor Commented:

public Browser()
      Uri url = new Uri("");
      HttpWebRequest wr = (HttpWebRequest) WebRequest.Create(url);            
      AsyncCallback a = new AsyncCallback(OnComplete);
      IAsyncResult r = (IAsyncResult) wr.BeginGetResponse(a,wr);

static void OnComplete(IAsyncResult ar)
      HttpWebRequest r = (HttpWebRequest) ar.AsyncState;
      HttpWebResponse re = (HttpWebResponse) r.EndGetResponse(ar);                        
      Stream str = re.GetResponseStream();
      Byte[] inBuf = new Byte[500000];
                int bytesToRead = (int) inBuf.Length;
      int bytesRead = 0;
      while (bytesToRead > 0)
            int n = str.Read(inBuf, bytesRead,bytesToRead);
                      if (n==0)
            bytesRead += n;
            bytesToRead -= n;
      FileStream fstr = new FileStream("C:\\webpagetest.html", FileMode.Create,FileAccess.Write);
      fstr.Write(inBuf, 0, bytesRead);

The error occurs in the line
HttpWebResponse re = (HttpWebResponse) r.EndGetResponse(ar);
I've also tried it synchronously, with GetResponse, and get the same error.
I think the problem is that you are never starting the communication.  Here's a snippet I wrote for a scraper.  It's snychronous and it uses a string URL as opposed to creating a Uri object.

Try this and see if it works (works for me), then we will at least know if it is a code problem or not.

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sURL);
req.KeepAlive = false;
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(),true);
string s = sr.ReadToEnd();
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

shifty_mcAuthor Commented:
yeah, still get the error in
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
shifty_mcAuthor Commented:
Ahh, I've got it - I was connecting to the internet through a proxy which was causing the problem.
When I turned that off it worked fine.
But how would I use this code when working with proxies and so on - ideally I'd be able to check the connection on a user's computer and adjust the connection appropriately?
Ah, a proxy!  There is a class for working with that and a little code to tell your app to use a proxy.;en-us;307023
You can add a test function that tries to connect to a well known and ususally up URL (  If it fails you can switch over to the proxy code and possibly prompt the user for proxy info.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shifty_mcAuthor Commented:
ok, I'll give it a go,
thanks for your help.
You're welcome.  Glad you got it working.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.