Solved

Trying to read an RSS.xml from behind a proxy

Posted on 2004-09-16
13
322 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

747 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