文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>google wether webservice

google wether webservice

时间:2011-03-17  来源:四渡冰点

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();
        }
    }
}
再别的页面实例化对象后,调用即可。

相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载