使用WebRequest时,URL中含有中文的问题

如下URL:http://php.weather.sina.com.cn/search.php?city=北京,由于中间有中文字符,传到webrequest中时,得不到正确结果。为了使WebRequest操作时,URL支持中文,需要对这些中文特殊处理一下:

Encoding ed=Encoding.GetEncoding("gb2312");
WebRequest req = WebRequest.Create("http://php.weather.sina.com.cn/search.php?city=" + HttpUtility.UrlEncode("北京", ed));
WebResponse response = req.GetResponse();
string content = string.Empty;
using (Stream st = response.GetResponseStream())
{
    StreamReader sr = new StreamReader(st, Encoding.Default);
    content = sr.ReadToEnd();
}

搞定!

原文地址:https://www.cnblogs.com/skywind/p/1139790.html