Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getInputStream() Hang on a POST Request

Tags:

java

http

post

My problem is that getInputStream() hangs when I try to open a stream after posting to a server. The server, written in python, gives an error message that leads me to believe that it is not able to parse my query string correctly. Please help me diagnose this problem.

I'm posting to a web server with the following code:

String boundarySolo = "--158211729626281";
String boundary = boundarySolo + "\r\n";
String contentHeader = "Content-Disposition: form-data; name=\"";
URL requestUrl;
URLConnection connection;
String queryString = new String();

try{
    requestUrl = new URL(config.getServerUrl());
    connection = requestUrl.openConnection();
    connection.setReadTimeout(1000);
    connection.setDoOutput(true);
    connection.setDoInput(true);

    connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundarySolo);

. . .

    outStream = new DataOutputStream(connection.getOutputStream());

    System.out.println("Writing bytes...");
    outStream.writeUTF(queryString);
    System.out.println("Bytes written");
    outStream.flush();
    System.out.println("Buffer flushed.");
    outStream.close();
    System.out.println("Stream closed.");

    try{
    inStream = new DataInputStream(connection.getInputStream());
    String buffer;

    System.out.println("Entering the loop.");

    while((buffer = inStream.readLine())!= null)
        System.out.println(buffer);

    System.out.println("Leaving the loop.");

    }
    catch (SocketTimeoutException e) {
    System.out.println("Socket timed out.");
    }

The query string consists of this (with \r\n at the end of each line):

  --158211729626281
  Content-Disposition: form-data; name="view"

  a
  --158211729626281
  Content-Disposition: form-data; name="termdb"

  Saccharomyces_cerevisiae.etd
  --158211729626281
  Content-Disposition: form-data; name="raw_weights"

  blank
  --158211729626281
  Content-Disposition: form-data; name="MAX_FILE_SIZE"

  256
  --158211729626281
  Content-Disposition: form-data; name="cutoff_Evalue"

  0.01
  --158211729626281
  Content-Disposition: form-data; name="min_term_size"

  2
  --158211729626281
  Content-Disposition: form-data; name="effective_tdb_size"

  0
  --158211729626281
  Content-Disposition: form-data; name="stats"

  wsum
  --158211729626281
  Content-Disposition: form-data; name="transform_weights"

  null
  --158211729626281
  Content-Disposition: form-data; name="cutoff_type"

  none
  --158211729626281
  Content-Disposition: form-data; name="output"

  tab
  --158211729626281--
like image 396
Alex Bliskovsky Avatar asked Aug 24 '26 02:08

Alex Bliskovsky


1 Answers

If I remember rightly, URLConnection#getInputStream() is what actually sends the request to the server (if the request written to the output stream wasn't big enough to cause a flush). It then hangs until a response is flushed from the server.

Can you debug on the server? That way you can tell how far it gets and why the connection is being kept open. If the server were to close the connection, I would expect an exception on the client side.

If you are using Eclipse, use Window menu -> Show View -> Other and select TCP/IP Monitor. You can use that to really see what is sent over the wire.

like image 123
Ant Kutschera Avatar answered Aug 25 '26 15:08

Ant Kutschera