简体   繁体   中英

Java HTTP full duplex

I have a RESTful web service running on Jersey (GlassFish 3.1.1). This one's a bit unusual - it uses HTTP streaming. The client and server have a long running conversation in which they keep sending each other messages encoded as XML tags. The main reason for doing this is so the server can push to the client.

I have a test client that uses HttpURLConnection . The problem is that the either the client's OutputStream or server's InputStream are being closed prematurely. When I try to send a bit of XML from the client, I get a ProtocolException thrown (cannot write output after reading input).

When I test using WizTools RESTClient, I can post an XML request, but this tool doesn't allow for keeping the connection alive. But at least I know the web service is functioning at least in this scenario. (It's probably just fine in the streaming scenario; my guess is the HttpURLConnection is letting me down.

Is there a way to make the HttpURLConnection stay alive and give me the chance to actually send something to the OutputStream ? And, on top of that, is there a way to allow it to keep writing to the OutputStream even after it has received data via the InputStream ?

Update:

It looks like HttpURLConnection is enforcing the conventional usage of HTTP, namely that a client cannot process the response until it has completely sent the request (and closed the output stream). Is there an alternative that will allow for the request and response to temporally overlap? I just started looking at Apache HttpClient, but I haven't yet found anything to indicate it's possible, so I'm not sure if I'm wasting my time.

You are not supposed to write more data after reading from the InputStream because this does not follow the nature of HTTP Protocol which is Request-Response . Check more on this here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM