2

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!

1
  • Got the same problem with spanish. The accepted answer worked for me. Commented Jun 15, 2011 at 22:39

1 Answer 1

4

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);
            }
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Let me buy you a beer sometime.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.