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

16 篇文章 0 订阅
13 篇文章 0 订阅

目前比较常用的 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;

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

原文地址:http://www.izhangheng.com/java-csharp-webservice-timeout

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#调用WebService的方法有两种,静态调用和动态调用。 静态调用是指通过添加WebService的引用来调用WebService。这种方式需要先在项目中添加WebService的引用,然后通过生成的代理类来访问WebService的方法。静态调用的缺点是如果WebService的地址或者内容发生改变,就需要重新添加引用。 动态调用是指在运行时动态创建WebService的代理类来调用WebService的方法。使用动态调用的方式可以解决静态调用中的问题,无需添加引用,可以根据实际情况动态调用不同的WebService。在C#中,可以使用WebClient、HttpWebRequest或者HttpClient等类来进行动态调用WebService。 以下是使用动态调用的示例代码: ``` string url = "WebService的地址"; string methodName = "WebService的方法名"; string param1 = "参数1"; string param2 = "参数2"; // 创建HttpWebRequest对象 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; // 构建SOAP消息体 string soapRequest = string.Format( @"<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> <{0} xmlns=""http://tempuri.org/""> <param1>{1}</param1> <param2>{2}</param2> </{0}> </soap:Body> </soap:Envelope>", methodName, param1, param2); byte[] requestData = Encoding.UTF8.GetBytes(soapRequest); request.ContentLength = requestData.Length; // 发送请求并获取响应 using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(requestData, 0, requestData.Length); } using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { // 读取响应内容 using (StreamReader reader = new StreamReader(response.GetResponseStream())) { string responseXml = reader.ReadToEnd(); // 解析响应内容,获取结果 // ... } } ``` 通过以上的代码,你可以根据实际情况动态调用WebService的方法,并获取到返回的结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值