Use Asp.net to obtain local weather forecasts based on IP addresses

Source: Internet
Author: User

Use Asp.net to obtain local weather forecasts based on IP addresses

Visit www.163.com. The homepage contains the local weather forecast. We can guess that the weather forecast here should be based on the visitor's IP address to determine the local weather conditions. I asked some friends and confirmed this. The project also needs the weather forecast section. My colleague has done one (captured from other sites), but it cannot be used to display the local weather conditions based on the IP address. You need to make your own choice, and the captured site belongs to the small site .... Its reliability is questionable .. So I came up with the idea of capturing Netease's weather forecast... Analyze the page .. The area where the weather forecast is displayed is an IFRAME, And the IFRAME is embedded with the following link http://news.163.com/util/position1.html to direct access to this address to another link http://news.163.com/weather/news/qx1/56294.html, this link displays the weather condition,

Screen. width * 0.7) {This. resized = true; this. width = screen. width * 0.7; this. style. cursor = 'hand'; this. alt = 'click here to open new window \ nctrl + mouse wheel to zoom in/out';} "onclick =" If (! This. resized) {return true;} else {window. open ('HTTP: // dotnet.chinaitlab.com/uploadfiles_6597/200703/20070306104952833.jpg');} "alt =" "src =" http://dotnet.chinaitlab.com/UploadFiles_6597/200703/20070306104952833.jpg "onLoad =" If (this. width> screen. width * 0.7) {This. resized = true; this. width = screen. width * 0.7; this. alt = 'click here to open new window \ nctrl + mouse wheel to zoom in/out';} "border = 0>

From this we can speculate that limit 56294 represents Chengdu. How can we ask Netease to help our site visitors determine their regions, give weather conditions, and display them on their own site pages? You have to continue the analysis .. Because of http://news.163.com/util/position1.html, this chain is connected to the bad weather, and the source code is undisciplined. I guess .. There must be something on this page .. Helpless .. Webrequest:Code:

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 ('20170101', '20160301', '20160301', '20160301', '20160301', '20160301 ', '123', '123', '123', '123 ',
'123', '123', '123', '123', '123', '123', '123', '123', '123', '123', '123 ', '123', '123', '123', '123', '123 ',
'123', '123', '123', '123 ');
4
5 function 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}
11 function getcookie (name ){
12 var Arg = Name + "= ";
13 var Alen = Arg. length;
14 var clen = Document. Cookie. length;
15 var I = 0;
16 while (I <clen ){
17 var J = I + Alen;
18 if (document. Cookie. substring (I, j) = Arg)
19 Return getcookieval (j );
20 I = Document. Cookie. indexof ("", I) + 1;
21 if (I = 0)
22 break;
23}
24 return "";
25}
26 function setcookie (cookiename, cookievalue, ndays ){
27 var today = new date ();
28 var expire = new date ();
29 If (ndays = NULL | ndays = 0) ndays = 1;
30 expire. settime (today. gettime () + 3600000*24 * ndays );
31 document. Cookie = cookiename + "=" + escape (cookievalue) + "; Path =/; domain = .163.com; expires =" + expire. togmtstring ();
32}
33 function getcityweatherid (cityname ){
34 For (I = 0; I <city. length; I ++ ){
35 if (City= Cityname ){
36 return weaths;
37}
38}
39 return "54511 ";
40}
41
42var ntes_weatheraddr = getcookie ("ntes_weatheraddr");
43if (! Ntes_weatheraddr) {
44 var loc = getcookie ("theaddr");
45 if (! Loc) {
46 document. write ("
50
56
57

the preceding JavaScript code identifies the visitor's IP address and provides a link to the weather forecast result. In JS, this link is http: // 202.108.39.152/ipquery. It determines the location of the user and returns the province where the visitor is located. After the analysis, the desired results will almost come out...
call this JS link on the client to obtain the weather forecast result and submit it to the server for processing. (Why do I have to hand it over to the background for processing, rather than directly displaying it ?) Because there are unnecessary links on the directly obtained link page and style is also applied (1), it is inconvenient for you to use it, so you have to deal with it. There are many methods for the client to call the server. After the Ajax framework is initially used, the client feels a bit cool .. Bored .. It is implemented again with callback .. Feeling right .. Later I found out that .. _ Dopostback can also be used by the client to call the server method .. It seems that implementing such a function is really simple...
now, we can achieve this. I feel a little sorry for the expected results. Only the weather forecasts for provincial capitals are provided.)

Screen. width * 0.7) {This. resized = true; this. width = screen. width * 0.7; this. style. cursor = 'hand'; this. alt = 'click here to open new window \ nctrl + mouse wheel to zoom in/out';} "onclick =" If (! This. resized) {return true;} else {window. open ('HTTP: // dotnet.chinaitlab.com/uploadfiles_6597/200703/20070306104956670.jpg');} "alt =" "src =" http://dotnet.chinaitlab.com/UploadFiles_6597/200703/20070306104956670.jpg "onLoad =" If (this. width> screen. width * 0.7) {This. resized = true; this. width = screen. width * 0.7; this. alt = 'click here to open new window \ nctrl + mouse wheel to zoom in/out';} "border = 0>

Front-end Page code defaul. aspx:


1 <% @ page Language = "C #" autoeventwireup = "true" codefile = "efault. 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 5 <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 ('20170101', '20160301', '20160301', '20160301', '20160301', '20160301 ', '123', '123', '123', '123', '123', '123', '123 ',
'123', '123', '123', '123', '123', '123', '123', '123', '123', '123', '123 ', '123', '123 ',
'123', '123', '123', '123 ');
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
18 function 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
25 function 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
41 function 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 // obtain the corresponding number based on the province name returned by the IP server
50 function getcityweatherid (cityname ){
51 for (I = 0; I <city. length; I ++ ){
52 If (City= Cityname ){
53 return weaths;
54}
55}
56 return "57816 ";
57}
58
59 // link to obtain the weather forecast result
60 function 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 // The client calls the server method to parse the page content of the link to the weather forecast result.
70 function showweatherbyanthem (){
71 anthem_invokepagemethod ("showweatherbyanthem", [], getserverresult );
72}
73
74 function getserverresult (result ){
75 document. getelementbyid ("result"). innerhtml = result. value;
76}
77
78 // The client calls the server method to parse the page content of the link to the weather forecast result. _ dopostback Implementation Method
79 function showweatherbylink ()
80 {
81 _ dopostback ('linkbutton1 ','');
82}
83
84 // The client calls the server method to parse the page content of the link to the weather forecast result. The callback Implementation Method
85 function 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", "utputresult", "context") %>;
91}
92 function outputresult (result)
93 {
94 document. getelementbyid ("result"). innerhtml = result;
95
96}
97 </SCRIPT>
98 99 <body>
100 <Form ID = "form1" runat = "server">
101 <span id = "result"> </span>
102 <input id = "text1" type = "hidden" runat = "server"/>
103 </form>
104 </body>
105 106
Background code default. CS:
1 using system;
2 using system. Data;
3 using system. configuration;
4 using system. Web;
5 using system. Web. Security;
6 using system. Web. UI;
7 using system. Web. UI. webcontrols;
8 using system. IO;
9 using system. net;
10 using Anthem;
11
12 public 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 fixed callback format # fixed region callback format
21 Public String str_content;
22
23 public void raisecallbackevent (string the_string)
24 {
25 str_content = the_string;
26}
27
28/** // <summary>
29 // callback, parse the client Parameters
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 method body for parsing the page content of a URL # method body for Region to parse the page content of a URL
44/*** // <summary>
45 // parse the page content of the obtained URL in the anthem Mode
46 /// </Summary>
47 // <Param name = "url"> URL </param>
48 /// <returns> resolution result </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 // parse the page content of the obtained URL in callback Mode
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

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.