There are two methods of WebService that are commonly used: SOAP and REST. This article introduces the following methods:programmingCommonly used timeout settings includeJava、C#Calling remote SOAP services and using HttpClient(Java) and HttpWebRequest(C#) Timeout issue calling REST service (simple HTTP service).
What is WebSerivce?
A Web Service is a software interface that describes a set of operations that can be accessed over the network through standardized XML messaging. The most basic components of Web Service are the service provider and the service requester. Applications on both ends of Web Service communicate through protocols based on standard XML formats. The most commonly used protocol is SOAP (Simple Object Access Protocol).
Detailed introduction:Architecture Web Service: What is a Web Service?
SOAP
When using WebService, we may need a backup WebService server. Once the main server is down, we can use the backup server. Then here we need to make a modification to the time when the client service connects to the server.
The following are introduced inJavaand the timeout setting for calling WebService in C#.
Java (CXF service)
In the WebService environment of Spring+CXF, the client has two time attributes that are configurable, namely ConnectionTimeout and ReceiveTimeout.
ConnectionTimeout — WebService is based on TCP connection. This property can be understood as the time setting of tcp's handshake. If the time exceeds the setting, it is considered to be the connection timeout, in milliseconds. The default is 30,000 milliseconds, that is, 30 seconds.
ReceiveTimeout — This property is the time to wait for a response after sending a WebService request. If the time exceeds the set time, it is considered to be the response timeout. In milliseconds, the default is 60,000 milliseconds, that is, 60 seconds.
Add the following configuration to the xml file that calls the CXF service:
<!-- Single Service --> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="/schema/beans" xmlns:xsi="http:///2001/XMLSchema-instance" xmlns:jee="/schema/jee" xmlns:jaxws="/jaxws" xmlns:http-conf="/transports/http/configuration" xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-2. /schema/jee /schema/jee/spring-jee-2. /jaxws /schemas/ /transports/http/configuration /schemas/configuration/ "> <http-conf:conduit name="{/}-conduit"> <http-conf:client ConnectionTimeout="10000" ReceiveTimeout="20000"/> </http-conf:conduit> </beans> <!-- All Services --> <http-conf:conduit name="*.http-conduit"> <http-conf:client ConnectionTimeout="10000" ReceiveTimeout="20000"/> </http-conf:conduit>
There are a few things to note here:
1. You need to specify the http-conf namespace: xmlns:http-conf=/transports/http/configuration
2. Specify the mode location: /transports/http/configuration/schemas/configuration/
3. The name attribute in http-conf:conduit specifies the service to be set to take effect.
For more detailed configuration, please refer to:CXF official documentation
C# (WSDL generation proxy class)
Timeout setting for WebService on the .net platform (also including the general HttpWebRequest).
1. Server-side setting timeout
Add the following configuration items in :
< httpRuntime executionTimeout="30"/>
The above time unit is seconds. Remember to turn off the debug mode:
< compilation defaultLanguage="c#" debug="false"/>
If debug mode is not turned off, executionTimeout will be ignored.
2. Client settings timeout
Set the Request timeout time in the client agent of WebService (used to generate) in milliseconds, the connection timeout Timeout defaults to 100 seconds, and the reading data timeout defaults to 300 seconds:
protected override WebRequest GetWebRequest(Uri uri) { HttpWebRequest webRequest = (HttpWebRequest)( uri ); = 100*1000; = 300*1000; return webRequest; }
For more detailed configuration, please refer to::、
REST
What is REST?
Representational State Transfer (REST) has been widely accepted in the web field and is a simpler alternative to Web services based on SOAP and Web Services Description Language (WSDL). Key evidence of this shift in interface design is the adoption of REST by mainstream Web 2.0 service providers (including Yahoo, Google, and Facebook) that deprecated or abandoned SOAP and WSDL-based interfaces to expose their services with an easier-to-use, resource-oriented model. If you consider the number of web services that use it, REST has become the most important web service design model in recent years.
Detailed introduction:REST-based web services
Java(HttpClient)
When Java uses HttpClient to call REST service, the key is to set a timeout for HttpParams, as follows:
HttpParams params = new BasicHttpParams(); (CoreConnectionPNames.CONNECTION_TIMEOUT, 30*1000); //Set connection timeout(CoreConnectionPNames.SO_TIMEOUT, 60*1000); //When waiting for the maximum dataHttpClient client = new DefaultHttpClient(params);
C#(HttpWebRequest)
C# still uses HttpWebRequest to call REST service or simple HTTP. The timeout setting method is similar to calling SOAP, as follows:
HttpWebRequest httpRequest = (HttpWebRequest)(url); = "application/json;charset=UTF-8"; = "POST"; = 100*1000; = 300*1000;
That’s all I’ve introduced, I think it’s good, please like it!
Original address:/java-csharp-webservice-timeout