Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-03-29
9
Medium Priority
?
1,479 Views
Last Modified: 2010-08-05
Hi,

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.
0
Comment
Question by:shifty_mc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 12

Expert Comment

by:dfiala13
ID: 10710500
Post your code. It could be a firewall issue, but if you are accessing the Internet otherwise it is unlikely.
0
 

Author Comment

by:shifty_mc
ID: 10710603
ok...

public Browser()
{
      Uri url = new Uri("http://www.radiotimes.com/tv/now.html");
      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)
            break;
            bytesRead += n;
            bytesToRead -= n;
      }
      FileStream fstr = new FileStream("C:\\webpagetest.html", FileMode.Create,FileAccess.Write);
      fstr.Write(inBuf, 0, bytesRead);
      str.Close();
      fstr.Close();            
}

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.
Thanks.
0
 
LVL 12

Expert Comment

by:dfiala13
ID: 10710697
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();
res.Close();
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:shifty_mc
ID: 10710761
yeah, still get the error in
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
0
 

Author Comment

by:shifty_mc
ID: 10710823
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?
0
 
LVL 12

Expert Comment

by:dfiala13
ID: 10710876
Ah, a proxy!  There is a class for working with that and a little code to tell your app to use a proxy.

http://support.microsoft.com/default.aspx?scid=kb;en-us;307023
0
 
LVL 12

Accepted Solution

by:
dfiala13 earned 1000 total points
ID: 10710885
You can add a test function that tries to connect to a well known and ususally up URL (http://www.microsoft.com).  If it fails you can switch over to the proxy code and possibly prompt the user for proxy info.
0
 

Author Comment

by:shifty_mc
ID: 10710931
ok, I'll give it a go,
thanks for your help.
0
 
LVL 12

Expert Comment

by:dfiala13
ID: 10710957
You're welcome.  Glad you got it working.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question