Web请求编码问题 [英] Web Request Encoding Problem
本文介绍了Web请求编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我在下面的代码中遇到编码问题?
Hi i'm getting encoding problems with the code below any ideas?
string url = "http://www.google.com/ig/api?weather=istanbul,TR&hl=tr";
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
string retVal = reader.ReadToEnd();
Response.Write(retVal);
}
我的Screenshoot就是这样;
My Screenshoot is like that;
>
非常感谢您的帮助!
推荐答案
Google臭名昭着的是检查 useragent
HTTP头。因为你没有设置它的编码一切 ISO-8859-9
。简单的解决方案是手动设置 HttpWebRequest
的 UserAgent
属性。设置为任何你想要的,下面是一个Firefox字符串(和额外的使用
块):
Google is notorious for checking the useragent
HTTP header. Because you're not setting it its encoding everything as ISO-8859-9
. The simple solution is to manually set the UserAgent
property of the HttpWebRequest
. Set it to anything you want, below is a Firefox string (and an extra Using
block):
string url = "http://www.google.com/ig/api?weather=istanbul,TR&hl=tr";
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1";
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string retVal = reader.ReadToEnd();
Console.WriteLine(retVal);
}
}
这篇关于Web请求编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文