WebService获取Google天气信息
2013-04-24 11:56
218 查看
GetWeather.asmx 代码
再别的页面实例化对象后,调用即可。
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Text; using System.Configuration; using System.Net; using System.IO; using System.Xml; namespace TongLu.Weather { /// <summary> /// Summary description for GetWeather /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [ToolboxItem(false)] public class GetWeather : System.Web.Services.WebService { [WebMethod] public string GetWeathers(int lng, int lat) //传入的坐标: 例如: 30670000,104019996 { string BaseURI = http://www.google.com.hk/ig/api?hl=zh-cn string weather = ",,," + lng + "," + lat; string RequestUrl = BaseURI + "&weather=" + weather; string ListXml = SendRequestBackXml(RequestUrl, ""); string s = WeatherExecue(ListXml); return s; } private string SendRequestBackXml(string Uri, string ContentStr) { try { HttpWebRequest MyRequest = (HttpWebRequest)WebRequest.Create(Uri); MyRequest.Timeout = 30000; MyRequest.KeepAlive = true; MyRequest.ContentType = "application/x-www-form-urlencoded"; MyRequest.Method = WebRequestMethods.Http.Post; byte[] RequestBytes = Encoding.ASCII.GetBytes(ContentStr); Stream swt = MyRequest.GetRequestStream(); swt.Write(RequestBytes, 0, RequestBytes.Length); swt.Flush(); swt.Close(); HttpWebResponse MyResponse = (HttpWebResponse)MyRequest.GetResponse(); StreamReader srd = new StreamReader(MyResponse.GetResponseStream(), Encoding.GetEncoding("GB2312")); string BackContent = srd.ReadToEnd(); srd.Close(); MyResponse.Close(); return BackContent; } catch (Exception e) { return string.Empty; } } /// <summary> /// 获取天气信息 /// </summary> /// <param name="cities"></param> /// <returns></returns> private string WeatherExecue(string cities) { StringBuilder SB = new StringBuilder(); if (string.IsNullOrEmpty(cities)) { return null; } XmlDocument doc = new XmlDocument(); doc.LoadXml(cities); XmlNode root = doc.DocumentElement; if (root.SelectSingleNode("descendant::weather").FirstChild.Name == "problem_cause") { return null; } XmlNodeList NodeList = root.SelectNodes("weather/forecast_conditions"); string day = NodeList.Item(0).SelectSingleNode("day_of_week").Attributes["data"].InnerText; string low = NodeList.Item(0).SelectSingleNode("low").Attributes["data"].InnerText; string high = NodeList.Item(0).SelectSingleNode("high").Attributes["data"].InnerText; string picPath = "http://www.google.com" + NodeList.Item(0).SelectSingleNode("icon").Attributes["data"].InnerText;//获取远程图片地址 string condition= NodeList.Item(0).SelectSingleNode("condition").Attributes["data"].InnerText; SB.Append("<img width='30'heigh='20' "); SB.Append("src='"); SB.Append(picPath); SB.Append("'/>"); SB.Append(low); SB.Append("℃"); SB.Append("/"); SB.Append(high); SB.Append("℃"); SB.Append("<br/>"); SB.Append(day); SB.Append(" 天气:"); SB.Append(condition); return SB.ToString(); } } }
再别的页面实例化对象后,调用即可。
相关文章推荐
- TASKCTL使用插件获取WebService天气信息
- Java通过webservice接口获取天气信息
- Android模拟器访问google网站获取天气信息时,出现 java.net.UnknownHostException: www.google.com 错误
- android通过google api获取天气信息示例
- Java通过webservice接口获取天气信息
- (PHP)用cURL调用WebService获取天气信息
- java调用webservice天气预报(SOAP请求的方式获取天气信息并解析返回的XML)
- android-基于Webservice实现天气信息获取及源码下载(一)
- Kettle通过Webservice获取天气信息
- android-基于Webservice实现天气信息获取及源码下载(一)
- java调用webservice天气预报(SOAP请求的方式获取天气信息并解析返回的XML)
- android-基于Webservice实现天气信息获取及源码下载(二)
- 获取城市天气信息的简单例子-----WebService
- android-基于Webservice实现天气信息获取及源码下载(三)
- webservice--获取天气信息
- 使用WebService获取天气信息接口信息
- android-基于Webservice实现天气信息获取及源码下载(二)
- java调用webservice天气预报(SOAP请求的方式获取天气信息并解析返回的XML)
- Kettle通过Webservice获取天气信息
- android-基于Webservice实现天气信息获取及源码下载(三)