Encoding trouble with HttpWebResponse

后端 未结 7 1673
时光取名叫无心
时光取名叫无心 2020-11-29 07:27

Here is a snippet of the code :

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(request.RawUrl);
WebRequest.DefaultWebProxy = null;//Ensure that         


        
7条回答
  •  感情败类
    2020-11-29 08:02

    There are some good solutions here, but they all seem to be trying to parse the charset out of the content type string. Here's a solution using System.Net.Mime.ContentType, which should be more reliable, and shorter.

     var client = new System.Net.WebClient();
     var data = client.DownloadData(url);
     var encoding = System.Text.Encoding.Default;
     var contentType = new System.Net.Mime.ContentType(client.ResponseHeaders[HttpResponseHeader.ContentType]);
     if (!String.IsNullOrEmpty(contentType.CharSet))
     {
          encoding = System.Text.Encoding.GetEncoding(contentType.CharSet);
     }
     string result = encoding.GetString(data);
    

提交回复
热议问题