获取天气情况

 

http://service.weather.com.cn/plugin/index.shtml

 

<iframe src="http://m.weather.com.cn/m/pn12/weather.htm " width="245" height="110"
  marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no">
  </iframe>

 

 

 前台页面代码Defaul.aspx:

 

 

 

  后台代码Default.cs:

 

1using System; 
2using System.Data; 
3using System.Configuration; 
4using System.Web; 
5using System.Web.Security; 
6using System.Web.UI; 
7using System.Web.UI.WebControls; 
8using System.IO; 
9using System.Net; 
10using Anthem; 
11 
12public partial class _Default : System.Web.UI.Page, ICallbackEventHandler 
13{ 
14     protected void Page_Load(object sender, EventArgs e) 
15     { 
16         Anthem.Manager.Register(this); 
17 
18     } 
19 
20     回调的固定格式#region 回调的固定格式 
21     public string str_content; 
22    
23     public void RaiseCallbackEvent(string the_string) 
24     { 
25         str_content = the_string; 
26     } 
27 
28     /**//// <summary> 
29     /// 回调,解析客户端的参数 
30     /// </summary> 
31     /// <returns></returns> 
32     public string GetCallbackResult() 
33     { 
34         
35         string[] parts = str_content.Split('|'); 
36         object[] theArgList = new object[parts.Length - 1]; 
37         for (int int_index = 1; int_index < parts.Length; int_index++) 
38             theArgList[int_index - 1] = parts[int_index]; 
39         return (string)GetType().GetMethod(parts[0]).Invoke(this, theArgList); 
40     } 
41     #endregion 
42 
43     解析url的页面内容的方法体#region 解析url的页面内容的方法体 
44     /**//// <summary> 
45     /// Anthem方式,解析获取的url的页面内容 
46     /// </summary> 
47     /// <param name="url">url</param> 
48     /// <returns>解析结果</returns> 
49     [Anthem.Method] 
50     public string ShowWeatherByAnthem() 
51     { 
52 
53         WebRequest request = WebRequest.Create(Text1.Value); 
54         request.Credentials = CredentialCache.DefaultCredentials; 
55         HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
56         Stream dataStream = response.GetResponseStream(); 
57         StreamReader reader = new StreamReader(dataStream, System.Text.Encoding.Default); 
58         string str = reader.ReadToEnd(); 
59         return str.Substring(220); 
60 
61     } 
62      //<summary> 
63      //回调方式,解析获取的url的页面内容 
64      //</summary> 
65      //<param name="url"></param> 
66      //<returns></returns> 
67     public string ShowWeatherByCall(string url) 
68     { 
69         WebRequest request = WebRequest.Create(url); 
70         request.Credentials = CredentialCache.DefaultCredentials; 
71         HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
72         Stream dataStream = response.GetResponseStream(); 
73         StreamReader reader = new StreamReader(dataStream, System.Text.Encoding.Default); 
74         string str = reader.ReadToEnd(); 
75         return str.Substring(220); 
76 
77     } 
78     #endregion 
79} 
80

 

   1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"   ResponseEncoding="GB2312" %> 
   2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
   3<html xmlns="http://www.w3.org/1999/xhtml"> 
   4<head runat="server"> 
   5<title></title> 
   6<script> 
   7var city = new Array("安徽","黑龙江","山东","北京","湖北","山西","福建","湖南","陕西","甘肃","吉林","上海","广东","江苏","四川","广西","江西","天津","贵州","辽宁","西藏","海南","内蒙古","新疆","河北","宁夏","云南","河南","青海","浙江","重庆"); 
   8var weaths = new Array('58321','50953','54823','54511','57494','53772','59134','57679','57036','52889','54172','58367','59287','58238','56294','59431','58606','54527','57816','54342','55591','52856','53463','51463','53698','53614','56778','57083','52866','58457','57516'); 
   9 
10var NTES_WeatherAddr = GetCookie("NTES_WeatherAddr"); 
11if (!NTES_WeatherAddr){ 
12     var loc = GetCookie("theaddr"); 
13     if(!loc){ 
14         document.write("<script type='text/javascript' src='http://202.108.39.152/ipquery'><" + "/script>"); 
15     } 
16}  
17 
18function getCookieVal (offset) { 
19   var endstr = document.cookie.indexOf (";", offset); 
20   if (endstr == -1) 
21   endstr = document.cookie.length; 
22   return unescape(document.cookie.substring(offset, endstr)); 
23} 
24 
25function GetCookie (name) { 
26   var arg = name + "="; 
27   var alen = arg.length; 
28   var clen = document.cookie.length; 
29   var i = 0; 
30   while (i < clen) { 
31     var j = i + alen; 
32     if (document.cookie.substring(i, j) == arg) 
33       return getCookieVal (j); 
34     i = document.cookie.indexOf(" ", i) + 1; 
35     if (i == 0) 
36        break; 
37   } 
38   return ""; 
39} 
40 
41function SetCookie(cookieName,cookieValue,nDays) { 
42 var today = new Date(); 
43 var expire = new Date(); 
44 if (nDays==null || nDays==0) nDays=1; 
45 expire.setTime(today.getTime() + 3600000*24*nDays); 
46 document.cookie = cookieName+"="+escape(cookieValue) + ";path=/;domain=.163.com;expires="+expire.toGMTString(); 
47} 
48 
49//根据Ip服务器返回的省份名称获取对应的编号 
50function getCityWeatherID(cityname){ 
51     for(i=0;i<city.length;i++){ 
52     if(city[i]==cityname){ 
53         return weaths[i]; 
54         } 
55     } 
56     return "57816"; 
57} 
58 
59//获取所在地天气预报结果的链接 
60function getWeatherUrl(){ 
61if (!NTES_WeatherAddr){ 
62     NTES_WeatherAddr=getCityWeatherID(loc); 
63     
64} 
65var addr="http://news.163.com/weather/news/qx1/"+NTES_WeatherAddr+".html"; 
66document.form1.Text1.value=addr; 
67} 
68 
69//客户端调用服务端方法实现对天气预报结果链接的页面内容进行解析,Anthem实现方式 
70function showWeatherByAnthem() { 
71     Anthem_InvokePageMethod("ShowWeatherByAnthem", [], getServerResult); 
72} 
73 
74function getServerResult(result) { 
75     document.getElementById("result").innerHTML = result.value; 
76} 
77 
78//客户端调用服务端方法实现对天气预报结果链接的页面内容进行解析,_doPostBack实现方式 
79function showWeatherBylink() 
80{ 
81     __doPostBack('LinkButton1',''); 
82} 
83 
84//客户端调用服务端方法实现对天气预报结果链接的页面内容进行解析,CallBack实现方式 
85function showWeatherByCallBack() 
86{ 
87     var context=document.getElementById("result"); 
88     var weatherUrl=document.getElementById("Text1"); 
89     var arg="ShowWeatherByCall|" + weatherUrl.value; 
90     <%= ClientScript.GetCallbackEventReference(this,"arg","outPutResult","context")%>; 
91} 
92function outPutResult(result) 
93{      
94    document.getElementById("result").innerHTML = result; 
95         
96} 
97</script> 
98</head> 
99<body onload="getWeatherUrl(),showWeatherByCallBack()"> 
100     <form id="form1" runat="server"> 
101         <span id="result"></span> 
102         <input id="Text1" type="hidden" runat="server" /> 
103     </form> 
104</body> 
105</html> 
106

 

 

转自其他网站

posted @ 2013-11-23 23:13  乐思草  阅读(269)  评论(0编辑  收藏  举报