使用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();
}

搞定!

posted on 2008-04-07 09:14  Jack Niu  阅读(1220)  评论(0编辑  收藏  举报

Affiliate Marketing and Web Technology