Usuário com melhor resposta
Problemas de Encoding.

Pergunta
-
Pessoal, estou com o seguinte problema.
Preciso consumir o service weather do Google.
Segui o seguinte exemplo:
http://www.vsasp.net/apis/google-weather-api/
Está funcionando muito bem, exceto se quando vou realizar uma busca, contém um ascento.
Neste caso retorna o seguinte erro:Invalid character in the given encoding
Esse erro ocorre exatamente na última linha do código abaixo.
HttpWebRequest GoogleRequest; HttpWebResponse GoogleResponse = null; XmlDocument GoogleXMLdoc = null; GoogleRequest = (HttpWebRequest)WebRequest.Create("http://www.google.com/ig/api?weather=" + string.Format(location)); GoogleResponse = (HttpWebResponse)GoogleRequest.GetResponse(); GoogleXMLdoc = new XmlDocument(); //--Erro ocorre nessa linha, no Load.----------------- GoogleXMLdoc.Load(GoogleResponse.GetResponseStream());
Alguém sabe como configurar o encode ou alguma forma de corrigir este problema?
Grato antecipadamente.
Notepad na mão e uma idéia na cabeça......Se foi útil, marca lá.... Me ajuda que eu marco também... heheheh
Respostas
-
Glauber,
No HttpWebRequest existe a propriedade TransferEncoding, não resolve?
Acho que o link abaixo pode te ajudar:
http://stackoverflow.com/questions/5542900/google-weather-api-in-asp-net
Alexsandre Rodrigues de Almeida - MCTS .NET Framework - Web Applications
E-mail: alexsandrer@gmail.com
Twitter: @AlexRAlmeida- Marcado como Resposta Glauber Rocha sexta-feira, 13 de julho de 2012 13:32
-
Isso aê Alexandre.
Seu link foi muito útil. Juntei umas idéias do seu link com o code que estava fazendo e funcionou.Para ficar registrado, segue versão final do código.
public static StringBuilder GoogleWeatherReturn(string location) { XmlDocument GoogleXMLdoc = null; StringBuilder str = new StringBuilder(); WebClient client = new WebClient(); string data = client.DownloadString("http://www.google.com/ig/api?hl=pt-br&unit=C&weather=" + string.Format(location)); byte[] encoded = Encoding.UTF8.GetBytes(data); MemoryStream stream = new MemoryStream(encoded); try { GoogleXMLdoc = new XmlDocument(); GoogleXMLdoc.Load(stream); XmlNode root = GoogleXMLdoc.DocumentElement; XmlNodeList nodeList1 = root.SelectNodes("weather/forecast_information"); str.Append("<b>Cidade : " + nodeList1.Item(0).SelectSingleNode("city").Attributes["data"].InnerText + "</b>"); XmlNodeList nodeList = root.SelectNodes("weather/current_conditions"); str.Append(""); str.Append("<table class='bordered' cellpadding='5'><tbody><tr><td><b><big><nobr>" + nodeList.Item(0).SelectSingleNode("temp_c").Attributes["data"].InnerText + " °C | " + nodeList.Item(0).SelectSingleNode("temp_f").Attributes["data"].InnerText + " °F</nobr></big></b><br>"); str.Append("<b>Situação Atual:</b> " + nodeList.Item(0).SelectSingleNode("condition").Attributes["data"].InnerText + "<br>"); str.Append("" + nodeList.Item(0).SelectSingleNode("wind_condition").Attributes["data"].InnerText + "<br>"); str.Append(nodeList.Item(0).SelectSingleNode("humidity").Attributes["data"].InnerText); nodeList = root.SelectNodes("descendant::weather/forecast_conditions"); foreach (XmlNode nod in nodeList) { str.Append("</td><td align='center'>" + nod.SelectSingleNode("day_of_week").Attributes["data"].InnerText + "<br>"); str.Append("<img src='http://www.google.com" + nod.SelectSingleNode("icon").Attributes["data"].InnerText + "' alt='" + nod.SelectSingleNode("condition").Attributes["data"].InnerText + "'><br>"); str.Append(nod.SelectSingleNode("low").Attributes["data"].InnerText + "°C | "); str.Append(nod.SelectSingleNode("high").Attributes["data"].InnerText + "°C"); } str.Append("</td></tr></tbody></table>"); } catch (System.Exception ex) { str.Append(ex.Message); } finally { stream.Close(); } return str; }
Notepad na mão e uma idéia na cabeça......Se foi útil, marca lá.... Me ajuda que eu marco também... heheheh- Marcado como Resposta Glauber Rocha sexta-feira, 13 de julho de 2012 13:35
Todas as Respostas
-
Glauber,
No HttpWebRequest existe a propriedade TransferEncoding, não resolve?
Acho que o link abaixo pode te ajudar:
http://stackoverflow.com/questions/5542900/google-weather-api-in-asp-net
Alexsandre Rodrigues de Almeida - MCTS .NET Framework - Web Applications
E-mail: alexsandrer@gmail.com
Twitter: @AlexRAlmeida- Marcado como Resposta Glauber Rocha sexta-feira, 13 de julho de 2012 13:32
-
Isso aê Alexandre.
Seu link foi muito útil. Juntei umas idéias do seu link com o code que estava fazendo e funcionou.Para ficar registrado, segue versão final do código.
public static StringBuilder GoogleWeatherReturn(string location) { XmlDocument GoogleXMLdoc = null; StringBuilder str = new StringBuilder(); WebClient client = new WebClient(); string data = client.DownloadString("http://www.google.com/ig/api?hl=pt-br&unit=C&weather=" + string.Format(location)); byte[] encoded = Encoding.UTF8.GetBytes(data); MemoryStream stream = new MemoryStream(encoded); try { GoogleXMLdoc = new XmlDocument(); GoogleXMLdoc.Load(stream); XmlNode root = GoogleXMLdoc.DocumentElement; XmlNodeList nodeList1 = root.SelectNodes("weather/forecast_information"); str.Append("<b>Cidade : " + nodeList1.Item(0).SelectSingleNode("city").Attributes["data"].InnerText + "</b>"); XmlNodeList nodeList = root.SelectNodes("weather/current_conditions"); str.Append(""); str.Append("<table class='bordered' cellpadding='5'><tbody><tr><td><b><big><nobr>" + nodeList.Item(0).SelectSingleNode("temp_c").Attributes["data"].InnerText + " °C | " + nodeList.Item(0).SelectSingleNode("temp_f").Attributes["data"].InnerText + " °F</nobr></big></b><br>"); str.Append("<b>Situação Atual:</b> " + nodeList.Item(0).SelectSingleNode("condition").Attributes["data"].InnerText + "<br>"); str.Append("" + nodeList.Item(0).SelectSingleNode("wind_condition").Attributes["data"].InnerText + "<br>"); str.Append(nodeList.Item(0).SelectSingleNode("humidity").Attributes["data"].InnerText); nodeList = root.SelectNodes("descendant::weather/forecast_conditions"); foreach (XmlNode nod in nodeList) { str.Append("</td><td align='center'>" + nod.SelectSingleNode("day_of_week").Attributes["data"].InnerText + "<br>"); str.Append("<img src='http://www.google.com" + nod.SelectSingleNode("icon").Attributes["data"].InnerText + "' alt='" + nod.SelectSingleNode("condition").Attributes["data"].InnerText + "'><br>"); str.Append(nod.SelectSingleNode("low").Attributes["data"].InnerText + "°C | "); str.Append(nod.SelectSingleNode("high").Attributes["data"].InnerText + "°C"); } str.Append("</td></tr></tbody></table>"); } catch (System.Exception ex) { str.Append(ex.Message); } finally { stream.Close(); } return str; }
Notepad na mão e uma idéia na cabeça......Se foi útil, marca lá.... Me ajuda que eu marco também... heheheh- Marcado como Resposta Glauber Rocha sexta-feira, 13 de julho de 2012 13:35