zoukankan      html  css  js  c++  java
  • 天气预报webservice代码

    <%@ WebService Language="c#" Class="WeatherWS.GetChinaWeather" %>
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Net;
    using System.Text;
    using System.IO;
    using System.Web;
    using System.Web.Services;

    namespace WeatherWS
    {
     /// <summary>
     /// GetChinaWeather 的摘要说明。
     /// </summary>
     [WebService(Namespace="http://flying.redv.com/monster")]
     public class GetChinaWeather : System.Web.Services.WebService
     {
      public GetChinaWeather()
      {
       //CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
       InitializeComponent();
      }

      #region 组件设计器生成的代码
      
      //Web 服务设计器所必需的
      private IContainer components = null;
        
      /// <summary>
      /// 设计器支持所需的方法 - 不要使用代码编辑器修改
      /// 此方法的内容。
      /// </summary>
      private void InitializeComponent()
      {
      }

      /// <summary>
      /// 清理所有正在使用的资源。
      /// </summary>
      protected override void Dispose( bool disposing )
      {
       if(disposing && components != null)
       {
        components.Dispose();
       }
       base.Dispose(disposing);  
      }
      
      #endregion

      [WebMethod(Description="中国各城市(县)天气预报获取服务,可接受一字符串参数(可选的查询方式:·国内城市(县)全名·字首拼音缩写·电话区号·邮政编码,如查询徐州的天气情况可输入'徐州'或'xz'作为参数)")]
      public DataSet getWeather(string strCity)
      {
       DataSet dsWeather = new DataSet();
       try
       {
        const int maxDay=5;
        string []time = new string[maxDay];//存储日期,从今天开始算起
        string []weather = new string[maxDay];//保存天气情况数据
        string []max = new string[maxDay];//保存最高温度数据
        string []min = new string[maxDay];//保存最低温度数据
        string []wind = new string[maxDay];//保存风向数据

        //发送一个post请求到index.jsp页面以获取城市数据
        Uri uri = new Uri("http://www.weathercn.com/forecastn/forcast/index.jsp?searchname="+System.Web.HttpUtility.UrlEncode(strCity,System.Text.Encoding.GetEncoding("GB2312")));
        WebRequest wreq=WebRequest.Create(uri);
      
        HttpWebResponse wresp=(HttpWebResponse)wreq.GetResponse();
      
        string HTML ="";

        Stream s=wresp.GetResponseStream();
      
        StreamReader objReader = new StreamReader(s,System.Text.Encoding.Default);
        HTML=objReader.ReadToEnd();
        if(HTML==null||HTML=="")
         return dsWeather;

        HTML = HTML.ToLower();//全部转换为小写
        if(HTML==null||HTML=="")
         return dsWeather;
        int head,tail,i;
        //查找城市数据 如果没有找到 则返回一个空的dataset
        head = HTML.IndexOf("查询结果:",0);
        head = HTML.IndexOf("station_name=",head);
        if(head==-1)
        {
         return dsWeather;
        }
        head = HTML.IndexOf("station_name=",head+1);
        tail = HTML.IndexOf("'",head);
        string strCityData = HTML.Substring(head,tail-head);//城市数据获取
        string href = "http://www.weathercn.com/forecastn/forcast/forecastDetail.jsp?"+strCityData;
        //根据城市数据去查询天气情况
        
        wreq=WebRequest.Create(href);
        wresp=(HttpWebResponse)wreq.GetResponse();
      
        HTML ="";
        s=wresp.GetResponseStream();
      
        objReader = new StreamReader(s,System.Text.Encoding.Default);
        HTML=objReader.ReadToEnd();
        if(HTML==null||HTML=="")
         return dsWeather;
        HTML = HTML.ToLower();//全部转换为小写
        
        DateTime dtNow = new DateTime();
        dtNow = DateTime.Today;//获取系统当前日期
        dtNow = dtNow.Subtract(TimeSpan.Parse("1"));
        for(i=0;i<maxDay;i++)
        {
         dtNow = dtNow.Add(TimeSpan.Parse("1"));
         time[i] = dtNow.ToShortDateString();//日期数据
        }
        
        //获取天气情况数据,总共maxDay天的数据
        String date = DateTime.Now.Year.ToString()+"年"+DateTime.Now.Month.ToString()+"月";//当前年月
        head = HTML.IndexOf(date,0);
        head = HTML.IndexOf("<tr>",head);
        for(i=0;i<maxDay;i++)
        {
         head = HTML.IndexOf("<td",head);
         head = HTML.IndexOf("<img",head);
         head = HTML.IndexOf("/",head);
         head = HTML.IndexOf("/",head+1);
         tail = HTML.IndexOf("_",head);
         weather[i] = HTML.Substring(head+1,tail-head-1);
         head = HTML.IndexOf("</td>",head);
        }

        //获取近maxDay天温度数据,包括最高温度和最低温度
        for(i=0;i<maxDay;i++)
        {
         head = HTML.IndexOf("max",head);
         head = HTML.IndexOf(">",head);
         tail = HTML.IndexOf("<",head);
         max[i] = HTML.Substring(head+1,tail-head-1);//最高温度

         head = HTML.IndexOf("min",head);
         head = HTML.IndexOf(">",head);
         tail = HTML.IndexOf("<",head);
         min[i] = HTML.Substring(head+1,tail-head-1);//最低温度
        }

        //最近maxDay天的风向数据
        head = HTML.IndexOf("<tr",head);
        for(i=0;i<maxDay;i++)
        {
         head = HTML.IndexOf("class",head);
         head = HTML.IndexOf(">",head);
         tail = HTML.IndexOf("<",head);
         wind[i] = HTML.Substring(head+1,tail-head-1);//风向数据
        }

        //将数据填充到DataSet中去
        DataTable dtWeather = new DataTable();
        dtWeather.Columns.Add("日期");
        dtWeather.Columns.Add("天气");
        dtWeather.Columns.Add("最高温度");
        dtWeather.Columns.Add("最低温度");
        dtWeather.Columns.Add("风力风向");
        for(i=0;i<maxDay;i++)
        {
         DataRow drWeather = dtWeather.NewRow();
         drWeather["日期"] = time[i];
         drWeather["天气"] = weather[i];
         drWeather["最高温度"] = max[i];
         drWeather["最低温度"] = min[i];
         drWeather["风力风向"] = wind[i];
         dtWeather.Rows.Add(drWeather);
        }
        dsWeather.Tables.Add(dtWeather);
        dsWeather.AcceptChanges();

        return dsWeather;

       }
       catch(Exception e)
       {
        //DO Something
        return dsWeather;
       }
      }
     }
    }


  • 相关阅读:
    Go语言new( )函数
    Go语言讲解深拷贝与浅拷贝
    (转)使用kubectl访问Kubernetes集群时的身份验证和授权
    执行kubectl命令时报错 error: You must be logged in to the server (Unauthorized)
    报错 cannot allocate memory 或者 no space left on device ,修复K8S内存泄露问题
    linux之apt-get命令(转)
    GSensor 碰撞检测方法与实现
    uboot流程(转)
    linux 环境搭建
    333开发记录
  • 原文地址:https://www.cnblogs.com/itelite/p/924276.html
Copyright © 2011-2022 走看看