简体   繁体   English

Java HttpURLConnection如何从服务器接收多个响应

[英]Java HttpURLConnection how to receive multiple responses from server

I am attempting to connect to a server with a POST message asking the server to subscribe me. 我试图通过POST消息连接到服务器,要求该服务器订阅我。 The server will then hold the http connection open and send back asynchronous messages to me with live statuses until I request to cancel the subscription or close the connection myself. 然后,服务器将保持http连接打开,并以实时状态将异步消息发送回给我,直到我请求取消订阅或自己关闭连接。 I am having trouble reading these subsequent responses from the server. 我无法从服务器读取这些后续响应。 The below code does connect to the server and read the first response successfully and print it to the console. 以下代码确实连接到服务器并成功读取了第一个响应,并将其打印到控制台。 The problem is after that it keeps reading the same response (the first response) over infinitely and printing it to the screen. 问题是,它不断读取无限的相同响应(第一个响应)并将其打印到屏幕上。

Does anyone see what I am messing up here? 有人看到我在这里搞砸了吗? I am trying to just watch for the next asynchronous message from the server and block until it comes. 我正在尝试仅监视来自服务器的下一个异步消息,并阻止它直到出现。 Also if anyone knows how to register to be notified when the next message shows up asynchronously so that I do not have to block wait that would be even better. 另外,如果有人知道如何注册,以便在下一条消息异步显示时得到通知,这样我就不必阻止等待,那会更好。

public void HttpSubscription() 
{
    byte[] result = new byte[10240];

    try
    {
        /* Generate the hard coded request data */
        final StringBuffer soap = new StringBuffer();
        soap.append("<s:Envelope><s:Body><SoapTest1>thing1</SoapTest1></s:Body></s:Envelope>");

        // to make HTTP Post request with HttpURLConnection
        URL url = new URL("http://192.168.1.110:80/services");
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();

        // then set some properties and make a request
        conn.setRequestMethod("POST");
        conn.setRequestProperty( "Content-type", "text/xml; charset=utf-8" );

        // Get a handle to the output stream 
        OutputStream OStream = conn.getOutputStream();

        // Write the soap data to the output stream
        OStream.write(soap.toString().getBytes());

        InputStream ResponseStream = conn.getInputStream();
        while (true)
        {
            int len = ResponseStream.read(result);
            String value = new String(result);
            System.out.println(value);
        }
    }
    catch (Exception e)
    {
        System.out.println(e);
    }

    return;
}

A bit old, but I decided to correct some blatant misinformation here. 有点老了,但是我决定在这里纠正一些公然的错误信息。

The answers stating that multiple responses for an HTTP request is not according to the HTTP specification are wrong! 指出针对HTTP请求的多个响应不符合HTTP规范的答案是错误的!

From RFC 2616 : RFC 2616

10 Status Code Definitions 10状态码定义

Each Status-Code is described below, including a description of which method(s) it can follow and any metainformation required in the response. 每个状态代码的说明如下,包括对它可以采用的方法的说明以及响应中所需的任何元信息。

10.1 Informational 1xx 10.1信息性1xx

This class of status code indicates a provisional response, consisting only of the Status-Line and optional headers, and is terminated by an empty line. 此类状态码表示仅由状态行和可选标头组成的临时响应,并以空行终止。 There are no required headers for this class of status code. 此类状态码没有必需的标题。 Since HTTP/1.0 did not define any 1xx status codes, servers MUST NOT send a 1xx response to an HTTP/1.0 client except under experimental conditions. 由于HTTP / 1.0没有定义任何1xx状态代码,因此服务器必须禁止在实验条件下向HTTP / 1.0客户端发送1xx响应。

A client MUST be prepared to accept one or more 1xx status responses prior to a regular response, even if the client does not expect a 100 (Continue) status message. 即使客户端不希望收到100(继续)状态消息,客户端也必须准备在接受常规响应之前接受一个或多个1xx状态响应。 Unexpected 1xx status responses MAY be ignored by a user agent. 用户代理可能会忽略意外的1xx状态响应。

What you have described is not HTTP, it is something else. 您所描述的不是HTTP,而是其他东西。 You might be able to get your server to implement it, you might not. 您也许能够使服务器实现它,但可能没有。 But expecting HttpURLConnection to understand something that violates the HTTP protocol is asking a bit much, don't you think? 但是,期望HttpURLConnection理解一些违反HTTP协议的内容会产生很多问题,您不觉得吗?

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

相关问题 使用HttpURLConnection的多个Http请求和响应 - Multiple Http requests and responses using HttpURLConnection Java如何始终侦听来自异步服务器套接字的响应 - Java how to always listen for responses from async server socket Java-HttpURLConnection到立交服务器 - Java - HttpURLConnection to overpass server 服务器未从多个客户端(Java套接字)接收数据 - server doesn't receive data from multiple clients (java sockets) 如何通过Java Socket发送命令并接收对OSGi控制台的响应? - How to send commands and receive responses to OSGi console via a Java Socket? Java AJAX服务器,如何压缩JSON响应 - Java AJAX server, how to zip JSON responses RHEL5.5中的Java 6 HTTPURLConnection和Project Server NTLM身份验证 - Java 6 HTTPURLConnection and Project Server NTLM Authentication from RHEL5.5 如何在 Java 中实现多线程 HttpURLConnection - How to MultiThread HttpURLConnection in Java 如何在Java Server中从Apple Push Notifications Server可靠而有效地读取所有错误响应? - How to reliably and efficiently read all error responses from Apple Push Notifications Server in a Java Server? 无法接收SOAP响应,因此从HttpURLConnection抛出java.io.FileNotFoundException - Unable to receive SOAP Response, java.io.FileNotFoundException is thrown from HttpURLConnection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM