I used WebClient to get an Xml object from a restfull service (.net web api) and everything worked great:
using(WebClient client = new WebClient())
{
client.Encoding = UTF8Encoding.UTF8;
client.Headers[HttpRequestHeader.ContentType] = "text/xml";
client.Credentials = // ....;
xmlResult = webClient.DownloadString(url);
}
....
this code works great. I get an Xml as a string back, everyone's happy.
Now, I changed it so it would work with HttpClient instead and I can't get an Xml returned - always a json as a string.
using(var handler = new HttpClientHandler() {Credentials = new NetworkCredentials})
using(var client = new HttpClient(handler))
{
var request = new HttpRequestMessage(HttpMethod.Get, url);
request.Headers.Add(HttpRequestHeader.ContentType.ToString(), "text/xml");
returnedXml = client.SendAsync(request).Result.Content.ReadAsStringAsync().Result;
}
What am I doing wrong? How can I get the Xml I long for?
Thanks
Just Try this one ..
using(var handler = new HttpClientHandler() {Credentials = new NetworkCredentials})
using(var client = new HttpClient(handler))
{
client.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("text/xml"));
var request = new HttpRequestMessage(HttpMethod.Get, url);
returnedXml = client.SendAsync(request).Result.Content.ReadAsStringAsync().Result;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With