1.采用maven导入jar包;
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
2.采用httpClient发包实现get,post请求,
注:这里在main方法中演示,url和路径,参数 我这里只写了个格式,数据也是写死的,具体的看大家自己的需要从外部获取或传入地址和参数进行拼接
数据获取的方式:数据库,文件(json文件,xml文件,Excel文件等,)
2.1:GET请求
package com.devapi.hibuting.demo;
import java.util.Arrays;
import org.apache.http.Header;
import org.apache.http.HttpRequest;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class demo {
public static void main(String[] args) {
// get请求的方式,https://域名/路径?k1=v1&k2=v2
String url = "https://xxx.xxx.com/xxx" + "?" + "k1=v1&k2=v2";
try {
// 创建httpGet请求
HttpGet httpGet = new HttpGet(url);
CloseableHttpClient httpClient = HttpClients.createDefault();
// 准备请求头 (这一步看需要,若接口有添加请求头信息则直接添加,若不需要 可直接略过)
HttpRequest httpRequest = httpGet;
httpRequest.addHeader("token1", "xxxxx1");
httpRequest.addHeader("token2", "xxxxx2");
// 发送请求
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
int httpCode = httpResponse.getStatusLine().getStatusCode();
Header[] header = httpResponse.getAllHeaders();
String result = EntityUtils.toString(httpResponse.getEntity());
System.out.println("接口的响应状态码" + httpCode);
System.out.println("接口的响应头" + Arrays.toString(header));
System.out.println("接口的响应报文" + result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.2:POST请求
package com.devapi.hibuting.demo;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import org.apache.http.Header;
import org.apache.http.HttpRequest;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSONObject;
public class demo {
public static void main(String[] args) {
//请求的url
String url ="https://xxx.xxx.com";
//接口请求的数据,拼接成json字符串
String jsonStr ="{\"xx\":\"xx\",\"xx\":\"xx\"}";
//接口请求的类型:json,form
String submitType = "form";
try {
HttpPost httpPost = new HttpPost(url);
if ("form".equalsIgnoreCase(submitType)) {
httpPost.addHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"));
String params =jointBodyParam(jsonStr);
httpPost.setEntity(new StringEntity(params, "UTF-8"));
}else if("json".equalsIgnoreCase(submitType)){
httpPost.addHeader(new BasicHeader("Content-Type", "application/json;charset=UTF-8"));
httpPost.setEntity(new StringEntity(jsonStr,"UTF-8"));
}
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
int httpCode = httpResponse.getStatusLine().getStatusCode();
Header[] header = httpResponse.getAllHeaders();
String result = EntityUtils.toString(httpResponse.getEntity());
System.out.println("接口的响应状态码:"+httpCode);
System.out.println("接口的响应头:"+Arrays.toString(header));
System.out.println("接口的响应报文"+result);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String jointBodyParam(String jsonStr) {
Map<String, Object> map = (Map<String, Object>) JSONObject.parse(jsonStr);
Set<String> keySet = map.keySet();
StringBuffer sBuffer = new StringBuffer();
String[] keyArr = new String[keySet.size()];
keySet.toArray(keyArr);
for (int i = 0; i < keyArr.length; i++) {
String paramName = keyArr[i];
// 第一个参数前用 ? 来拼接
sBuffer.append(paramName).append("=").append(map.get(paramName)).append("&");
}
// 截取字符串, 从0开始,到(最后一个&出现的位置); 是为了截掉最后一个&
return sBuffer.substring(0, sBuffer.lastIndexOf("&"));
}
}