Sentropie Sentropie - 2 months ago 8
C# Question

Xml response file: Received in browser, not via C#

I'm trying to access the last.fm APIs via C#. As a first test I'm querying similar artists if that matters.

I get an XML response when I pass a correct artist name, i.e. "Nirvana". My problem is when I deliver an invalid name (i.e. "Nirvana23") I don't receive XML but an error code (403 or 400) and a

WebException
.

Interesting thing: If I enter the URL inside a browser (tested with Firefox and Chrome) I receive the XML file I want (containing a lastfm specific error message).

I tried both
XmlReader
and
XDocument
:

XDocument doc = XDocument.Load(requestUrl);


and
HttpWebRequest
:

string httpResponse = "";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUrl);

HttpWebResponse response = null;
StreamReader reader = null;

try
{
response = (HttpWebResponse)request.GetResponse();
reader = new StreamReader(response.GetResponseStream());
httpResponse = reader.ReadToEnd();
}


The URL is something like "http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist=Nirvana23" (and a specific key given by lastfm, but even without it - it should return XML). A link to give it a try: link (this is the error file I cannot access via C#).

What I also tried (without success): comparing the request by both the browser and my program with the help of WireShark. Then I added some headers to the request, but that didn't help either.

Answer

In .NET the WebRequest is converting HTTP error codes into exceptions, while your browser is just ignoring them since the response is not empty. If you catch the exception then the GetResponseStream method should still return the error XML that you are expecting.

Edit:

Try this:

string httpResponse = "";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUrl);

WebResponse response = null;
StreamReader reader = null;
try
{
    response = request.GetResponse();
}
catch (WebException ex)
{
    response = ex.Response;
}

reader = new StreamReader(response.GetResponseStream());
httpResponse = reader.ReadToEnd();