Web Service

Web Service 是利用SOAP(Simple Object Access Protocol)在HTTP上执行远程方法调用的一种新方法。

SOAP

POST

POST /SomeLocation/myWebService.asmx HTTP/1.1
Host: hostname
Content-Type: text/xml;charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/DoSomething"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xml:xsi="http://www.w3.org/2001/XMLSchema-instance"
                      xml:xsd="http://www.w3.org/2001/XMLSchema"
                      xml:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <DoSomething xmlns="http://tempuri.org/">
            <stringParam>string</stringParam>
            <intParam>int</intParam>
        </DoSomething>
    </soap:Body>
</soap:Envelope>

 

GET

GET /SomeLocation/myWebService.asmx/DoSomething?stringParam=string&intParam=int HTTP/1.1
HOST: hostname

 

响应

SOAP响应

HTTP/1.1 200 OK
Content-Type: text/xml;charset=utf-8
Content-Length:length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                      xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                      xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <DoSomethingResponse xmlns="http://tempuri.org/">
            <DoSomethingResult>int</DoSomethingResult>
        </DoSomethingResponse>
    </soap:Body>
</soap:Envelope>

http响应

HTTP/1.1 200 OK
Content-Type: text/xml;charset=utf-8
Content-Length: length
<?xml version="1.0"?>
<int xmlns="http://tempuri.org">int</int>

 

WSDL

WSDL可以完整的描述Web服务、可用的方法以及调用这些方法的各种格式。

WSDL最重要的部分是类型定义部分。

使用Web Service
生成代理类
  • wsdl.exe
  • 使用VS.NET中的Add Web Reference(这种方法可以简单的进行更新)
Web服务Web Service)是一种基于Web的应用程序接口,允许不同的应用程序通过网络进行通信和交互。 ### 特征 Web服务通常具有以下特征: - 完好的封装性:使用者仅看到Web Service提供的功能列表,不能看到其实现细节。 - 松散耦合:只要服务的接口保持不变,就不影响使用者,而且客户端和服务端的生命周期不相互影响,同时需要一种适合Internet环境的消息交换协议。 - 使用标准协议规范:Web Service架构使用开放的标准协议进行描述、传输和交换。 - 高度可互操作性:可以跨越平台、语言进行调用。 - 高度可集成能力:高度可互操作性所带来的高度可集成能力,使得Web Service可用于集成异构的软件系统。 - 动态性:可以自动发现服务并进行调用 [^1]。 ### 与Web API的关系 Web API由Web Service演变而来,所有Web Service都是API,但并非所有API都是Web Service。两者都有利于信息的传输,但Web API比Web Service更具灵活性 [^2]。 ### 使用方法示例 以在“添加Web引用”界面添加服务为例:在“添加Web引用”界面,“URL”输入本地wsdl文件并点击右侧箭头;在身份验证弹出框输入账号口令,即可看到对方的Web service服务名是SI_03AA6_SNZYXT2ERP_JHDGBService,其函数/方法为SI_03AA6_SNZYXT2ERP_JHDGB()。把该服务命名为“ws1”,然后点击【添加引用】 [^3]。 ### 代码示例 以下是一个简单的Java代码示例,展示如何调用Web服务: ```java public class TestHelloWorld { public static void main(String[] args) throws Exception { HelloWorld_PortType helloWorld_portType = new HelloWorldServiceLocator().getHelloWorld(); String result = helloWorld_portType.sayHelloWorldFrom("Charles"); System.out.println(result); } } ``` 该代码通过`HelloWorldServiceLocator`获取`HelloWorld_PortType`实例,调用`sayHelloWorldFrom`方法并输出结果 [^4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值