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

上篇地址:atlas学习实践系列之二(AutoCompleteExtender篇)
atlas调用本地web services那是相当的简单,调用远程的也不难,看了dflying chen的几篇文章也学了不少内容,尤其是yahoo天气预报的例子更是受益匪浅。自己动手实践一把。
目标:实现IP地址查询功能.输入IP地址,获得该IP所在的国家和城市。
难点:1)如何设置调用远程服务的asbx文件(有关配置支持asbx的方法可以见dflying chen 的介绍)
难点2):如何动态获得请求的客户端IP,然后在js中使用。
首先,建立一个网络服务,用于查询IP,IP地址信息库是一个mdb文件,相关代码为:

ContractedBlock.gif ExpandedBlockStart.gif
 1None.gifusing System;
 2None.gifusing System.Web;
 3None.gifusing System.Web.Services;
 4None.gifusing System.Web.Services.Protocols;
 5None.gifusing System.Data;
 6None.gifusing System.Data.OleDb;
 7None.gif
 8None.gif[WebService(Namespace = "http://tempuri.org/")]
 9None.gif[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
10None.gifpublic class Service : System.Web.Services.WebService
11ExpandedBlockStart.gifContractedBlock.gifdot.gif{
12ExpandedSubBlockStart.gifContractedSubBlock.gif    public Service () dot.gif{
13InBlock.gif
14InBlock.gif        //如果使用设计的组件,请取消注释以下行 
15InBlock.gif        //InitializeComponent(); 
16ExpandedSubBlockEnd.gif    }

17InBlock.gif    [WebMethod]
18InBlock.gif    public string[] GetIPAddress(string ip)
19ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
20InBlock.gif        //数据库文件物理路径
21InBlock.gif        string dbFilePath = Server.MapPath("~/App_Data/IPaddress.mdb");
22InBlock.gif        //数据库连接字符串
23InBlock.gif        string conStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + dbFilePath;
24InBlock.gif        string country = "未知";
25InBlock.gif        string city = "未知";
26InBlock.gif        //数据库连接
27InBlock.gif        OleDbConnection con = new OleDbConnection(conStr);
28InBlock.gif        try
29ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
30InBlock.gif            long ipAddress = System.Net.IPAddress.Parse(ip).Address;
31InBlock.gif            string cmdText = " select top 1 * from dv_address where ip1 <= " + ipAddress + " and ip2 >= " + ipAddress;
32InBlock.gif            con.Open();
33InBlock.gif            OleDbCommand cmd = new OleDbCommand(cmdText, con);
34InBlock.gif            OleDbDataReader dr = cmd.ExecuteReader();
35InBlock.gif            if (dr.Read())
36ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
37InBlock.gif                country = dr["country"].ToString();
38InBlock.gif                city = dr["city"].ToString();
39ExpandedSubBlockEnd.gif            }

40ExpandedSubBlockEnd.gif        }

41InBlock.gif        catch
42ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
43ExpandedSubBlockStart.gifContractedSubBlock.gif            return new string[] dot.gif{ country, city };
44ExpandedSubBlockEnd.gif        }

45InBlock.gif        finally
46ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
47InBlock.gif            con.Close();
48ExpandedSubBlockEnd.gif        }

49ExpandedSubBlockStart.gifContractedSubBlock.gif        return new string[] dot.gif{ country, city };
50ExpandedSubBlockEnd.gif    }
 
51InBlock.gif     
52InBlock.gif    
53ExpandedBlockEnd.gif}

54None.gif

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

None.gif < system .web >
None.gif    
< webServices >
None.gif      
< protocols >
None.gif        
< add  name ="HttpPost" />
None.gif        
< add  name ="HttpGet" />
None.gif      
</ protocols >
None.gif    
</ webServices >
None.gif
</ system.web >

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

ContractedBlock.gif ExpandedBlockStart.gif asbx配置文件
 1None.gif<?xml version="1.0" encoding="utf-8"?>
 2None.gif<bridge namespace="n2dog" className="IpServices">
 3None.gif  <proxy type="Microsoft.Web.Services.BridgeRestProxy" serviceUrl="http://127.0.0.1/IPServices/Service.asmx/GetIPAddress"></proxy>
 4None.gif  <method name="GetIPAddress">
 5None.gif    <input>
 6None.gif      <parameter name="ip"></parameter>     
 7None.gif    </input>
 8None.gif  </method>
 9None.gif
10None.gif</bridge>
11None.gif
12None.gif

调用页面为:

ContractedBlock.gif ExpandedBlockStart.gif 页面前台
 1ExpandedBlockStart.gifContractedBlock.gif<%dot.gif@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 2None.gif
 3None.gif<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
 4None.gif<html xmlns="http://www.w3.org/1999/xhtml">
 5None.gif<head runat="server">
 6None.gif    <title>Untitled Page</title>
 7ExpandedBlockStart.gifContractedBlock.gif    <script language="javascript" type="text/javascript">dot.gif
 8InBlock.gif    
 9InBlock.gif       function onGetCompleted(result)
10ExpandedSubBlockStart.gifContractedSubBlock.gif       dot.gif{        
11InBlock.gif          $('country').innerHTML = '您的位置:'+result;         
12ExpandedSubBlockEnd.gif       }

13ExpandedBlockEnd.gif    
</script>
14None.gif</head>
15None.gif<body>
16None.gif    <form id="form1" runat="server">
17None.gif        <atlas:ScriptManager ID="ScriptManager1" runat="server">
18None.gif           <Services>
19None.gif              <atlas:ServiceReference Path="IPServices.asbx" />
20None.gif           </Services>
21None.gif        </atlas:ScriptManager>
22None.gif        <div>
23None.gif            <input id="Button1" type="button" value="button" onclick="getIpAddress()" /><div id="country"></div>            
24None.gif        </div>
25None.gif    </form>
26None.gif
27ExpandedBlockStart.gifContractedBlock.gif    <script type="text/xml-script">dot.gif
28InBlock.gif        <page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
29InBlock.gif            <references>
30InBlock.gif            </references>
31InBlock.gif            <components>
32InBlock.gif            </components>
33InBlock.gif        </page>
34ExpandedBlockEnd.gif    
</script>
35None.gif</body>
36None.gif</html>
37None.gif

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

 1 None.gif using  System;
 2 None.gif using  System.Data;
 3 None.gif using  System.Configuration;
 4 None.gif using  System.Web;
 5 None.gif using  System.Web.Security;
 6 None.gif using  System.Web.UI;
 7 None.gif using  System.Web.UI.WebControls;
 8 None.gif using  System.Web.UI.WebControls.WebParts;
 9 None.gif using  System.Web.UI.HtmlControls;
10 None.gif using  System.Text;
11 None.gif
12 None.gif public  partial  class  _Default : System.Web.UI.Page 
13 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
14InBlock.gif    protected void Page_Load(object sender, EventArgs e)
15ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
16InBlock.gif        StringBuilder sb = new StringBuilder();
17InBlock.gif        string ip = Request.UserHostAddress;
18InBlock.gif        sb.Append("<script language=\"javascript\" type=\"text/javascript\">");
19InBlock.gif        sb.Append(" function getIpAddress(){var ip='"+ip+"';n2dog.IpServices.GetIPAddress({'ip':ip},onGetCompleted);}");
20InBlock.gif        sb.Append("</script>");
21InBlock.gif        string script = sb.ToString();        
22InBlock.gif        Page.RegisterClientScriptBlock("IpServices", script);
23InBlock.gif
24ExpandedSubBlockEnd.gif    }

25ExpandedBlockEnd.gif}

这样目标即可实现,效果图:
上篇地址:atlas学习实践系列之二(AutoCompleteExtender篇)
再次感谢dflying chen的系列文章对我的启发和指导。

转载于:https://www.cnblogs.com/jillzhang/archive/2006/08/22/483020.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值