2

Based on the this sourcecode I'm not able to retrieve the data from the API into XDocument.

I retrieve the error message

{"The remote server returned an error: (400) Bad Request."}

Question:
I don't know what to do?

XDocument xml = XDocument.Parse(new
WebClient().DownloadString("http://api.arbetsformedlingen.se/af/v0/platsannonser/matchning?lanid=1&kommunid=180&yrkesid=2419&1&antalrader=10000"));
3
  • What errors are you getting? Some more information would help. Commented Jun 6, 2015 at 13:30
  • I want to retrieve that data that is from the link into the variable xml Commented Jun 6, 2015 at 13:33
  • possible duplicate Commented Jun 6, 2015 at 13:47

1 Answer 1

4

You need to send HTTP headers:

using (WebClient client = new WebClient())
{
    client.Headers.Add("Accept-Language", " en-US");
    client.Headers.Add("Accept", "application/xml");
    client.Headers.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");

    XDocument xml = XDocument.Parse(client.DownloadString("http://api.arbetsformedlingen.se/af/v0/platsannonser/matchning?lanid=1&kommunid=180&yrkesid=2419&1&antalrader=10000"));
}
Sign up to request clarification or add additional context in comments.

2 Comments

The server is returning XML, and that is the format that the OP is expecting. So I would change the Accept header to just "application/xml".
The code is working but I retrieve another eroror message "{System.Xml.XmlException} {"Data at the root level is invalid. Line 1, position 1."}". I need to add a new message in Stackoverflow. Thank you for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.