zoukankan      html  css  js  c++  java
  • 获取天气预报

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Net;
    using System.IO;

    public class WeatherHelper
    {
        public WeatherHelper()
        { }

        /// <summary>
        /// 获取今天气温,远程捕获
        /// </summary>
        public static string GetWeatherToday(string CityCode)
        {
            string strUrl = "http://weather.china.com.cn/city/"+CityCode+"_pic2.html".Trim();
            string WeatherToday = "";
            if (UrlExistsUsingSockets(strUrl))
            {
                WebRequest wreq = WebRequest.Create(strUrl);
                WebResponse wresp = (WebResponse)wreq.GetResponse();
                Stream s = wresp.GetResponseStream();
                StreamReader sr = new StreamReader(s, System.Text.Encoding.GetEncoding("utf-8"));
                string HTML = sr.ReadToEnd();
                int laststr = HTML.LastIndexOf("℃");
                if (laststr > 0)
                {
                    string Newhtml = HTML.Substring(0, laststr + 1);
                    int startstr = Newhtml.LastIndexOf(">");
                    int mylen = laststr - startstr;
                    WeatherToday = Newhtml.Substring(startstr + 1, mylen);
                }
                else
                {
                    WeatherToday = "天气预报解析不成功!";
                }
            }
            else
            {
                WeatherToday = "获取天气预报失败!";
            }
            return WeatherToday;
        }

        /// <summary>
        /// 方法一、检测远程url是否存在
        /// </summary>
        private static bool UrlExistsUsingHttpWebRequest(string url)
        {
            try
            {
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
                myRequest.Method = "HEAD";
                myRequest.Timeout = 100;
                HttpWebResponse res = (HttpWebResponse)myRequest.GetResponse();
                return (res.StatusCode == HttpStatusCode.OK);
            }
            catch (System.Net.WebException we)
            {
                System.Diagnostics.Trace.Write(we.Message);
                return false;
            }
        }

        /// <summary>
        /// 方法二、检测远程url是否存在
        /// </summary>
        private static bool UrlExistsUsingSockets(string url)
        {
            if (url.StartsWith("http://")) url = url.Remove(0, "http://".Length);
            try
            {
                IPHostEntry ipHost = Dns.GetHostEntry(url); //GetHostEntry
                return true;
            }
            catch (Sockets.SocketException se)
            {
                System.Diagnostics.Trace.Write(se.Message);
                return false;
            }
        }
    }

  • 相关阅读:
    html页面转成jsp页面之后样式变化的问题解决方法
    servlet数据库验证登录
    《基于 JSP 技术的试题库系统的设计与实现》10
    《通用试题库管理系统的设计与实现》9
    《基于多目标粒子群算法的智能组卷研究》8
    《基于改进随机抽取算法的信息论题库和智能组卷系统的设计与实现》7
    《高校试题库管理系统的设计与分析》6
    《试题库管理系统的设计与开发》
    《基于 B/S 模式的试题库管理系统的设计与实现 》笔记
    《通用试题库管理系统的设计与开发》笔记
  • 原文地址:https://www.cnblogs.com/Elgin/p/2238194.html
Copyright © 2011-2022 走看看