1.使用IDEA创建maven项目
- 打开IDEA后点击settings ,然后在VM Options内添加-DarchetypeCatalog=internal 运行参数
- File--new--project,选择moven
- 填写项目名称
- 配置maven,点击finish完成
- 等待下载完成,如果下载慢可以更换成阿里源镜像
添加阿里源 ,找到 上图setting.xml中的 <mirrors> </ mirrors>标签,在标签内部 添加内容如下:
<mirror>
<id>AliMaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
2.添加httpclient依赖
搜索httpclient maven
粘贴上图中的依赖到pom.xml下的dependencies标签下
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.0.1</version>
</dependency>
</dependencies>
保存后,会自动下载依赖
3.HttpClient使用
package main.java.demo1;
import org.apache.hc.client5.http.ClientProtocolException;
import org.apache.hc.client5.http.classic.methods.HttpDelete;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* @description:
* @author: ljx
* @time: 2020/6/20 17:28
*/
public class HttpTest {
public static void main(String[] args) throws IOException, ParseException {
String url = "https://www.baidu.com";
CloseableHttpClient client = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
//执行请求,获取响应
CloseableHttpResponse response = client.execute(httpGet);
//获取响应码
int responseCode = response.getCode();
System.out.println(responseCode);
//响应内容
String responseString = EntityUtils.toString(response.getEntity());
System.out.println(responseString);
//获取响应头信息
Header[] headers = response.getHeaders();
for (Header header : headers) {
System.out.print(header.getName());
System.out.println(header.getValue());
}
}
public static CloseableHttpResponse get(String url, HashMap<String, String> headermap) throws ClientProtocolException, IOException {
/**
* @description: 发送带请求头的get请求
* @param url 请求url
* @param headermap 请求头map
* @return: org.apache.hc.client5.http.impl.classic.CloseableHttpResponse
* @author: ljx
* @time: 2020/6/22 14:51
**/
//创建一个可关闭的HttpClient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
//创建一个HttpGet的请求对象
HttpGet httpget = new HttpGet(url);
//加载请求头到httpget对象
for (Map.Entry<String, String> entry : headermap.entrySet()) {
httpget.addHeader(entry.getKey(), entry.getValue());
}
//执行请求,相当于postman上点击发送按钮,然后赋值给HttpResponse对象接收
CloseableHttpResponse httpResponse = httpclient.execute(httpget);
return httpResponse;
}
public static CloseableHttpResponse post(String url, String askParam, HashMap<String, String> headsmap) throws IOException {
/**
* @description: 进行post请求
* @param url 请求的url
* @param askParam 请求参数,json字符串
* @param headsmap 请求头map
* @return: org.apache.hc.client5.http.impl.classic.CloseableHttpResponse
* @author: ljx
* @time: 2020/6/22 14:41
**/
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
//添加请求参数
httpPost.setEntity(new StringEntity(askParam));
//添加请求头信息
for (Map.Entry<String, String> entry : headsmap.entrySet()) {
httpPost.addHeader(entry.getKey(), entry.getValue());
}
//发起请求
CloseableHttpResponse response = client.execute(httpPost);
return response;
}
public static CloseableHttpResponse httppost(String url, String entityString, HashMap<String, String> headermap) throws ClientProtocolException, IOException {
/**
* @description:
* @param url
* @param entityString
* @param headermap
* @return: org.apache.hc.client5.http.impl.classic.CloseableHttpResponse
* @author: ljx
* @time: 2020/6/22 14:56
**/
//创建一个可关闭的HttpClient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
//创建一个HttpPost的请求对象
HttpPost httppost = new HttpPost(url);
//设置payload
httppost.setEntity(new StringEntity(entityString));
//加载请求头到httppost对象
for (Map.Entry<String, String> entry : headermap.entrySet()) {
httppost.addHeader(entry.getKey(), entry.getValue());
}
//发送post请求
CloseableHttpResponse httpResponse = httpclient.execute(httppost);
return httpResponse;
}
public CloseableHttpResponse delete(String url) throws ClientProtocolException, IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpDelete httpdel = new HttpDelete(url);
//发送put请求
CloseableHttpResponse httpResponse = httpclient.execute(httpdel);
return httpResponse;
}
}