我正在使用HTTPClient构建测试项目以测试静态服务。这是到目前为止我成功用于在GET上获得200 OK响应的代码。但是,我在.releaseConnection上遇到错误,HTTPClient文档说该错误很重要。
码:
public class Container {
public String tryGet() {
String getResult = null;
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://test.url");
try {
HttpResponse response1 = httpclient.execute(httpGet);
System.out.println(response1.getStatusLine());
getResult = response1.getStatusLine().toString();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
httpGet.releaseConnection();
}
return getResult;
}
}
运行junit测试时,出现与.releaseConnection()相关的以下错误:
java.lang.NoSuchMethodError: org.apache.http.client.methods.HttpGet.releaseConnection()V
at com.qa.Container.tryGet(CreativeContainer.java:49)
at com.qa.SandboxTest.test(SandboxTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:44)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
我知道代码很丑陋。我才刚刚开始。
最佳答案
在版本中添加了HttpRequestBase#releaseConnection()方法。
可能您有地狱。如果编译器没有编译错误,则可以在运行时加载该类的另一个版本。真正的原因取决于您的构建工具。如果您使用的是Maven / Gradle,则可能存在一些传递依赖。
因此,实际上HttpRequestBase#releaseConnection()方法只是将代理代理调用到AbstractExecutionAwareRequest#reset()。而AbstractExecutionAwareRequest#reset()只是取消方法的执行。可能不是您所需要的。
执行和释放httpClient资源的正确方法是关闭httpResponse,这意味着您可以在httpClient的内部threadPool中释放Thread。
private static String makeRequest(HttpUriRequest httpRequest) throws IOException {
CloseableHttpResponse httpResponse = httpClient.execute(httpRequest);
try {
HttpEntity httpEntity = httpResponse.getEntity();
StringWriter writer = new StringWriter();
IOUtils.copy(httpEntity.getContent(), writer, "UTF-8");
return writer.toString();
} finally {
httpResponse.close();
}
}
关于java - .releaseConnection的HTTPClient-NoSuchMethodError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13735367/