Java HttpURLConnection POST请求不返回任何值
我在处理不返回数据的HTTP URL连接时遇到问题。我使用的代码如下。。。它基本上是一个消息传递客户机,这个方法获取发送给假用户(bot)的任何消息,然后我可以处理这些消息,查找关键字,并从bot响应Java HttpURLConnection POST请求不返回任何值,java,webrequest,httpurlconnection,Java,Webrequest,Httpurlconnection,我在处理不返回数据的HTTP URL连接时遇到问题。我使用的代码如下。。。它基本上是一个消息传递客户机,这个方法获取发送给假用户(bot)的任何消息,然后我可以处理这些消息,查找关键字,并从bot响应 public void getMessages(String bot) { xml = "" + "xmlMessage=<message type=\"comet.get.message.updates\" " + "id=\"" + 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=\"chalkboard.status@fdq.att.com\" 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
public void getMessages(字符串bot)
{
xml=“”+
“xmlMessage=”;
//替换空格(部分url编码)。
xml=xml.replace(“,“%20”);
字符串serverResponse=“”;
尝试
{
//构建URL
url=新url(botUrl);
请求=(HttpURLConnection)url.openConnection();
//设置请求方法。
请求。setRequestMethod(“POST”);
request.setDoInput(true);
请求。设置输出(true);
请求。setUseCaches(false);
request.setAllowUserInteraction(false);
setRequestProperty(“内容类型”、“文本/xml;字符集=“+”UTF-8”);
out=request.getOutputStream();
writer=新的输出流writer(out,“UTF-8”);
write.write(xml);
writer.close();
字符串temp=“”;
buff=新的BufferedReader(新的InputStreamReader(request.getInputStream());
而((temp=buff.readLine())!=null)
{
serverResponse=serverResponse+temp;
}
//XML响应示例
//xml=“-z”+
// "- " +
// "" +
// "" +
// "" +
// "";
}
捕获(格式错误)
{
System.out.println(“错误URL:+ex”);
}
捕获(IOEX异常)
{
System.out.println(“连接错误:+ex”);
}
//使用serverResponse字符串执行操作
如果在方法调用时没有收到任何消息,则该方法可以正常工作。问题是自上次检查以来没有收到任何消息。该方法只是停留在while循环中,直到有消息发送到锁定我的应用程序的bot。如何确定服务器是否没有响应?Tcomet的要点是“长轮询”——你发出一个请求,直到有一个真正的响应或超时才会完成。换句话说,如果没有消息,我希望对readLine
的调用会被阻塞很长一段时间
如果您需要发出一个不会花费很长时间超时的请求,那么您需要在某个地方指定一个超时(可能在HTTP级别,可能在XML内容中)或者使用不同的调用开始-可能有不同类型的消息用于非挂起请求。不知道,但以下是我的HTTP Post代码: HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(UPLOAD_URL); MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); try { reqEntity.addPart("param1", new StringBody("yes")); reqEntity.addPart("param2", new StringBody("no")); httppost.setEntity(reqEntity); LOG.debug("executing request " + httppost.getRequestLine()); HttpResponse response = httpclient.execute(httppost); HttpEntity resEntity = response.getEntity(); String urlImageShack = null; if (resEntity != null) { // XML returned by Imageshack String page = EntityUtils.toString(resEntity); LOG.debug("It return: " + page); } HttpClient HttpClient=新的DefaultHttpClient(); HttpPost HttpPost=新的HttpPost(上传URL); MultipartEntity reqEntity=新的MultipartEntity(HttpMultipartMode.BROWSER_兼容); 试一试{ REQUENTITY.addPart(“参数1”,新的StringBody(“是”); 要求实体添加部分(“参数2”,新的StringBody(“编号”); httppost.setEntity(reqEntity); debug(“正在执行请求”+httppost.getRequestLine()); HttpResponse response=httpclient.execute(httppost); HttpEntity当前性=response.getEntity(); 字符串urlImageShack=null; if(最近性!=null){ //Imageshack返回的XML 字符串页=EntityUtils.toString(最近性); LOG.debug(“它返回:+页);
}好吧,我完全忽略了这一点。非常感谢你为我指明了正确的方向!