• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 370
  • Last Modified:

Trying to read an RSS.xml from behind a proxy

Here is some of the code I'm using to try to connect to "http://msdn.microsoft.com/rss.xml." Unfortunately, I'm behind a proxy. It connects to web pages in our intranet but nothing outside. I was getting a (407) Proxy Authentication Required error before, but I think I've fixed that. I'm currently getting a error "The underlying connection was closed. The remote name could not be resolved."
Any suggestions would be appreciated. :)

using System.Net;

private void button1_Click(object sender, System.EventArgs e)
      {
      string html = GetPageHTML(txtURL.Text, 10);
      txtHTML.Text = html;
      }

public string GetPageHTML(string pageUrl, int timeoutSeconds)
      {
      WebResponse response = null;

      try
      {
      // Setup of proxy - should pull default settings from IE?
      WebProxy proxyObject = WebProxy.GetDefaultProxy();
      // Also tried this...
      //WebProxy proxyObject = new WebProxy("myproxyaddress",true);
      GlobalProxySelection.Select = proxyObject;

      // Setup Web request
      WebRequest request = WebRequest.Create(pageUrl);
      request.PreAuthenticate = true; //Do I need this?
      request.Proxy = GlobalProxySelection.GetEmptyWebProxy();
                request.Credentials = new NetworkCredential("username", "password");
      request.Timeout = (timeoutSeconds * 1000);

      // Retrieve data from request
      // *** crashes on this step***
      response = request.GetResponse();

      return "";
      
      // go on to read xml, etc...
      // .........
      }            

      catch (Exception ex)
      {
      // Error occured grabbing data, return empty string.
      MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
      return "";
      }
      
      finally
      {
      // Check if exists, then close the response.
      if ( response != null )
      {
      response.Close();
      }
      }
      }
0
VBWeasel
Asked:
VBWeasel
  • 7
  • 6
1 Solution
 
Bob LearnedCommented:
Does your program do other things besides reading RSS feeds?  There is a free RSSReader application called SharpReader.

Bob
0
 
VBWeaselAuthor Commented:
I've used SharpReader and I like it, but I'm trying to make a smaller, simpler custom app for work. I also have a couple other features I'd like to integrate into it. Sadly, the proxy server is holding me back. I used SharpReader here to be sure that I could even bypass the proxy and receive feeds, and it worked fine. I'm just doing something wrong and I'm not sure what it is yet. :(
0
 
Bob LearnedCommented:
What line does the error occur on?

Bob
0
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.

 
VBWeaselAuthor Commented:
Error occurs on the line:
  response = request.GetResponse();

It should also be noted that the line following that (return "";) will eventually return a value, not "".
0
 
Bob LearnedCommented:
What proxy are you using?  We use Micro$oft Proxy 2.0 here, and the code worked for me.  I don't see where you are getting the response stream to get the text.  You are returning an empty string.

Bob
0
 
VBWeaselAuthor Commented:
> What proxy are you using?
I'm not sure what proxy we're using. I just know that the error: "The underlying connection was closed. The remote name could not be resolved." is winning this battle. At least I was able to get past the "(407) Proxy Authentication Required" error. :P

> I don't see where you are getting the response stream to get the text.  You are returning an
> empty string.
Yeah, at that point I am just returning an empty string, I don't have a stream reader in place yet because I can even get the GetResponse to work properly outside the proxy. If the code works for you though, I'll go back to the drawing board.

Any other suggestions, anyone?
0
 
Bob LearnedCommented:
Is the URL for the proxy in the form:  http://proxyserver:port?  Are you using port 80 with your proxy server? There is a "domain" property for the proxy:

myProxy.Credentials = new NetworkCredential("username", "password", "domain");

Bob
0
 
Bob LearnedCommented:
Also:

myProxy.Credentials = CredentialCache.DefaultCredentials;

Bob
0
 
VBWeaselAuthor Commented:
No, we're not using port 80, so yes, I've had to use the form http://proxyserver:port.

Unfortunately, no luck yet.
0
 
Bob LearnedCommented:
I didn't notice this before, but why are you using:

request.Proxy = GlobalProxySelection.GetEmptyWebProxy();?

Shouldn't that be:

request.Proxy = proxyObject;

Bob
0
 
VBWeaselAuthor Commented:
lol

I remember changing from request.Proxy = proxyObject; to request.Proxy = GlobalProxySelection.GetEmptyWebProxy(); but I don't remember why. An act of desperation, I guess. Still doesn't work though. I'll keep working at it.

Once I changed that line back, I'm back to getting the "(407) Proxy Authentication Required" error again. Hmmm.

Thank you for all your suggestions, Bob. I hope we're on the right track. :)
0
 
Bob LearnedCommented:
This is a very interesting exercise.  I am glad that I don't have to deal with all this mess with authenticated proxies.  

Bob
0
 
VBWeaselAuthor Commented:
Finally got it to work with this code:

WebProxy proxyObject = new WebProxy(proxy, 80);
proxyObject.BypassProxyOnLocal = true;
GlobalProxySelection.Select = proxyObject;
proxyObject.Credentials = new NetworkCredential("username", "password");

This whole project has been trial and error, I'm still not raelly sure why it's working now and not before. Be that as it may, you still get the points for helping. :D

Thanks again!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now