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

Reading number is top 10 articles
.Net+MySQL组合开发(二),数据访问篇_[Asp.Net教程]
C#网络应用编程基础练习题与答案(十)_[Asp.Net教程]
PHP5,OOP编程中的代理与异常定制_php资料_编程技术
讲述c#中的类型转换_[Asp.Net教程]
Asp.net日期字符串格式化显示方法_.net资料_编程技术
动态网页技术PHP关于cookie和session的分析_php资料_编程技术
小结:PHP动态网页程序两个有用的小技巧_php资料_编程技术
网页设计必学:快速精通网页框架FRAME_[Html教程]
在ASP.NET2.0中的WEB.CONFIG文件中调用外部文件_[Asp.Net教程]
用标准的SQL语句实现查询记录分页_[SQL,Server教程]
Reading number is top 10 pictures
2012 national geographic daily picture5
Angie Chiu vijara myth1
Ashlynn Brooke a group sexy photo2
So beauty, will let you spray blood4
PengLiYuan lady for the world health organization fight TB and AIDS goodwill ambassador
2012 national geographic daily picture8
29 the belle stars after bath figure4
Plump, too plump!1
西游四格漫画(六)
再来随便发几张
Download software ranking
linux高级编程
WebService在.NET中的实战应用教学视频 → 第4集
致我们终将逝去的青春
Ashlynn Video4
少妇苏霞全本
C#COM编程指南
Professional killers2 for Android
jdk1.5
Sora aoi's film--cangkong_Blue.Sky
虚拟机5.5.3版
归海一刀 published in(发表于) 2014/1/30 0:59:43 Edit(编辑)
Asp.Net实例:根据IP获取当地天气预报的实现_[Asp.Net教程]

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

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,此链接显示了天气情况,如图:




1.JPG




  由此可以推测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也可以实现客户端调用服务端方法。。看来实现这么一个功能还真是简单。。。




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




2.JPG




  前台页面代码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.