java - httpcomponents 没有按照文档工作

标签 java apache-httpclient-4.x

使用我在这里找到的代码:http://hc.apache.org/httpcomponents-client-ga/quickstart.html我创建了一个快速 HTTP 客户端应用程序。这是一个代码片段:

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
log.info("Logging in");
HttpResponse response;
try {
  response = httpclient.execute(httpGet);
}
catch (ClientProtocolException cpe) {
  String msg = "Error logging in with:" + rootUrl;
  log.error(msg, cpe);
  throw new BusinessRuleException(msg, cpe);
}
catch (IOException ioe) {
  String msg = "Error logging in with:" + rootUrl;
  log.error(msg, ioe);
  throw new BusinessRuleException(msg, ioe);
}

try {
  System.out.println(response.getStatusLine());
  HttpEntity entity = response.getEntity();
  // do something useful with the response body
  // and ensure it is fully consumed
  EntityUtils.consume(entity);
}
catch (IOException ioe) {
  String msg = "Error retrieving login response:" + rootUrl;
  log.error(msg, ioe);
  throw new BusinessRuleException(msg, ioe);
}
finally {
  httpGet.releaseConnection();
}

我得到这个异常:

java.lang.NoSuchMethodError: org.apache.http.client.methods.HttpGet.releaseConnection()V
    at com.xerox.tclg.juror.schedule.RestScheduleRunner.runDailyCheckout(RestScheduleRunner.java:93)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:276)
    at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:260)

我发现一些文档说releaseConnection()不是必需的,所以我删除了finally block 。然后我得到这个异常:

java.lang.NoSuchMethodError: org.apache.http.util.EntityUtils.consume(Lorg/apache/http/HttpEntity;)V
    at com.xerox.tclg.juror.schedule.RestScheduleRunner.runDailyCheckout(RestScheduleRunner.java:85)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:276)
    at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:260)

有人见过这个吗?

最佳答案

在这种情况下,java.lang.NoSuchMethodError 很可能意味着您的 Http 组件 JAR 文件之一的版本错误。

如果您能准确地找出“RestScheduleRunner.java:85”中发生的情况,可能会帮助您诊断问题。

关于java - httpcomponents 没有按照文档工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12839473/

相关文章:

java - 在JAVA中按顺序将多个mp3文件连接成1个mp3文件

java - Apache HttpClient 4.3.3 如何找到请求站点的目标IP

java - Apache HttpClient 连接配置

java - 收集日志 ZIP 并上传到 Http 服务,无需临时文件

java - Flink keyby 然后 window 然后聚合所有结果?

Java Swing GridBagLayout 在其他组件之间添加组件

java - 从异步线程访问Android Activity 堆栈?

java - 从 httpclient 3 转换为 httpclient 4(Cookie 策略)

java - 发送 HTTP POST 时出现 InvalidAuthenticityToken 异常

java - 错误“在 Junit 测试中没有公共(public) TestCase(字符串名称)或 TestCase()