1

I use the weather API in my ASP.Net page.

If I add the language (hl) to the query, I will get this error: "Invalid character in the given encoding. Line 1, position 526.".
It works without the get parameter for language, but I want to localize the output.

Here is my code with the error in second line:

  XmlDocument doc = new XmlDocument();
            doc.Load("http://www.google.com/ig/api?hl=de&weather=" + location );

this works:

  XmlDocument doc = new XmlDocument();
            doc.Load("http://www.google.com/ig/api?weather=" + location );

Any idea?

hippietrail
  • 15,848
  • 18
  • 99
  • 158
PassionateDeveloper
  • 14,558
  • 34
  • 107
  • 176
  • 1
    **The Google weather API was shut down in 2012** -> http://stackoverflow.com/questions/12145820/google-weather-api-gone/35943521 – John Slegers Mar 11 '16 at 15:55

2 Answers2

3

For some reason, Google isn't UTF encoding the output. Here is a way for you to compensate:

WebClient client = new WebClient();
string data = client.DownloadString("http://www.google.com/ig/api?hl=de&weather=YourTown");

byte[] encoded = Encoding.UTF8.GetBytes(data);

MemoryStream stream = new MemoryStream(encoded);

XmlDocument xml = new XmlDocument();
xml.Load(stream);

Console.WriteLine(xml.InnerXml);
Console.ReadLine();
Forgotten Semicolon
  • 13,909
  • 2
  • 51
  • 61
2

You can do it using HttpWebRequest in place of WebClient as like below:

HttpWebRequest myRequest;  
HttpWebResponse myResponse= null;  
XmlDocument MyXMLdoc = null; 

myRequest = (HttpWebRequest)WebRequest.Create("http://www.google.com/ig/api" + 
    "?weather=" + string.Format(location));  
myResponse = (HttpWebResponse)myRequest.GetResponse();  
MyXMLdoc = new XmlDocument();  
MyXMLdoc.Load(myResponse.GetResponseStream());  
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
asif bhura
  • 71
  • 7