即使设置了超时,HttpClient 3.1也会挂起而不会抛出异常

时间:2015-03-26 20:08:49

标签: java apache-commons-httpclient

我正在为仅支持Java 1.4的Windows Mobile设备编写应用程序。所以我正在使用旧的HttpClient 3.1。超时大部分时间都可以正常工作,但随机会挂起整个应用程序一分钟尝试发送请求。我试图将超时设置如下:

HttpClient client = new HttpClient();
StringRequestEntity requestEntity = new StringRequestEntity(myJSONString,"application/json","UTF-8");
PostMethod postMethod = new PostMethod("http://localhost:8080/rest/");
postMethod.setRequestEntity(requestEntity);
postMethod.setRequestHeader(new Header("Accept","application/json"));
postMethod.setRequestHeader(new Header("Content-Type","application/json"));
try
{
  client.setTimeout(2000);
  client.setConnectionTimeout(2000);
  int statusCode = client.executeMethod(postMethod);  
  if (statusCode == HttpStatus.SC_OK) {
    System.out.println("Success!");
  }
  else
    System.out.println("Error");
}
catch(Exception e)
{
  e.printStackTrace();
}

有什么想法吗?感谢。

0 个答案:

没有答案