我已经编写了一些代码来与Web服务进行通信并将一些数据传递给它。以下是我的代码段。我将readtimeout设置为55秒,我不知道它是否合适?我对readtimeout的理解是从服务器发送最后一条消息的时间。我不知道我的代码或服务器有什么问题,但是我的阅读时间太长了?我在下面列出了我一直得到的错误消息。我可以做些什么调整来解决读取超时的问题?
void sendData(int dID,String message) throws Exception {
SOAPConnection soapConnection = null;
try{
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
soapConnection = soapConnectionFactory.createConnection();
String url = "http://****/**/***/webservice.asmx?WSDL";
URL endpoint = new URL (null, "http://****/**/***/webservice.asmx?WSDL", new URLStreamHandler () {
protected URLConnection openConnection (URL url) throws IOException {
// The url is the parent of this stream handler, so must create clone
URL clone = new URL (url.toString ());
URLConnection connection = clone.openConnection ();
// If you cast to HttpURLConnection, you can set redirects
// connection.setInstanceFollowRedirects (false); // no redirs
connection.setConnectTimeout (5 *1000); // 5 sec
connection.setReadTimeout (55 *1000); // 55 sec
return connection;
}});
SOAPMessage soapACK = soapConnection.call(createSOAPSending(dID,message), endpoint); }
catch (SocketTimeoutException e){
soapExceptions=1;
e.printStackTrace();
System.err.println(e.toString());
e.printStackTrace(System.out);
throw e;
}
catch (SOAPException e){
soapExceptions=1;
e.printStackTrace();
System.err.println(e.toString());
e.printStackTrace(System.out);
throw e;
}
catch (Exception e){
soapExceptions=1;
e.printStackTrace();
System.err.println(e.toString());
System.out.println(e.toString());
e.printStackTrace(System.out);
throw e;
}
finally{
try{
if ( soapConnection != null ){
soapConnection.close();
soapConnection=null;
}
else{
System.out.println("is null ");
}
}
catch(SOAPException ex){
System.out.println("SOAPException has been caught for soapConnection close");
ex.printStackTrace(System.out);
}
}
}
SOAPMessage createSOAPSending(int dID,String message) throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();
SOAPBodyElement element = body.addBodyElement(envelope.createName("sData","", "http://***"));
element.addChildElement("dID").addTextNode(dID);
element.addChildElement("pmessage").addTextNode(message);
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", "http://*****");
soapMessage.saveChanges();
/* Print the request message */
soapMessage.writeTo(System.out);
return soapMessage;
}
以下是错误日志。
Caused by: java.net.SocketTimeoutException: Read timed out
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1659)
at sun.reflect.GeneratedConstructorAccessor7.newInstance(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1657)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1240)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1661)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1659)
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:265)
at java.security.AccessController.doPrivileged(Native Method)
... 4 more
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1657)
Caused by: java.net.SocketTimeoutException: Read timed out
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1240)
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
at java.net.SocketInputStream.read(SocketInputStream.java:150)
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:265)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
... 4 more
at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
Caused by: java.net.SocketTimeoutException: Read timed out
at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
at java.net.SocketInputStream.socketRead0(Native Method)
at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
at java.net.SocketInputStream.read(SocketInputStream.java:150)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:643)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:589)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1319)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:245)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:643)
... 4 more
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:589)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1319)