Java HttpURLConnection如何接收来自服务器的多个响应

发布于 2024-09-29 21:14:54 字数 1527 浏览 4 评论 0原文

我正在尝试使用 POST 消息连接到服务器,要求服务器订阅我。然后,服务器将保持 http 连接打开并向我发送带有实时状态的异步消息,直到我请求取消订阅或自己关闭连接。我无法读取来自服务器的这些后续响应。下面的代码确实连接到服务器并成功读取第一个响应并将其打印到控制台。问题在于,它会无限地读取相同的响应(第一个响应)并将其打印到屏幕上。

有人看到我在这里搞砸了什么吗?我试图监视来自服务器的下一条异步消息并阻止直到它到来。另外,如果有人知道如何注册以在下一条消息异步显示时收到通知,这样我就不必阻塞等待,那就更好了。

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;
}

I am attempting to connect to a server with a POST message asking the server to subscribe me. 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. 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花心好男孩 2024-10-06 21:14:54

有点旧了,但我决定在这里纠正一些公然的错误信息。

关于一个 HTTP 请求的多个响应不符合 HTTP 规范的答案是错误的!

来自 RFC 2616

10 个状态代码定义

下面描述了每个状态代码,包括它可以遵循的方法的描述以及响应中所需的任何元信息。

10.1 信息 1xx

此类状态代码表示临时响应,包括
仅包含状态行和可选标头,并以
空行。此类状态没有必需的标头
代码。由于 HTTP/1.0 没有定义任何 1xx 状态代码,服务器必须
不向 HTTP/1.0 客户端发送 1xx 响应,除非
实验条件。

客户端必须准备好接受一个或多个 1xx 状态响应
在定期回复之前,即使客户不期望得到 100
(继续)状态消息。意外的 1xx 状态响应可能是
被用户代理忽略。

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!

From RFC 2616:

10 Status Code Definitions

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

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.

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. Unexpected 1xx status responses MAY be
ignored by a user agent.

我也只是我 2024-10-06 21:14:54

你所描述的不是HTTP,而是其他东西。您也许可以让您的服务器实现它,也可能不行。但是期望 HttpURLConnection 理解违反 HTTP 协议的内容有点过分了,您不觉得吗?

What you have described is not HTTP, it is something else. 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文