1,配置maven
<!-- httpClient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.2</version>
</dependency>
2,编写脚本(testng自己上网资料配置,很简单)
package com.test.testInterface.httpClient;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
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;
public class MyHttpClient {
/*
* <!DOCTYPE html> <!--STATUS OK--><html>这段话开始就意味着请求成功
*/
@Test
public void test1() throws ClientProtocolException, IOException {
// 用来保存结果
String result;
// 新建一个请求对象
HttpGet get = new HttpGet("http://www.baidu.com");
// 新建一个httpClient 客户端
HttpClient client = new DefaultHttpClient();
// httpClient 执行请求对象并且返回结果
HttpResponse response = client.execute(get);
// 把结果以UTF-8形式存在result中
result = EntityUtils.toString(response.getEntity(), "utf-8");
// 输出结果
System.out.println("-------------------------");
System.out.println(result);
}
}
3,执行代码
![](https://img-blog.csdnimg.cn/20200204012552968.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2Mzc5NTk3,size_16,color_FFFFFF,t_70)
4,mock模拟接口代码参考
https://blog.csdn.net/qq_36379597/article/details/104163678