如何改变的HttpClient响应的编码 [英] How to change the encoding of the HttpClient response

查看:516
本文介绍了如何改变的HttpClient响应的编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解如何使用VS2012和异步等待关键字异步编程。这就是为什么我写这片code的: -

I'm trying to learn about Async programming using VS2012 and its Async Await keyword. That is why i wrote this piece of code :-

protected override async void OnNavigatedTo(NavigationEventArgs e)
    {
            var get = await GetResultsAsync("http://saskir.medinet.se");
            resultsTextBox.Text=get;
    }

    private async Task<string> GetResultsAsync(string uri)
    {
        var client = new HttpClient();
        var response = await client.GetStringAsync(uri);
        return response;
    }

问题是,当我尝试调试应用程序,它给了我这个味精的错误: - 字符集contentType中规定无效。使用无效字符集'不能读作字符串内容。我想这是因为该网站有一些瑞典字符,但我无法找到如何更改响应的编码。任何人都可以指导我PLZ?

The problem is that when i try to debug the application, it gives me an error with this msg:- 'The character set provided in ContentType is invalid. Cannot read content as string using an invalid character set'. I guess this is because the website have some Swedish char, but i can't find how to change the encoding of the response. Anyone can guide me plz?

推荐答案

您可能需要检查的编码选项,并获得正确的。否则,此code应该让你的反应会。

You may have to check the encoding options and get the correct one. Otherwise, this code should get you going with the response.

private async Task<string> GetResultsAsync(string uri)
    {
        var client = new HttpClient();
        var response = await client.GetByteArrayAsync(uri);
        var responseString = Encoding.Unicode.GetString(response, 0, response.Length - 1);
        return responseString;
    }

这篇关于如何改变的HttpClient响应的编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆