Solved

Trying to read an RSS.xml from behind a proxy

Posted on 2004-09-16
13
326 Views
Last Modified: 2008-02-01
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
Comment
Question by:VBWeasel
  • 7
  • 6
13 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12076810
Does your program do other things besides reading RSS feeds?  There is a free RSSReader application called SharpReader.

Bob
0
 

Author Comment

by:VBWeasel
ID: 12076878
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12076905
What line does the error occur on?

Bob
0
 

Author Comment

by:VBWeasel
ID: 12076982
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12077109
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
 

Author Comment

by:VBWeasel
ID: 12077291
> 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 12077337
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12077365
Also:

myProxy.Credentials = CredentialCache.DefaultCredentials;

Bob
0
 

Author Comment

by:VBWeasel
ID: 12077904
No, we're not using port 80, so yes, I've had to use the form http://proxyserver:port.

Unfortunately, no luck yet.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12077965
I didn't notice this before, but why are you using:

request.Proxy = GlobalProxySelection.GetEmptyWebProxy();?

Shouldn't that be:

request.Proxy = proxyObject;

Bob
0
 

Author Comment

by:VBWeasel
ID: 12083410
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
 
LVL 96

Accepted Solution

by:
Bob Learned earned 430 total points
ID: 12083465
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
 

Author Comment

by:VBWeasel
ID: 12084343
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

919 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now