网页请求编码问题 [英] Web Request Encoding Problem
本文介绍了网页请求编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到的编码问题下的任何想法code?
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;
感谢您的帮助!
推荐答案
谷歌是臭名昭著的检查用户代理
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);
}
}
这篇关于网页请求编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文