Asp. NET: The realization of local weather forecast based on IP

Source: Internet
Author: User
Tags setcookie client
Asp.net| Weather Forecast

Visit www.163.com, the first page of the column has the local weather forecast. It can be guessed that the weather forecast here should be based on the caller's IP to determine the location of the local weather conditions. Asked some friends, also confirmed this point. The project also needs weather forecast this small column, the colleague has done one (from other sites to crawl), but cannot realize according to the IP display local weather situation, needs the user to choose, moreover crawls the site belongs to the station .... Its reliability is questionable. So the idea of crawling the weather forecast of NetEase ... Analyze the page ... The area that shows the weather forecast is a iframe,iframe embedded in the following link http://news.163.com/util/position1.html, which directly jumps to another link for this address http:// news.163.com/weather/news/qx1/56294.html, this link shows the weather conditions, as shown in figure:

It can be inferred from this that http://news.163.com/util/position1.html is based on the caller's IP to determine the area, and then return to a region corresponding to the location code, such as: 56294 for Chengdu. How to let NetEase to help our site visitors to judge the region, and give weather conditions, and display on their own site page? We have to continue with the analysis. Because http://news.163.com/util/position1.html, this link to a visit to the weather link, and can not view the source. Then guess. There must be something on this page. Helpless under. WebRequest, the following code appears:

1<script language= "Javascript" >
2var City = new Array ("Anhui", "Heilongjiang", "Shandong", "Beijing", "Hubei", "Shanxi", "Fujian", "Hunan", "Shaanxi", "Gansu", "Jilin", "Shanghai", "Guangdong", "Jiangsu", "Sichuan", "Guangxi", "Jiangxi", "Tianjin", " Guizhou "," "Liaoning", "Tibet", "Hainan", "Inner Mongolia", "Xinjiang", "Hebei", "Ningxia", "Yunnan", "Henan", "Qinghai", "Zhejiang", "Chongqing");
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 ');
4
5function Getcookieval (offset) {
6 var endstr = Document.cookie.indexOf (";", offset);
7 if (endstr = = 1)
8 endstr = document.cookie.length;
9 Return unescape (document.cookie.substring (offset, endstr));
10}
11function GetCookie (name) {
var arg = name + "=";
var alen = Arg.length;
var clen = document.cookie.length;
var i = 0;
while (I < Clen) {
var j = i + Alen;
if (Document.cookie.substring (i, j) = arg)
Return Getcookieval (j);
i = Document.cookie.indexOf ("", I) + 1;
if (i = = 0)
break;
23}
Return "";
25}
26function Setcookie (cookiename,cookievalue,ndays) {
var today = new Date ();
var expire = new Date ();
if (Ndays==null | | ndays==0) Ndays=1;
Expire.settime (Today.gettime () + 3600000*24*ndays);
Document.cookie = cookiename+ "=" +escape (cookievalue) + ";p ath=/;d omain=.163.com;expires=" +expire.togmtstring ();
32}
33function Getcityweatherid (cityname) {
for (i=0;i<city.length;i++) {
if (city[i]==cityname) {
return weaths[i];
37}
38}
"54511";
40}
41
42var ntes_weatheraddr = GetCookie ("ntes_weatheraddr");
43IF (! NTES_WEATHERADDR) {
The var loc = GetCookie ("theaddr");
if (!loc) {
document.write ("<script type= ' text/javascript ' src= ' http://202.108.39.152/ipquery ' ><" + "/script>");
47}
48}
49</script>
50<script>
51if (! NTES_WEATHERADDR) {
Ntes_weatheraddr=getcityweatherid (Loc);
53}
54window.location.href= "http://news.163.com/weather/news/qx1/" +ntes_weatheraddr+ ". html";
55</script>
56
57

The above JS realizes a link to the caller IP judgment and gives the weather forecast results. JS in this link: http://202.108.39.152/ipquery, play is to judge the role of the user location, return is the location of the province of visitors. Analysis to this, the desired results are almost out ...

The client calls this JS to obtain the result of the Weather Report link address, and then to the server to deal with. (Why should I give it to the background instead of showing it directly?) Because of the direct link page, there are redundant links, but also applied the style (such as figure I), inconvenient for their own use, so have to deal with off. The client invokes the service side many methods, originally used the AJAX framework anthem, realized after, felt a bit overkill feeling. Boring. I realized it again with callback. Feel just right. And then it turns out that. __doPostBack can also implement client invoke service-side methods. It seems that the realization of such a function is really simple ...

All right, here's the result: (It feels a bit regrettable that only the weather forecast for the capital city is given).

Front page Code defaul.aspx:

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 ">
345<title></title>
6<script>
7var City = new Array ("Anhui", "Heilongjiang", "Shandong", "Beijing", "Hubei", "Shanxi", "Fujian", "Hunan", "Shaanxi", "Gansu", "Jilin", "Shanghai", "Guangdong", "Jiangsu", "Sichuan", "Guangxi", "Jiangxi", "Tianjin", " Guizhou "," "Liaoning", "Tibet", "Hainan", "Inner Mongolia", "Xinjiang", "Hebei", "Ningxia", "Yunnan", "Henan", "Qinghai", "Zhejiang", "Chongqing");
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) {
var loc = GetCookie ("theaddr");
if (!loc) {
document.write ("<script type= ' text/javascript ' src= ' http://202.108.39.152/ipquery ' ><" + "/script>");
15}
16}
17
18function Getcookieval (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr = = 1)
Endstr = Document.cookie.length;
Return unescape (document.cookie.substring (offset, endstr));
23}
24
25function GetCookie (name) {
var arg = name + "=";
var alen = Arg.length;
var clen = document.cookie.length;
var i = 0;
while (I < Clen) {
to var j = i + Alen;
if (Document.cookie.substring (i, j) = arg)
Getcookieval (j);
i = Document.cookie.indexOf ("", I) + 1;
if (i = = 0)
The break;
37}
Return "";
39}
40
41function Setcookie (cookiename,cookievalue,ndays) {
var today = new Date ();
The var expire = new Date ();
if (Ndays==null | | ndays==0) Ndays=1;
Expire.settime (Today.gettime () + 3600000*24*ndays);
Document.cookie = cookiename+ "=" +escape (cookievalue) + ";p ath=/;d omain=.163.com;expires=" +expire.togmtstring ();
47}
48
49//gets the corresponding number based on the province name returned by the IP server
50function Getcityweatherid (cityname) {
A for (i=0;i<city.length;i++) {
if (city[i]==cityname) {
Weaths[i];
54}
55}
return "57816";
57}
58
59//get a link to the results of the location forecast
60function Getweatherurl () {
61if (! NTES_WEATHERADDR) {
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
The 69//client invokes the server-side method to analyze the content of the page which is linked to the result of weather forecast, and Anthem Realization Way
70function Showweatherbyanthem () {
Anthem_invokepagemethod ("Showweatherbyanthem", [], Getserverresult);
72}
73
74function Getserverresult (Result) {
document.getElementById ("Result"). InnerHTML = Result.value;
76}
77
The 78//client invokes the server-side method to analyze the content of the page which is linked to the result of weather forecast, and _dopostback realization Way
79function Showweatherbylink ()
80{
Bayi __dopostback (' LinkButton1 ', ');
82}
83
The 84//client invokes the server-side method to analyze the content of the page which is linked to the result of weather forecast, and callback realization Way
85function Showweatherbycallback ()
86{
var Context=document.getelementbyid ("result");
The Var Weatherurl=document.getelementbyid ("Text1");
arg= var "showweatherbycall|" + weatherurl.value;
<%= Clientscript.getcallbackeventreference (This, "Arg", "Outputresult", "context")%>;
91}
92function Outputresult (Result)
93{
document.getElementById ("Result"). InnerHTML = result;
95
96}
97</script>
9899<body >
<form id= "Form1" runat= "Server" >
<span id= "Result" ></span>
102 <input id= "Text1" type= "hidden" runat= "Server"/>
</form>
104</body>
105106

Background Code 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{
protected void Page_Load (object sender, EventArgs e)
15 {
Anthem.Manager.Register (this);
17
18}
19
Fixed format for 20-callback fixed format #region callbacks
public string str_content;
22
public void RaiseCallbackEvent (string the_string)
24 {
Str_content = the_string;
26}
27
/**////<summary>
29///Callback, parsing client's parameters
///</summary>
///<returns></returns>
public string GetCallbackResult ()
33 {
34
string[] Parts = str_content. Split (' | ');
object[] thearglist = new Object[parts. LENGTH-1];
Panax for (int int_index = 1; int_index < parts. Length; int_index++)
THEARGLIST[INT_INDEX-1] = Parts[int_index];
Return (String) GetType (). GetMethod (Parts[0]). Invoke (this, thearglist);
40}
#endregion
42
43 method body that parses the content of a URL's page #region the method body that resolves the page content of the URL
/**////<summary>
///Anthem Way to parse the page content of the URL obtained
///</summary>
///<param name= "url" >url</param>
///<returns> Analytical Results </returns>
[Anthem.method]
public string Showweatherbyanthem ()
51 {
52
WebRequest request = WebRequest.Create (Text1.value);
Request. Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse) request. GetResponse ();
The Stream DataStream = response. GetResponseStream ();
StreamReader reader = new StreamReader (DataStream, System.Text.Encoding.Default);
The string str = reader. ReadToEnd ();
Return to Str. Substring (220);
60
61}
//<summary>
63//Callback mode, parse the URL to get the content of the page
//</summary>
//<param name= "url" ></param>
//<returns></returns>
public string showweatherbycall (string url)
68 {
WebRequest request = webrequest.create (URL);
Request. Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse) request. GetResponse ();
DataStream = Response. GetResponseStream ();
StreamReader reader = new StreamReader (DataStream, System.Text.Encoding.Default);
The string str = reader. ReadToEnd ();
Return str. Substring (220);
76
77}
#endregion
79}
80

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.