开发者

getInputStream() Hang on a POST Request

开发者 https://www.devze.com 2023-03-19 14:13 出处:网络
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 p

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 po开发者_开发知识库sting 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--


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.


Try to test your payload with something like http://apikitchen.com/ and see what is returned.

The alternative would be to avoid doing this yourself altogether. HTTPClient, Jersey Client and Resty (which I'm the author of) solve this problem without you having to dive into the depths of HTTP and mime types.

Here is a Resty example to get you started:

import us.monoid.web.Resty;
import static us.monoid.web.Resty.*;

Resty r = new Resty();
String result = r.text(config.getServerUrl(), 
       form(data("MAX_FILE_SIZE","256"), 
            data("stats", "wsum"),...etc.etc.).toString();

(Assuming that text/* is being returned by the server)

0

精彩评论

暂无评论...
验证码 换一张
取 消