Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows 8:下载带编码的字符串(WinRT)_C#_Windows 8_Windows Runtime_Microsoft Metro - Fatal编程技术网

C# Windows 8:下载带编码的字符串(WinRT)

C# Windows 8:下载带编码的字符串(WinRT),c#,windows-8,windows-runtime,microsoft-metro,C#,Windows 8,Windows Runtime,Microsoft Metro,我使用此代码从Internet下载字符串 public static async Task<string> DownloadPageAsync(string url) { HttpClientHandler handler = new HttpClientHandler {UseDefaultCredentials = true, AllowAutoRedirect = true}; HttpClient client = new HttpClient(handler

我使用此代码从Internet下载字符串

public static async Task<string> DownloadPageAsync(string url)
{
    HttpClientHandler handler = new HttpClientHandler {UseDefaultCredentials = true, AllowAutoRedirect = true};
    HttpClient client = new HttpClient(handler);            
    client.MaxResponseContentBufferSize = 196608;
    HttpResponseMessage response = await client.GetAsync(url);

    response.EnsureSuccessStatusCode();

     string responseBody = await response.Content.ReadAsStringAsync();
     return responseBody;
  }
公共静态异步任务下载页面异步(字符串url)
{
HttpClientHandler handler=newHttpClientHandler{UseDefaultCredentials=true,AllowAutoRedirect=true};
HttpClient=新的HttpClient(处理程序);
client.MaxResponseContentBufferSize=196608;
HttpResponseMessage response=wait client.GetAsync(url);
response.EnsureSuccessStatusCode();
string responseBody=wait response.Content.ReadAsStringAsync();
返回响应体;
}
但它只适用于UTF8文档。在哪里设置编码?

设置HttpResponse对象的“ContentEncoding”属性:

价值包括:

    • System.Text.asciencoding
    • System.Text.Unicode编码
    • System.Text.utf7编码
    • System.Text.utf8编码
附言:


这实际上并不是“Metro”本身——只是WinRT中的C#/.Net(尽管是.Net 4.x)

HttpContent从Headers属性读取编码。如果来自服务器的HTTP响应没有使用编码设置内容类型头,它将尝试在流中查找BOM标记,如果没有BOM,它将默认为UTF-8编码


如果服务器未发送正确的内容类型标头,请使用HttpContent.ReadAsStreamAsync()方法,并使用自己的编码类实例正确解码数据。

将ReadAsStringAsync更改为ReadAsBufferAsync,并使用所需编码解析结果

var buffer = await response.Content.ReadAsBufferAsync();
byte [] rawBytes = new byte[buffer.Length];
using (var reader = DataReader.FromBuffer(buffer))
{
    reader.ReadBytes(rawBytes);
}

var res = Encoding.UTF8.GetString(rawBytes, 0, rawBytes.Length);   

我本来打算回答这个帖子的。在我读了这个回复之后,没有任何意义。我只是想建议一旦他有了字符串,就把UTF8的编码转换成所需的编码。起初,我建议“写你自己的方法”,因为我认为他是做研究的。我真傻。你注意到我的问题是问温特的吗?HttpResponseMessage上没有编码属性。根据使用自定义生成的编码类解决了该问题