一个Web Service的例子

Web服务例子(自己写的,好像在给自己做宣传,不过,暂时没别的例子可说只好将就): http://www26.brinkster.com/ipservices/ip.asmx,这个services就干一个事,你给它ip,它返回ip的所在地(北京、上海)或者空(它不知道)。返回的结构是这样的:
public class IPInfo
{
 public string ip;
 public string address = "";
}

只有一个函数可用:
[WebMethod]
public IPInfo GetIPInfo(string ip)
{
 // ...
}

如果在.net环境里用,首先要声称它的代理DLL(不知道国内怎么翻,就是一个客户端的包裹程序),别的环境也差不用。包裹程序在.net里是自动生成的(可惜不知道怎么贴图说明),下面是自动生成的包裹程序的代码(我用的是web matrix):

//------------------------------------------------------------------------------
//
//     This code was generated by a tool.
//     Runtime Version: 1.1.4322.573
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
//
//------------------------------------------------------------------------------

//
// ASP.NET Web Matrix
//
using System.Diagnostics;
using System.Xml.Serialization;
using System;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Web.Services;


///
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="IPSoap", Namespace="
http://www26.brinkster.com/ipservices/ip.asmx")]
public class IP : System.Web.Services.Protocols.SoapHttpClientProtocol {
   
    ///
    public IP() {
        this.Url = "
http://www26.brinkster.com/ipservices/ip.asmx";
    }
   
    ///
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("
http://www26.brinkster.com/ipservices/ip.asmx/GetIPInfo", RequestNamespace="http://www26.brinkster.com/ipservices/ip.asmx", ResponseNamespace="http://www26.brinkster.com/ipservices/ip.asmx", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    public IPInfo GetIPInfo(string ip) {
        object[] results = this.Invoke("GetIPInfo", new object[] {
                    ip});
        return ((IPInfo)(results[0]));
    }
   
    ///
    public System.IAsyncResult BeginGetIPInfo(string ip, System.AsyncCallback callback, object asyncState) {
        return this.BeginInvoke("GetIPInfo", new object[] {
                    ip}, callback, asyncState);
    }
   
    ///
    public IPInfo EndGetIPInfo(System.IAsyncResult asyncResult) {
        object[] results = this.EndInvoke(asyncResult);
        return ((IPInfo)(results[0]));
    }
}

///
[System.Xml.Serialization.XmlTypeAttribute(Namespace="
http://www26.brinkster.com/ipservices/ip.asmx")]
public class IPInfo {
   
    ///
    public string ip;
   
    ///
    public string address;
}

编译它,生成一个ip.dll,用起来是这样的,在你的代码里。

IP ip = new IP();
this.Address.Text = ip.GetIPInfo("192.168.100.1").address;

ip 就是那个代理类
Address 是用来显示地址的label,
192.168.100.1 要查的地址。

label里就可以显示客户的所在地了。应该是很简单了,和调一般类函数没什么区别。


两点:
1、代理类是必要的,他简化了客户端很多工作,甚至不需要知道调用的是web services,还是一般的类函数,他们的语法是一样的。
2、代理类里还自动生成了返回用到的类型(当然有限制),这个就更简化了使用。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值