post

调用WebService超时问题汇总(Java、C#)

目前比较常用的 WebService 有两种方式:SOAP和REST,本文就以调用这两种方式的服务,介绍编程中常用的超时设置,包括 JavaC# 调用远程 SOAP 服务,以及利用 HttpClient(Java)和 HttpWebRequest(C#)调用 REST 服务(简单的 HTTP 服务)的超时问题。

小彩旗停不下来

WebSerivce 是什么?

Web 服务是一个软件接口,它描述了一组可以在网络上通过标准化的 XML 消息传递访问的操作。Web Service 最基本的组成部分为服务的提供者(Service Provider)和服务的请求者(Service Requester)。Web Service 两端的应用是通过基于标准的 XML 格式的协议进行通信的,这种最常用的协议就是 SOAP(Simple Object Access Protocol)。

详细介绍:架构Web Service: 什么是Web服务?

SOAP

在使用 WebService 的时候,我们可能需要一个备份的 WebService 服务器。一旦主服务器 down 了,我们可以使用备份的服务器。那么这里就需要对客服端连接服务器的时间做一个修改。

下面分别介绍在 Java 和 C# 中调用 WebService 的超时设置。

Java(CXF服务)

在 Spring+CXF 的 WebService 环境下,客户端有两个时间属性是可配置的,分别是 ConnectionTimeout 和ReceiveTimeout。

ConnectionTimeout — WebService 以 TCP 连接为基础,这个属性可以理解为 tcp 的握手时的时间设置,超过设置的时间长则认为是连接超时,以毫秒为单位,默认是 30,000 毫秒,即 30 秒。

ReceiveTimeout — 这个属性是发送 WebService 的请求后等待响应的时间,超过设置的时长就认为是响应超时,以毫秒为单位,默认是 60,000 毫秒,即60秒。

在调用 CXF 服务的 xml 文件中加入下面的配置:

<!-- 单个服务 -->
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:jaxws="http://cxf.apache.org/jaxws"  
    xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"   
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd  
           http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd  
           http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd ">  
    <http-conf:conduit name="{http://impl.service.izhangheng.com/}WebService.http-conduit">   
        <http-conf:client ConnectionTimeout="10000" ReceiveTimeout="20000"/>  
    </http-conf:conduit>
</beans> 

<!-- 所有服务 -->   
<http-conf:conduit name="*.http-conduit">  
    <http-conf:client ConnectionTimeout="10000" ReceiveTimeout="20000"/>   
</http-conf:conduit>  

这里需要注意的有几个地方:
1、需要指定 http-conf 名称空间:xmlns:http-conf=http://cxf.apache.org/transports/http/configuration
2、指定模式位置:http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd
3、http-conf:conduit中的 name 属性,指定设置生效的服务。

更详细的配置请参考:CXF官方文档

C#(WSDL生成代理类)

.net 平台上 WebService (也包括一般意义的 HttpWebRequest) 的超时设置。

1、服务器端设置超时
在 web.config 的 system.web 里添加如下配置项:
< httpRuntime executionTimeout="30"/>

以上时间单位是秒,记得要把 web.config 的 debug 模式关闭:
< compilation defaultLanguage="c#" debug="false"/>

如果 debug 模式没有关闭, executionTimeout 会被忽略。

2、客户端设置超时
在 WebService 的客户端代理程序 (用 wsdl.exe 生成) 里设置 Request 超时时间,单位是毫秒,连接超时 Timeout 默认 100 秒,读取数据超时 ReadWriteTimeout 默认为 300 秒:

protected override WebRequest GetWebRequest(Uri uri)
{
    HttpWebRequest webRequest = (HttpWebRequest)base.GetWebRequest( uri );
    webRequest.Timeout = 100*1000;
    webRequest.ReadWriteTimeout = 300*1000;
    return webRequest;
}  

更详细的配置请参考:: HttpWebRequest.TimeoutHttpWebRequest.ReadWriteTimeout

REST

REST是什么?

代表性状态传输(Representational State Transfer,REST)在 Web 领域已经得到了广泛的接受,是基于 SOAP 和 Web 服务描述语言(Web Services Description Language,WSDL)的 Web 服务的更为简单的替代方法。接口设计方面这一转变的关键证据是主流 Web 2.0 服务提供者(包括 Yahoo、Google 和 Facebook)对 REST 的采用,这些提供者弃用或放弃了基于 SOAP 和 WSDL 的接口,而采用了更易于使用、面向资源的模型来公开其服务。如果考虑使用它的 Web 服务的数量,REST 近年来已经成为最主要的 Web 服务设计模型。

详细介绍:基于 REST 的 Web 服务

Java(HttpClient)

Java 利用 HttpClient 调用 REST 服务时,关键是给 HttpParams 设置超时,如下:

HttpParams params = new BasicHttpParams();
params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30*1000); //设置连接超时
params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 60*1000); //等待数据的最大时
HttpClient client = new DefaultHttpClient(params);

C#(HttpWebRequest)

C# 还是利用 HttpWebRequest 来调用 REST 服务或者简单的 HTTP,超时的设置方法和调用 SOAP 类似,如下:

HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.ContentType = "application/json;charset=UTF-8";
httpRequest.Method = "POST";
httpRequest.Timeout = 100*1000;
httpRequest.ReadWriteTimeout = 300*1000;

就介绍到这,觉得不错,请给赞!

Speak Your Mind

*

· 4,541 次浏览