When sending a test request in Burp I get correct values for åäö in Swedish but using HttpClient I only get this character:
I have tried setting the Accept-Language
header to sv-SE
and sv
but with the same result. I have also tried getting GetByteArrayAsync
and converting this to UTF-8 but no luck there neither.
private HttpClient client = new HttpClient();
public HttpService()
{
client.DefaultRequestHeaders.Add("Accept-Language", "sv");
client.DefaultRequestHeaders.Add("Accept-Charset", "utf-8");
}
public string GetRequest(string url)
{
using (HttpResponseMessage response = client.GetAsync(url).Result)
{
//Same result
//var byteArray = response.Content.ReadAsByteArrayAsync().Result;
//var result = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);
using (HttpContent content = response.Content)
{
var result = content.ReadAsStringAsync().Result;
return result;
}
}
}
Update:
Headers from server:
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 31903
Content-Type: application/json
Expires: Sun, 14 May 2017 22:00:00 GMT
Server: Microsoft-IIS/8.5
Set-Cookie: ASPSESSIONIDCSACQTRA=<REMOVED>; path=/
X-Powered-By: ASP.NET
Date: Mon, 15 May 2017 13:10:18 GMT
Thank you @RemusRusanu. Working code:
public string GetRequest(string url)
{
using (HttpResponseMessage response = client.GetAsync(url).Result)
{
var byteArray = response.Content.ReadAsByteArrayAsync().Result;
var result = Encoding.GetEncoding("ISO-8859-1").GetString(byteArray, 0, byteArray.Length);
return result;
}
}
Ogglas answer will fix it for åäö but might not work for ÅÄÖ. ISO-8859-1 correspondes to either
- Windows-1252 Western European (Windows). Code page 1252
or
- iso-8859-1 Western European (ISO). Code page 28591
To be sure to get the correct encoding, you might want to use Encoding.GetEncoding(int codePage)
instead. For more information about different encoding see, https://docs.microsoft.com/en-us/dotnet/api/system.text.encoding?view=netframework-4.8
Here is an example code where the response string is ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖabcdefghijklmnopqrstuvwxyzåäö0123456789!"#¤%&/()=?^*_:;>@£${[]}\\~|<,.-¨'´+
using System.Net.Http;
using log4net;
using System.Text;
namespace Stackoverflow
{
public static class Enc {
private static readonly ILog log = LogManager.GetLogger(typeof(Enc));
public static string GetRequest(HttpClient client, string url, int codepage) {
using (HttpResponseMessage response = client.GetAsync(url).Result) {
var byteArray = response.Content.ReadAsByteArrayAsync().Result;
var result = Encoding.GetEncoding(codepage).GetString(byteArray, 0, byteArray.Length);
return result;
}
}
public static void Example(HttpClient client, string url) {
string result1 = GetRequest(client, url, 1252);
string result2 = GetRequest(client, url, 28591);
log.Debug(result1);
log.Debug(result2);
}
}
}
This gives output:
ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖabcdefghijklmnopqrstuvwxyzåäö0123456789!"#¤%&/()=?^*_:;>@£${[]}\~|<,.-¨'´+"
ABCDEFGHIJKLMNOPQRSTUVWXYZÿÿÿabcdefghijklmnopqrstuvwxyzåäö0123456789!"#¤%&/()=?^*_:;>@£${[]}\~|<,.-¨'´+
where ÿ shows up as a black character xC3? in my logs.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.