HttpURLConnection POST 请求不返回任何值
我在处理不返回数据的 HTTP URL 连接时遇到问题。我正在使用的代码如下...它基本上是一个消息传递客户端,此方法获取发送给虚假用户(机器人)的任何消息,然后我能够操纵消息,查找关键字并从机器人。
public void getMessages(String bot)
{
xml = "" +
"xmlMessage=<message type=\"comet.get.message.updates\" "
+ "id=\"" + bot + "\" "
+ "password=\"" + password + "\" />";
// Replace spaces (partial url encode).
xml = xml.replace(" ", "%20");
String serverResponse = "";
try
{
// Build URL
url = new URL(botUrl);
request = (HttpURLConnection)url.openConnection();
// Set the Request Method.
request.setRequestMethod("POST");
request.setDoInput(true);
request.setDoOutput(true);
request.setUseCaches(false);
request.setAllowUserInteraction(false);
request.setRequestProperty("Content-type", "text/xml; charset=" + "UTF-8");
out = request.getOutputStream();
writer = new OutputStreamWriter(out, "UTF-8");
writer.write(xml);
writer.close();
String temp = "";
buff = new BufferedReader ( new InputStreamReader ( request.getInputStream() ) );
while ( (temp = buff.readLine()) != null )
{
serverResponse = serverResponse + temp;
}
// XML RESPONSE EXAMPLE
// xml = "- <message type=\"comet.message.updates\" id=\"[email protected]\" count=\"2\">z" +
// "- <contact id=\"jy5740\" />" +
// "<statement text=\"test\" from=\"jy5740\" />" +
// "<statement text=\"testing 123\" from=\"jy5740\" />" +
// "</contact>" +
// "</message>";
}
catch (MalformedURLException ex)
{
System.out.println("Bad URL: " + ex);
}
catch (IOException ex)
{
System.out.println("Connection error: " + ex);
}
// do stuff with the serverResponse string
如果在方法调用时尚未收到消息,则该方法可以正常工作。问题是自上次检查以来没有任何消息。该方法只是停留在 while 循环中,直到一条消息发送到锁定我的应用程序的机器人。如何判断服务器是否没有响应?
I am having an issue handling HTTP URL Connections that do not return data. The code I am using is below... It is basically a messaging client, and this method gets any messages sent to a fake user (bot) and then I am able to manipulate the messages, look for key words, and respond from the bot.
public void getMessages(String bot)
{
xml = "" +
"xmlMessage=<message type=\"comet.get.message.updates\" "
+ "id=\"" + bot + "\" "
+ "password=\"" + password + "\" />";
// Replace spaces (partial url encode).
xml = xml.replace(" ", "%20");
String serverResponse = "";
try
{
// Build URL
url = new URL(botUrl);
request = (HttpURLConnection)url.openConnection();
// Set the Request Method.
request.setRequestMethod("POST");
request.setDoInput(true);
request.setDoOutput(true);
request.setUseCaches(false);
request.setAllowUserInteraction(false);
request.setRequestProperty("Content-type", "text/xml; charset=" + "UTF-8");
out = request.getOutputStream();
writer = new OutputStreamWriter(out, "UTF-8");
writer.write(xml);
writer.close();
String temp = "";
buff = new BufferedReader ( new InputStreamReader ( request.getInputStream() ) );
while ( (temp = buff.readLine()) != null )
{
serverResponse = serverResponse + temp;
}
// XML RESPONSE EXAMPLE
// xml = "- <message type=\"comet.message.updates\" id=\"[email protected]\" count=\"2\">z" +
// "- <contact id=\"jy5740\" />" +
// "<statement text=\"test\" from=\"jy5740\" />" +
// "<statement text=\"testing 123\" from=\"jy5740\" />" +
// "</contact>" +
// "</message>";
}
catch (MalformedURLException ex)
{
System.out.println("Bad URL: " + ex);
}
catch (IOException ex)
{
System.out.println("Connection error: " + ex);
}
// do stuff with the serverResponse string
The method works perfectly if there are messages that have not been received at the time of the method call. The problem is when there have not been any messages since the last check. The method just stays in the while loop until a message is sent to the bot locking my app. How do I determine if there was no response from the server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
comet 的要点是“长轮询”——您发出请求,直到收到真正的响应或超时后,请求才会完成。换句话说,如果没有消息,我希望对 readLine 的调用会阻塞很长时间。
如果您需要发出一个不会需要很长时间才能超时的请求,那么您需要在某处指定超时(可能在 HTTP 级别,也可能在 XML 中)内容)或使用不同的调用来开始 - 很可能有一种不同类型的消息用于非挂起请求。
The point of comet is "long polling" - that you make a request and it won't complete until there's a real response, or until it times out. In other words, if there are no messages I'd expect the call to
readLine
to block for a long time.If you need to make a request which won't take a long time to time-out, you'll either need to specify a time-out somewhere (possibly at the HTTP level, possibly within the XML content) or use a different call to start with - there may well be a different kind of message used for non-hanging requests.
不知道,但这是我的 HTTP Post 代码:
No idea, but here is my code for HTTP Post: