用asp.net实现根据IP获取当地天气预报 (3)
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
后台代码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