上篇地址:atlas学习实践系列之二(AutoCompleteExtender篇)
atlas调用本地web services那是相当的简单,调用远程的也不难,看了dflying chen的几篇文章也学了不少内容,尤其是yahoo天气预报的例子更是受益匪浅。自己动手实践一把。
目标:实现IP地址查询功能.输入IP地址,获得该IP所在的国家和城市。
难点:1)如何设置调用远程服务的asbx文件(有关配置支持asbx的方法可以见dflying chen 的介绍)
难点2):如何动态获得请求的客户端IP,然后在js中使用。
首先,建立一个网络服务,用于查询IP,IP地址信息库是一个mdb文件,相关代码为:
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配置
<system.web><webServices>
<protocols>
<add name="HttpPost"/>
<add name="HttpGet"/>
</protocols>
</webServices>
</system.web>
网络服务建好了,下面就是调用了。
建立一个新的网站,首先建立一个IPServices.asbx文件,该文件是将远程服务映射到本地的配置文件,有关信息见dflying chen的文章。文件内容如下:
1<?xml version="1.0" encoding="utf-8"?>
2<bridge namespace="n2dog" className="IpServices">
3 <proxy type="Microsoft.Web.Services.BridgeRestProxy" serviceUrl="http://127.0.0.1/IPServices/Service.asmx/GetIPAddress"></proxy>
4 <method name="GetIPAddress">
5 <input>
6 <parameter name="ip"></parameter>
7 </input>
8 </method>
9
10</bridge>
11
12
调用页面为:
页面前台1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4<html xmlns="http://www.w3.org/1999/xhtml">
5<head runat="server">
6 <title>Untitled Page</title>
7 <script language="javascript" type="text/javascript">
8
9 function onGetCompleted(result)
10 {
11 $('country').innerHTML = '您的位置:'+result;
12 }
13 </script>
14</head>
15<body>
16 <form id="form1" runat="server">
17 <atlas:ScriptManager ID="ScriptManager1" runat="server">
18 <Services>
19 <atlas:ServiceReference Path="IPServices.asbx" />
20 </Services>
21 </atlas:ScriptManager>
22 <div>
23 <input id="Button1" type="button" value="button" onclick="getIpAddress()" /><div id="country"></div>
24 </div>
25 </form>
26
27 <script type="text/xml-script">
28 <page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
29 <references>
30 </references>
31 <components>
32 </components>
33 </page>
34 </script>
35</body>
36</html>
37
因为javscript不容易获得本机IP地址,故在后台注册了脚本,解决第二个难点问题 :后台代码
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("<script language=\"javascript\" type=\"text/javascript\">");
19 sb.Append(" function getIpAddress(){var ip='"+ip+"';n2dog.IpServices.GetIPAddress({'ip':ip},onGetCompleted);}");
20 sb.Append("</script>");
21 string script = sb.ToString();
22 Page.RegisterClientScriptBlock("IpServices", script);
23
24 }
25}
这样目标即可实现,效果图:
上篇地址:atlas学习实践系列之二(AutoCompleteExtender篇)
再次感谢dflying chen的系列文章对我的启发和指导。
转载于:https://www.cnblogs.com/jillzhang/archive/2006/08/22/483020.html