Web请求编码问题

发布于 2024-11-07 18:11:14 字数 579 浏览 4 评论 0原文

您好,我遇到以下代码的编码问题,有什么想法吗?

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);
        }

我的截图是这样的;

在此处输入图像描述

感谢您的帮助!

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);
        }

My Screenshoot is like that;

enter image description here

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

是伱的 2024-11-14 18:11:14

Google 因检查 useragent HTTP 标头而臭名昭著。因为您没有将其所有内容编码为 ISO-8859-9。简单的解决方案是手动设置 HttpWebRequestUserAgent 属性。将其设置为您想要的任何内容,下面是一个 Firefox 字符串(以及一个额外的 Using 块):

        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);
            }
        }

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);
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文