Question

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;
}
Was it helpful?

Solution

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?

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top