tags:

views:

326

answers:

1

Tomcat server runs in US. I am connecting to the server from China using Java's HTTPURLConnection. Please see the code snippet used in client side and the https connector configuration in the tomcat server side below.

<!-- Define a SSL Coyote HTTP/1.1 Connector on port 8443 -->
    <Connector acceptCount="100" clientAuth="false" connectionTimeout="-1" debug="4" disableUploadTimeout="true" enableLookups="false" keystoreFile="conf/server.keystore" keystorePass="passw47d" maxSpareThreads="75" maxThreads="150" minSpareThreads="25" port="443" scheme="https" secure="true" sslProtocol="TLS" useBodyEncodingForURI="true"/>


    URL url=new URL(urlString);
    HttpsURLConnection connection=null;
    try
    {
     connection=(HttpsURLConnection)url.openConnection();
     connection.setDoOutput(true);
     connection.setDoInput(true);
     connection.setRequestMethod("POST");
     connection.setRequestProperty("Content-Type", "application/zip");
     connection.setRequestProperty("Transfer-Encoding", "chunked" );
     connection.setChunkedStreamingMode(4096);
     connection.connect();
     sout=new BufferedOutputStream(connection.getOutputStream());
     break;
    }
    catch(FileNotFoundException exc)
    {
     throw exc;
    }
    bis=new FileInputStream(zipfile);

    int i;    
    byte bytes[]=new byte[4096]; 
    while((i=bis.read(bytes))!=-1)
    {
     sout.write(bytes,0,i);
     sout.flush();
    }  
    sout.close();
    bis.close();

The client uploads the zip files successfully most of the times. Occasionally the client program throws the following exception.

java.io.IOException: Error writing request body to server
at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.checkError(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.write(Unknown Source)
at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
at java.io.BufferedOutputStream.flush(Unknown Source)
at SendFiles.sendNowThruHttp(SendFiles.java:449)
at SendFiles.run(SendFiles.java:180)

What could be the problem?

+1  A: 

Probably a network timeout. But to be sure, have a look at the server log files. They will contain an error message, too.

I also suggest to have a look at the HttpClient Java library which makes things like these much more simple any reliable. See this article (near the end) for an example.

Aaron Digulla
+1 for Apache HttpClient. It has error-handling logic built in that will simply retry the request for most basic errors, and allows you to specify you own logic as well.
matt b
I set the disableUploadTimeout="true" property on the Tomcat side. So I don't think it will timeout.
Jay
@Jay: The TCP connection will time out after two minutes if there is no reply from the client/server. This value can't be changed, it's in the TCP/IP stack of your computer.
Aaron Digulla