繁体中文
设为首页
加入收藏
当前位置:.Net技术首页 >> Asp.Net开发 >> atlas学习实践系列之三(远程web services调用)

atlas学习实践系列之三(远程web services调用)

2007-09-15 08:00:00  作者:  来源:互联网  浏览次数:0  文字大小:【】【】【
简介:atlas调用本地web services那是相当的简单,调用远程的也不难,看了dflying chen的几篇文章也学了不少内容,尤其是yahoo天气预报的例子更是受益匪浅。自己动手实践一把。 目标:实现IP地址查询功能.输入IP地址,...

atlas调用本地web services那是相当的简单,调用远程的也不难,看了dflying chen的几篇文章也学了不少内容,尤其是yahoo天气预报的例子更是受益匪浅。自己动手实践一把。

目标:实现IP地址查询功能.输入IP地址,获得该IP所在的国家和城市。

难点:1)如何设置调用远程服务的asbx文件(有关配置支持asbx的方法可以见dflying chen 的介绍)

难点2):如何动态获得请求的客户端IP,然后在js中使用。

首先,建立一个网络服务,用于查询IP,IP地址信息库是一个mdb文件,相关代码为:

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--> 1using System;

2using System.Web;

3using System.Web.Services;

4using System.Web.Services.Protocols;

5using System.Data;

6using System.Data.OleDb;

7

8[WebService(Namespace = "http://tempuri.org/")]

9[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

10public class Service : System.Web.Services.WebService

11{

12 public Service () {

13

14 //如果使用设计的组件,请取消注释以下行

15 //InitializeComponent();

16 }

17 [WebMethod]

18 public string[] GetIPAddress(string ip)

19 {

20 //数据库文件物理路径

21 string dbFilePath = Server.MapPath("~/App_Data/IPaddress.mdb");

22 //数据库连接字符串

23 string conStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + dbFilePath;

24 string country = "未知";

25 string city = "未知";

26 //数据库连接

27 OleDbConnection con = new OleDbConnection(conStr);

28 try

29 {

30 long ipAddress = System.Net.IPAddress.Parse(ip).Address;

31 string cmdText = " select top 1 * from dv_address where ip1 <= " + ipAddress + " and ip2 >= " + ipAddress;

32 con.Open();

33 OleDbCommand cmd = new OleDbCommand(cmdText, con);

34 OleDbDataReader dr = cmd.ExecuteReader();

35 if (dr.Read())

36 {

37 country = dr["country"].ToString();

38 city = dr["city"].ToString();

39 }

40 }

41 catch

42 {

43 return new string[] { country, city };

44 }

45 finally

46 {

47 con.Close();

48 }

49 return new string[] { country, city };

50 }

51

52

53}

54

注意,该web services必须支持HTTP-Get方式访问,故需要如下web.config配置

网络服务建好了,下面就是调用了。

建立一个新的网站,首先建立一个IPServices.asbx文件,该文件是将远程服务映射到本地的配置文件,有关信息见dflying chen的文章。文件内容如下:

asbx配置文件

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--> 1

2

3

4

5

6

7

8

9

10

11

12

调用页面为:

页面前台

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--> 1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.ASPx.cs" Inherits="_Default" %>

2

3

4

5

6 Untitled Page

7

14

15

16

17

18

19

20

21

22

23

24

25

26

27

35

36

37

因为javscript不容易获得本机IP地址,故在后台注册了脚本,解决第二个难点问题 :后台代码

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.Web.UI.WebControls.WebParts;

9using System.Web.UI.HTMLControls;

10using System.Text;

11

12public partial class _Default : System.Web.UI.Page

13{

14 protected void Page_Load(object sender, EventArgs e)

15 {

16 StringBuilder sb = new StringBuilder();

17 string ip = Request.UserHostAddress;

18 sb.Append("");

21 string script = sb.ToString();

22 Page.RegisterClientScriptBlock("IpServices", script);

23

24 }

25}

这样目标即可实现,效果图:

责任编辑:admin
相关文章