Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2194
  • Last Modified:

client.downloaddata issue with webclient

Hi EE,
I have a URL that returns an AtomFeed that is fine in my browser although it fails when reading the feed in C#?
Im trying to read this feed in my console application running it locally.  When I try this I get a "The remote server returned an error: (500) Internal Server Error."
I'm using IIS 5.1 on an XP machine and running the code from within visual studio whilst debugging.
Any ideas why this fails and how to resolve it please as I need to read the atom feed into memory and extract certain nodes out of it.

Thanks!

public static void ParseAtomFeed()
        {
            string url = @"http://xxxxxxxxxx";
            WebClient client = new WebClient();
            byte[] bytedata = client.DownloadData(url);  // causes error
 
	    // when successful read through and extract a node from the feed
        }

Open in new window

0
jimbona27
Asked:
jimbona27
  • 7
  • 2
1 Solution
 
aibusinesssolutionsCommented:
Can you read it into a string?  String bytedata = client.DownloadString(url);

Also, does the atom feed require a browser cookie when viewing it?  Does it collect browser information?
0
 
jimbona27Author Commented:
just an update, im trying this code now because it works with a tutorial url but it doesnt work with my url i need to use it with..  'response' displays the contents of the url as I expect it although when I change it to the url to my atom feed I get an internal 500 error..?


string url="www.national-lottery.co.uk/player/p/results/resultsHistory/resultsHistoryAction.do";
                        
WebRequest myRequest = WebRequest.Create(url);
WebResponse myResponse = myRequest.GetResponse();
 
Stream strm = myResponse.GetResponseStream();
StreamReader sr = new StreamReader(strm);
string response = sr.ReadToEnd();

Open in new window

0
 
jimbona27Author Commented:
using your suggestion, see code snippet, I get the same 500 internal error, but only when using my url.  the tutorial url works for me.  

any ideas?
string url1 = "http://www.national-lottery.co.uk/player/p/results/resultsHistory/resultsHistoryAction.do";
string url2 = "http://xxx";
 
// works
            WebClient client = new WebClient();
            string bytedata = client.DownloadString(url1);
 
// fails
            WebClient client2 = new WebClient();
            string bytedata2 = client2.DownloadString(url12);

Open in new window

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.

 
jimbona27Author Commented:
i should also state that the url I want to use is a dev server. However, when viewing the atom feed on the dev server through IE I get to see the feed perfectly.
0
 
aibusinesssolutionsCommented:
I'm wondering if there is something session related, or cookie related with your feed specifically.

Try adding a cookie to your webrequest.  Here is some code for a cookie aware webclient class.


public class CookieAwareWebClient : WebClient    {
   private CookieContainer m_container = new CookieContainer();
   protected override WebRequest GetWebRequest(Uri address){
      WebRequest request = base.GetWebRequest(address);
      if (request is HttpWebRequest) { 
        (request as HttpWebRequest).CookieContainer = m_container;
        }
      return request;
   }    
}

Open in new window

0
 
jimbona27Author Commented:
i have several links, some return a file, i.e. when using Internet Explorer I get a prompt to download a file.  With this url the code works..

However, I have now realised the url I need to use does not prompt to download a file, although I need to pull the feed to XPATH search through it.  

In this case, using the same code (see below) the code fails on DownloadData();

any ideas?

Thanks!
string url = @"xxx";
byte[] bytedata = null;
WebClient client = new WebClient();
Stream data = null;
string feedstring = string.Empty;
 
bytedata = client.DownloadData(url);  // line fails '(500) internal server error'
client.DownloadFile(url, @"c:\test.xml");
data = client.OpenRead(url);
StreamReader sr = new StreamReader(data);
return sr.ReadToEnd();

Open in new window

0
 
jimbona27Author Commented:
i've added the following code although I still get the 500 error message..
Any ideas?


    public class CookieAwareWebClient : WebClient
    {
        private CookieContainer m_container = new CookieContainer();
        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);
            if (request is HttpWebRequest)
            {
                (request as HttpWebRequest).CookieContainer = m_container;
            }
            return request;
        }
    }
 
 
 
CookieAwareWebClient client = new CookieAwareWebClient();  // fails
bytedata = client.DownloadData(url);

Open in new window

0
 
jimbona27Author Commented:
sorry, this line fails:
client.DownloadData(url);
0
 
jimbona27Author Commented:
code works now, was something the server response wasnt right thats now fixed.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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