简体   繁体   English

Java HTTP全双工

[英]Java HTTP full duplex

I have a RESTful web service running on Jersey (GlassFish 3.1.1). 我有一个在Jersey上运行的RESTful Web服务(GlassFish 3.1.1)。 This one's a bit unusual - it uses HTTP streaming. 这个有点不寻常 - 它使用HTTP流。 The client and server have a long running conversation in which they keep sending each other messages encoded as XML tags. 客户端和服务器有一个长时间运行的对话,在这些对话中,他们不断发送编码为XML标签的消息。 The main reason for doing this is so the server can push to the client. 这样做的主要原因是服务器可以推送到客户端。

I have a test client that uses HttpURLConnection . 我有一个使用HttpURLConnection的测试客户端。 The problem is that the either the client's OutputStream or server's InputStream are being closed prematurely. 问题是客户端的OutputStream或服务器的InputStream过早关闭。 When I try to send a bit of XML from the client, I get a ProtocolException thrown (cannot write output after reading input). 当我尝试从客户端发送一些XML时,我会抛出一个ProtocolException (读取输入后无法写入输出)。

When I test using WizTools RESTClient, I can post an XML request, but this tool doesn't allow for keeping the connection alive. 当我使用WizTools RESTClient进行测试时,我可以发布XML请求,但此工具不允许保持连接活动。 But at least I know the web service is functioning at least in this scenario. 但至少我知道Web服务至少在这种情况下正在运行。 (It's probably just fine in the streaming scenario; my guess is the HttpURLConnection is letting me down. (在流媒体场景中可能还不错;我的猜测是HttpURLConnection让我失望。

Is there a way to make the HttpURLConnection stay alive and give me the chance to actually send something to the OutputStream ? 有没有办法让HttpURLConnection保持活着并让我有机会实际向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 ? 而且,最重要的是,有没有办法允许它继续写入OutputStream即使它已经通过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). 看起来HttpURLConnection强制执行HTTP的传统用法,即客户端在完全发送请求(并关闭输出流)之前无法处理响应。 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. 我刚开始看Apache HttpClient,但我还没有找到任何表明它可能的东西,所以我不确定我是否在浪费时间。

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 . InputStream读取后,您不应该写更多数据,因为这不符合HTTP协议的性质,即Request-Response Check more on this here . 这里查看更多内容。

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

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