All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
。net中前台javascript与后台c#相互调用
ASP.NET,MVC+LINQ开发一个图书销售站点(1)-需求分析_[Asp.Net教程]
关于在php.ini中添加extension=php_mysqli.dll指令的说明_php资料_编程技术
用SQL语句删除重复记录的四种方法_[SQL Server教程]
APMServ图文教程_[PHP教程]
在gridview中使用DataFromatString的小技巧_[Asp.Net教程]
Asp.net生成静态网页的实现代码_.net资料_编程技术
vs2005视频教程之自定义服务器控件(下)[视频]_[Asp.Net教程]
Asp.net,文件上传基类(取得文件后缀名,保存文件,加入文字水印)_[Asp.Net教程]
ASP.NET中利用DataGrid实现高效分页_[Asp.Net教程]
Reading number is top 10 pictures
Look for from human art net, is good1
Sora aoi possession of boudoir2
The goddess of the single reason1
Take you to walk into the most true north Korea rural1
A man's favorite things15
谁认识这位校花
奇趣的世界记录3
西游四格漫画(五)
小学生考试又现神作--还有外国的
The Soviet union swimsuit exposure in the 70 year2
Download software ranking
Love the forty days
尖东毒玫瑰A
Sora aoi - one of more PK
Boxer vs Yellow1
Sora aoi, the maid, students' uniforms
Tram sex maniac 2 (H) rar bag11
Boxer vs Yellow2
Tram sex maniac 2 (H) rar bag16
Unix video tutorial13
尖东毒玫瑰B
delv published in(发表于) 2014/1/16 9:34:02 Edit(编辑)
根据IP获取当地天气预报的实现_[Asp.Net教程]

根据IP获取当地天气预报的实现_[Asp.Net教程]

根据IP获取当地天气预报的实现_[Asp.Net教程]























访问www.163.com,首页的栏目里有当地的天气预报。可以猜想,这里的天气预报,应该是根据来访者的ip判断其所在地给出当地的天气情况。问了一些朋友,也证实了这一点。项目里也需要天气预报这个小栏目,同事做过一个(从其他站点抓取的),不过实现不了根据IP显示当地的天气情况,需要用户自行选择,而且抓取的站点属于小站….其可靠性值得怀疑。。所以就萌生了抓取网易的天气预报的想法。。。对页面进行分析。。发现显示天气预报的区域是一个IFrame,IFrame里嵌入了如下链接http://news.163.com/util/position1.html, 对这个地址访问直接跳转到另外一个链接http://news.163.com/weather/news/qx1/56294.html,此链接显示了天气情况,如图:





  由此可以推测http://news.163.com/util/position1.html,是在根据来访者的IP判断所属区域,然后返回一个该地区所对应的区位码,如: 56294代表成都。如何让网易来帮我们的站点来访者判断所属区域,并给出天气情况,并显示在自己的站点页面上呢?还得继续分析。。因为http://news.163.com/util/position1.html,此链接一访问就转向到天气情况的链接,而无法查看源码。便猜想。。此页面肯定有些东西。。无奈之下。。WebRequest一下,出现了如下代码:






















以下是引用片段:
1
50
56
57








  上面的这段js实现了对来访者IP判断并给出了天气预报结果的链接。Js里的此链接: http://202.108.39.152/ipquery,起到的是判断用户所在地的作用,返回的是来访者所在地省份。分析到此,想要的结果差不多就出来了…




  在客户端调用这段js获得天气预报结果的链接地址,然后交给服务端来处理。(为什么要交给后台处理,而不是直接显示呢?)因为直接得出的链接页面上,有多余的链接,还应用了样式(如图一),不便为自己所用,所以得处理掉。客户端调用服务端的方法很多,最初使用了Ajax框架Anthem,实现了过后,觉得有点杀鸡用牛刀的感觉。。无聊之余。。就又用CallBack实现了一次。。感觉恰到好处。。后来又发现。。__doPostBack也可以实现客户端调用服务端方法。。看来实现这么一个功能还真是简单。。。




  好了到此就实现了,自己想要的结果:(感觉有点遗憾的是只给出了省会城市的天气预报)







  前台页面代码Defaul.aspx:


























以下是引用片段:
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" ResponseEncoding="GB2312" %>
2
3
4
5
6
98
99
100
101
102
103
104
105
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 /**////


29 /// 回调,解析客户端的参数
30 ///

31 ///
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 /**////
45 /// Anthem方式,解析获取的url的页面内容
46 ///

47 /// url
48 /// 解析结果
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 //
63 //回调方式,解析获取的url的页面内容
64 //

65 //
66 //
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











来源:天极













































添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.