我正在尝试使用 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;
}