訪問www.163.com,首頁的欄目裡有當地的天氣預報。可以猜想,這裡的天氣預報,應該是根據來訪者的ip判斷其所在地給出當地的天氣情況。問了一些朋友,也證實了這一點。項目裡也需要天氣預報這個小欄目,同事做過一個(從其他站點抓取的),不過實現不了根據IP顯示當地的天氣情況,需要用戶自行選擇,而且抓取的站點屬於小站….其可靠性值得懷疑。。所以就萌生了抓取網易的天氣預報的想法。。。對頁面進行分析。。發現顯示天氣預報的區域是一個IFrame,IFrame裡嵌入了如下鏈接http://news.163.com/util/position1.html, 對這個地址訪問直接跳轉到另外一個鏈接http://news.163.com/weather/news/qx1/56294.Html,此鏈接顯示了天氣情況,如圖:
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592435.jpg)
由此可以推測http://news.163.com/util/position1.html,是在根據來訪者的IP判斷所屬區域,然後返回一個該地區所對應的區位碼,如: 56294代表成都。如何讓網易來幫我們的站點來訪者判斷所屬區域,並給出天氣情況,並顯示在自己的站點頁面上呢?還得繼續分析。。因為http://news.163.com/util/position1.Html,此鏈接一訪問就轉向到天氣情況的鏈接,而無法查看源碼。便猜想。。此頁面肯定有些東西。。無奈之下。。WebRequest一下,出現了如下代碼:
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
以下是引用片段:
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
1<script language="Javascript">
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
2var city = new Array("安徽","黑龍江","山東","北京","湖北","山西","福建","湖南","陝西","甘肅","吉林","上海","廣東","江蘇","四川","廣西","江西","天津","貴州","遼寧","西藏","海南","內蒙古","新疆","河北","寧夏","雲南","河南","青海","浙江","重慶");
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
3var 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'');
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
4
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592560.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592555.gif)
5function getCookIEVal (offset) ...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
6 var endstr = document.cookIE.indexOf (";", offset);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
7 if (endstr == -1)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
8 endstr = document.cookIE.length;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
9 return unescape(document.cookIE.substring(offset, endstr));
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592597.gif)
10}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592560.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592555.gif)
11function GetCookIE (name) ...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
12 var arg = name + "=";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
13 var alen = arg.length;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
14 var clen = document.cookIE.length;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
15 var i = 0;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592551.gif)
16 while (i < clen) ...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
17 var j = i + alen;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
18 if (document.cookIE.substring(i, j) == arg)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
19 return getCookIEVal (j);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
20 i = document.cookIE.indexOf(" ", i) + 1;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
21 if (i == 0)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
22 break;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592668.gif)
23 }
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
24 return "";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592597.gif)
25}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592560.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592555.gif)
26function SetCookie(cookieName,cookIEValue,nDays) ...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
27 var today = new Date();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
28 var expire = new Date();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
29 if (nDays==null || nDays==0) nDays=1;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
30 expire.setTime(today.getTime() + 3600000*24*nDays);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
31 document.cookie = cookieName+"="+escape(cookIEValue) + ";path=/;domain=.163.com;expires="+expire.toGMTString();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592597.gif)
32}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592560.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592555.gif)
33function getCityWeatherID(cityname)...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592551.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592609.gif)
34 for(i=0;i<city.length;i++)...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592551.gif)
35 if(city[i]==cityname)...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
36 return weaths[i];
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592668.gif)
37 }
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592668.gif)
38 }
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
39 return "54511";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592597.gif)
40}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
41
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
42var NTES_WeatherAddr = GetCookIE("NTES_WeatherAddr");
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592560.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592555.gif)
43if (!NTES_WeatherAddr)...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
44 var loc = GetCookIE("theaddr");
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592551.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592609.gif)
45 if(!loc)...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
46 document.write("<script type=''text/Javascript'' src=''http://202.108.39.152/ipquery''><" + "/script>");
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592668.gif)
47 }
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592597.gif)
48}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
49</script>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
50<script>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592560.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592555.gif)
51if (!NTES_WeatherAddr)...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
52 NTES_WeatherAddr=getCityWeatherID(loc);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592597.gif)
53}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
54window.location.href="http://news.163.com/weather/news/qx1/"+NTES_WeatherAddr+".Html";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
55</script>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
56
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
57
上面的這段js實現了對來訪者IP判斷並給出了天氣預報結果的鏈接。JS裡的此鏈接: http://202.108.39.152/ipquery,起到的是判斷用戶所在地的作用,返回的是來訪者所在地省份。分析到此,想要的結果差不多就出來了…
在客戶端調用這段JS獲得天氣預報結果的鏈接地址,然後交給服務端來處理。(為什麼要交給後台處理,而不是直接顯示呢?)因為直接得出的鏈接頁面上,有多余的鏈接,還應用了樣式(如圖一),不便為自己所用,所以得處理掉。客戶端調用服務端的方法很多,最初使用了AJax框架Anthem,實現了過後,覺得有點殺雞用牛刀的感覺。。無聊之余。。就又用CallBack實現了一次。。感覺恰到好處。。後來又發現。。__doPostBack也可以實現客戶端調用服務端方法。。看來實現這麼一個功能還真是簡單。。。
好了到此就實現了,自己想要的結果:(感覺有點遺憾的是只給出了省會城市的天氣預報)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592602.jpg)
前台頁面代碼Defaul.ASPx:
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
以下是引用片段:
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.ASPx.cs" Inherits="_Default" ResponseEncoding="GB2312" %>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xHtml1-transitional.dtd">
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
3<html XMLns="http://www.w3.org/1999/xHtml">
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
4<head runat="server">
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
5<title></title>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
6<script>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
7var city = new Array("安徽","黑龍江","山東","北京","湖北","山西","福建","湖南","陝西","甘肅","吉林","上海","廣東","江蘇","四川","廣西","江西","天津","貴州","遼寧","西藏","海南","內蒙古","新疆","河北","寧夏","雲南","河南","青海","浙江","重慶");
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
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'');
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
9
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
10var NTES_WeatherAddr = GetCookIE("NTES_WeatherAddr");
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592560.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592555.gif)
11if (!NTES_WeatherAddr)...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
12 var loc = GetCookIE("theaddr");
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592551.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592609.gif)
13 if(!loc)...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
14 document.write("<script type=''text/Javascript'' src=''http://202.108.39.152/ipquery''><" + "/script>");
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592668.gif)
15 }
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592597.gif)
16}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
17
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592560.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592555.gif)
18function getCookIEVal (offset) ...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
19 var endstr = document.cookIE.indexOf (";", offset);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
20 if (endstr == -1)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
21 endstr = document.cookIE.length;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
22 return unescape(document.cookIE.substring(offset, endstr));
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592597.gif)
23}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
24
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592560.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592555.gif)
25function GetCookIE (name) ...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
26 var arg = name + "=";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
27 var alen = arg.length;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
28 var clen = document.cookIE.length;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
29 var i = 0;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592551.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592609.gif)
30 while (i < clen) ...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
31 var j = i + alen;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
32 if (document.cookIE.substring(i, j) == arg)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
33 return getCookIEVal (j);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
34 i = document.cookIE.indexOf(" ", i) + 1;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
35 if (i == 0)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
36 break;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592668.gif)
37 }
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
38 return "";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592597.gif)
39}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
40
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592560.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592555.gif)
41function SetCookie(cookieName,cookIEValue,nDays) ...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
42 var today = new Date();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
43 var expire = new Date();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
44 if (nDays==null || nDays==0) nDays=1;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
45 expire.setTime(today.getTime() + 3600000*24*nDays);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
46 document.cookie = cookieName+"="+escape(cookIEValue) + ";path=/;domain=.163.com;expires="+expire.toGMTString();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592597.gif)
47}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
48
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
49//根據Ip服務器返回的省份名稱獲取對應的編號
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592560.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592555.gif)
50function getCityWeatherID(cityname)...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592551.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592609.gif)
51 for(i=0;i<city.length;i++)...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592551.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592609.gif)
52 if(city[i]==cityname)...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
53 return weaths[i];
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592668.gif)
54 }
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592668.gif)
55 }
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
56 return "57816";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592597.gif)
57}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
58
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
59//獲取所在地天氣預報結果的鏈接
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592560.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592555.gif)
60function getWeatherUrl()...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592551.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592609.gif)
61if (!NTES_WeatherAddr)...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
62 NTES_WeatherAddr=getCityWeatherID(loc);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
63
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592668.gif)
64}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
65var addr="http://news.163.com/weather/news/qx1/"+NTES_WeatherAddr+".Html";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
66document.form1.Text1.value=addr;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592597.gif)
67}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
68
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
69//客戶端調用服務端方法實現對天氣預報結果鏈接的頁面內容進行解析,Anthem實現方式
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592560.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592555.gif)
70function showWeatherByAnthem() ...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
71 Anthem_InvokePageMethod("ShowWeatherByAnthem", [], getServerResult);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592597.gif)
72}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
73
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592560.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592555.gif)
74function getServerResult(result) ...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
75 document.getElementById("result").innerHtml = result.value;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592597.gif)
76}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
77
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
78//客戶端調用服務端方法實現對天氣預報結果鏈接的頁面內容進行解析,_doPostBack實現方式
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
79function showWeatherBylink()
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592560.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592555.gif)
80...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
81 __doPostBack(''LinkButton1'','''');
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592597.gif)
82}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
83
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
84//客戶端調用服務端方法實現對天氣預報結果鏈接的頁面內容進行解析,CallBack實現方式
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
85function showWeatherByCallBack()
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592560.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592555.gif)
86...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
87 var context=document.getElementById("result");
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
88 var weatherUrl=document.getElementById("Text1");
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
89 var arg="ShowWeatherByCall|" + weatherUrl.value;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
90 <%= ClIEntScript.GetCallbackEventReference(this,"arg","outPutResult","context")%>;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592597.gif)
91}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
92function outPutResult(result)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592560.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592555.gif)
93...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
94 document.getElementById("result").innerHtml = result;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
95
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592597.gif)
96}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
97</script>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
98</head>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
99<body onload="getWeatherUrl(),showWeatherByCallBack()">
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
100 <form id="form1" runat="server">
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
101 <span id="result"></span>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
102 <input id="Text1" type="hidden" runat="server" />
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
103 </form>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
104</body>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
105</Html>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
106
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
後台代碼Default.cs:
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
以下是引用片段:
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
1using System;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
2using System.Data;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
3using System.Configuration;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
4using System.Web;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
5using System.Web.Security;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
6using System.Web.UI;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
7using System.Web.UI.WebControls;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
8using System.IO;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
9using System.Net;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
10using Anthem;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
11
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
12public partial class _Default : System.Web.UI.Page, ICallbackEventHandler
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592560.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592555.gif)
13...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
14 protected void Page_Load(object sender, EventArgs e)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592551.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592609.gif)
15 ...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
16 Anthem.Manager.Register(this);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
17
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592668.gif)
18 }
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
19
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592609.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592551.gif)
20 回調的固定格式回調的固定格式#region 回調的固定格式
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
21 public string str_content;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
22
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
23 public void RaiseCallbackEvent(string the_string)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592551.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592609.gif)
24 ...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
25 str_content = the_string;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592668.gif)
26 }
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
27
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592551.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592609.gif)
28 /**//**//**//// <summary>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592551.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592609.gif)
29 /**//// 回調,解析客戶端的參數
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592551.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592609.gif)
30 /**//// </summary>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592551.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592609.gif)
31 /**//// <returns></returns>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
32 public string GetCallbackResult()
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592551.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592609.gif)
33 ...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
34
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
35 string[] parts = str_content.Split(''|'');
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
36 object[] theArgList = new object[parts.Length - 1];
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
37 for (int int_index = 1; int_index < parts.Length; int_index++)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
38 theArgList[int_index - 1] = parts[int_index];
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
39 return (string)GetType().GetMethod(parts[0]).Invoke(this, theArgList);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592668.gif)
40 }
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592668.gif)
41 #endregion
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
42
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592609.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592551.gif)
43 解析url的頁面內容的方法體解析url的頁面內容的方法體#region 解析url的頁面內容的方法體
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592551.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592609.gif)
44 /**//**//**//// <summary>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592551.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592609.gif)
45 /**//// Anthem方式,解析獲取的url的頁面內容
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592551.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592609.gif)
46 /**//// </summary>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592551.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592609.gif)
47 /**//// <param name="url">url</param>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592551.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592609.gif)
48 /**//// <returns>解析結果</returns>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
49 [Anthem.Method]
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
50 public string ShowWeatherByAnthem()
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592551.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592609.gif)
51 ...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
52
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
53 WebRequest request = WebRequest.Create(Text1.Value);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
54 request.Credentials = CredentialCache.DefaultCredentials;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
55 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
56 Stream dataStream = response.GetResponseStream();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
57 StreamReader reader = new StreamReader(dataStream, System.Text.Encoding.Default);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
58 string str = reader.ReadToEnd();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
59 return str.Substring(220);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
60
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592668.gif)
61 }
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
62 //<summary>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
63 //回調方式,解析獲取的url的頁面內容
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
64 //</summary>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
65 //<param name="url"></param>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
66 //<returns></returns>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
67 public string ShowWeatherByCall(string url)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592551.gif)
68 ...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
69 WebRequest request = WebRequest.Create(url);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
70 request.Credentials = CredentialCache.DefaultCredentials;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
71 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
72 Stream dataStream = response.GetResponseStream();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
73 StreamReader reader = new StreamReader(dataStream, System.Text.Encoding.Default);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
74 string str = reader.ReadToEnd();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
75 return str.Substring(220);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592583.gif)
76
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592668.gif)
77 }
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592668.gif)
78 #endregion
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592597.gif)
79}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310592575.gif)
80