Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java getInputStream()挂起POST请求_Java_Http_Post - Fatal编程技术网

Java getInputStream()挂起POST请求

Java getInputStream()挂起POST请求,java,http,post,Java,Http,Post,我的问题是getInputStream()在发布到服务器后尝试打开流时挂起。用python编写的服务器给出了一条错误消息,使我相信它无法正确解析查询字符串。请帮我诊断这个问题 我正在使用以下代码发布到web服务器: String boundarySolo = "--158211729626281"; String boundary = boundarySolo + "\r\n"; String contentHeader = "Content-Disposition: form-data; na

我的问题是
getInputStream()
在发布到服务器后尝试打开流时挂起。用python编写的服务器给出了一条错误消息,使我相信它无法正确解析查询字符串。请帮我诊断这个问题

我正在使用以下代码发布到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.");
    }
查询字符串由以下内容组成(每行末尾有
\r\n
):


如果我没记错的话,URLConnection#getInputStream()实际上是将请求发送到服务器的(如果写入输出流的请求不够大,无法导致刷新)。然后它将挂起,直到从服务器刷新响应为止

你能在服务器上调试吗?这样,您就可以知道它走了多远,以及为什么连接保持打开状态。如果服务器关闭连接,我希望客户端出现异常


如果您使用的是Eclipse,请使用窗口菜单->显示视图->其他并选择TCP/IP监视器。您可以使用它来真正查看通过导线发送的内容。

尝试使用以下方法测试您的有效负载 看看有什么回报

另一种选择是完全避免自己这样做。 HTTPClient、Jersey Client和Resty(我是其作者)解决了这个问题,而不必深入研究HTTP和mime类型

下面是一个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();

(假设服务器返回text/*时)

那么您在服务器中收到了什么错误消息?你可能想把它重新组织成一个针对Python的问题。我最后选择了HttpClient。谢谢。以防万一:如果您想要TCP/IP监视器,您需要在Eclipse中安装Web开发人员工具
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();