最近在学习java自动化测试,学习过程中不可谓不艰巨。
首先IDEA无法自动下载JAR包,目前暂时还没解决。
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.Test;
import java.io.IOException;
public class test {
@Test
public void test1() throws IOException {
String result;
HttpGet get = new HttpGet("http://www.baidu.com");
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(get);
result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
}
}
问题描述:
上述代码在编写时,HttpResponse 引入时引入了sun.net.www.httpClient这个库,无法识别到 org.apache.http.HttpResponse这个库(Cannot resolve symbol 'HttpResponse')
解决方案:查询了很多博主的方法后发现是我这边仅有httpclient-4.1.2.jar缺少了依赖包commons-codec-1.3.jar 和commons-logging-1.1.1.jar,把上述两个依赖包添加后果然没有报错了
commons-cli-1.2.jar commons-codec-1.9.jar commons-logging-1.2.jar fluent-hc-4.5.1.jar httpclient-4.5.1.jar httpclient-cache-4.5.1.jar httpclient-win-4.5.1.jar httpcore-4.4.3.jar httpcore-ab-4.4.3.jar httpcore-nio-4.4.3.jar httpmime-4.5.1.jar jna-4.1.0.jar jna-platform-4.1.0.jar问题分析
httpclient4.x的版本,需要引入httpclient,httpcore,common-logging这三个jar包。而我因为不能通过pom.xml中引入org.apache.httpcomponents,只能手工添加jar包。所以在引入的时候缺少了其他的依赖包,导致无法识别到HttpReponse。
org.apache.httpcomponents httpclient4.1.2
按照正常来说应该按照上面的添加依赖是不会出现我这个问题的。
最后,感谢这两篇大佬的答案,让我找到了解决方案
HttpClient用法--这一篇全了解(内含例子)_Franco的博客-CSDN博客_httpclient
无法导入import org.apache.http.client.HttpClient;而自动导入sun.net.www.httpClient_xianshigudu的博客-CSDN博客



