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

x
?
Solved

Trying to read an RSS.xml from behind a proxy

Posted on 2004-09-16
13
Medium Priority
?
361 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
[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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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
 
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 1290 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

662 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