首页新闻找找看学习计划

HttpClient 设置编码

0
悬赏园豆:5 [已解决问题] 解决于 2018-11-29 16:21

string url = "http://www.time.ac.cn/stime.asp";
HttpClient helper = new HttpClient();
Func<Task<string>> datehtmlfun = async () => await helper.GetStringAsync(url);
Task<string> datehtml = datehtmlfun();
string html = datehtml.Result;

网站是gb2312的编码,直接调用GetStringAsync用的估计是默认的UTF-8编码
这个有什么办法吗?

左眼水星的主页 左眼水星 | 初学一级 | 园豆:41
提问于:2018-11-29 15:25
< >
分享
最佳答案
0

流的形式读出来,设置解码为gb2312

         string result = "";
        string url = "http://www.time.ac.cn/stime.asp";
        HttpClient httpclient= new HttpClient();
        var response = httpclient.GetAsync(url).Result;
        if (response.IsSuccessStatusCode)
        {
            Stream myResponseStream = response.Content.ReadAsStreamAsync().Result;
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("GBK"));
            result = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
        }

        return result;
收获园豆:5
chesterdotchen | 小虾三级 |园豆:542 | 2018-11-29 15:37
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册