Java HttpURLConnection如何接收来自服务器的多个响应
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有点旧了,但我决定在这里纠正一些公然的错误信息。
关于一个 HTTP 请求的多个响应不符合 HTTP 规范的答案是错误的!
来自 RFC 2616:
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:
你所描述的不是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?