简体   繁体   English

getInputStream() 挂起一个 POST 请求

[英]getInputStream() Hang on a POST Request

My problem is that getInputStream() hangs when I try to open a stream after posting to a server.我的问题是当我在发布到服务器后尝试打开 stream 时getInputStream()挂起。 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.用 python 编写的服务器给出了一条错误消息,让我相信它无法正确解析我的查询字符串。 Please help me diagnose this problem.请帮我诊断这个问题。

I'm posting to a web server with the following code:我使用以下代码发布到 web 服务器:

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):查询字符串由以下内容组成(每行末尾有\r\n ):

  --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).如果我没记错的话, URLConnection#getInputStream() 是实际将请求发送到服务器的内容(如果写入 output stream 的请求不足以导致刷新)。 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.如果您使用 Eclipse,请使用 Window 菜单 -> 显示视图 -> 其他和 select TCP/IP 监视器。 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.尝试使用http://apikitchen.com/ 之类的东西测试您的有效负载,然后查看返回的内容。

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. HTTPClient、Jersey Client 和 Resty(我是其作者)解决了这个问题,而无需深入了解 HTTP 和 mime 类型。

Here is a Resty example to get you started:这是一个让您入门的 Resty 示例:

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) (假设服务器正在返回 text/*)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HttpURLConnection getInputStream读取页面,然后发出POST请求 - HttpURLConnection getInputStream to read page, and then make POST request 发送 Java POST 请求而不调用 getInputStream() - Sending Java POST request without calling getInputStream() 发送POST请求并读取响应。 conException.getInputStream()时出现IOException - Send POST request and read response. IOException when conn.getInputStream() 如何使用getInputStream()获取Clojure中的POST请求数据。 我在Reflector中收到了NullPointerException - How to use getInputStream() to get POST request data in Clojure. I am getting an NullPointerException in Reflector POST时getInputStream时发生JAVA EOFException - JAVA EOFException when getInputStream from POST Java请求getInputStream返回空流 - Java request getInputStream return empty stream 发送JSON请求时getInputStream引发EOFException - getInputStream throws EOFException when sending JSON request 发出API请求时出现getInputStream错误 - getInputStream error when making API request 无法从request.getInputStream()获取图像 - Unable to getImage from request.getInputStream() 如何多次读取 request.getInputStream() - How to read request.getInputStream() multiple times
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM