Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding with HttpClient in .NET 4.5

I'm consuming some data using the fogbugz XML API. This API always offers data as UTF-8.

When using the WebClient class for making a request I am able to set the encoding. For example:

var result = new WebClient(); 
result.Encoding = Encoding.UTF8;

But what about the HttpClient class?

HttpClient client = new HttpClient();

Should I use:

client.GetByteArrayAsync(url);

...and then convert the bytes from the encoding (UTF-8) to a string?

Or is there a way to directly get the content as a UTF-8 string?

using (var client = Connector.GetHttpClient())
{
    var byteData = await client.GetByteArrayAsync(url);
    data = Encoding.UTF8.GetString(byteData);
}

Finally, here is an excerpt from the XML response:

<?xml version="1.0" encoding="UTF-8"?>
<response>
like image 656
Boas Enkler Avatar asked Jun 13 '12 15:06

Boas Enkler


People also ask

What is the difference between HttpClient and HttpWebRequest?

WebClient is just a wrapper around HttpWebRequest, so it uses HttpWebRequest internally. The WebClient bit slow compared to HttpWebRequest. But is very much less code. we can use WebClient for simple ways to connect and work with HTTP services.

Is .NET HttpClient thread-safe?

Since HttpClient instances are thread-safe and don't hold much in the way of state (except if you're setting up, eg, default headers or base urls), you can also use a singleton pattern with them - this is a performant way to do a lot of concurrent http requests.

What is .NET HttpClient?

The HttpClient class is used to send and receive requests to an HTTP endpoint. In this article, Camilo Reyes describes how to work with the class and how to avoid common pitfalls and issues. The . NET framework comes with a suite of tools to build distributed systems.


2 Answers

If I understand correctly, you don't need a string, you need XML.

So, assuming your data is not too big, read a byte array with

byte[] bytes = await client.GetByteArrayAsync(url); 

then create a memory stream from that array, and then read XML from that stream, for example:

XElement element = XElement.Load(new MemoryStream(bytes), LoadOptions.None);

If you're using another XML API, you can use

XmlReader reader = XmlReader.Create(new MemoryStream(bytes));
like image 83
Kris Vandermotten Avatar answered Sep 25 '22 15:09

Kris Vandermotten


You should be able to use GetStringAsync - I'd expect the encoding to be determined by the headers in the HTTP response. If the server doesn't specify the encoding, then you should potentially ask for that to be fixed.

Alternatively, if you're fetching XML data, just fetch it as a byte array and parse that binary directly - the XML declaration should specify the encoding for non-UTF-8/UTF-16 data anyway, so I'd argue that actually there's less room for error this way.

like image 25
Jon Skeet Avatar answered Sep 25 '22 15:09

Jon Skeet