How do you debug a hanging socket connection?

We connect to the web service as follows:

URL url = new URL("https://...");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-length", "" + data.length());
conn.setDoOutput(true);
OutputStreamWriter o = new OutputStreamWriter(conn.getOutputStream());

      

Sometimes the connection hangs at this last line with a very long or infinite timeout.

How can we figure this out and figure out what's going on? (We have a lot of speculation, but little confirmation at this point.) We measured our code with a lot of registration statements (as we know where the breakpoint is), but can't do the same very well for the Java library. What additional information can we convince these classes to tell us? If we wanted to set a timeout, how would we do it?

How do we debug this?

+2


source to share


2 answers


If you are using the Sun JSSE provider to support SSL (this is the default behavior when using the "https:" URL at Sun runtime), you can set a system property javax.net.debug

to view the progress of the SSL handshake.

Details on the values ​​that can be used are in the JSSE reference manual; to enable all SSL debugging use something like



java -Djavax.net.debug=all com.y.MyClass

      

Since most of the initial handshake is unencrypted, a tool like Wireshark can be very helpful if you are familiar with SSL. If you have the server private key, Wireshark should also decrypt the conversation (as long as you don't use an ephemeral cipher suite), but I could never get it to work.

+4


source


You may be stuck due to Keep-Alive connection. Try to force the connection to close ...



URL url = new URL("https://...");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "close");
conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-length", "" + data.length());
conn.setDoOutput(true);
OutputStreamWriter o = new OutputStreamWriter(conn.getOutputStream());

      

0


source







All Articles