Httpclient工具类
一、引入依赖包
org.apache.httpcomponents httpclient org.apache.httpcomponents httpmime com.alibaba fastjson 1.2.62
二、工具类
package com.sskd.drm.utils;
import java.util.*;
import com.alibaba.fastjson.JSON;
import org.apache.http.*;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.ssl.SSLContexts;
import javax.net.ssl.SSLContext;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import java.io.*;
import java.io.File;
/**
* @name HttpClientUtils
* @Description Httpclient工具类
* @author Mr.bai
* @time 2020/5/15 13:06
* @version 1.0
*/
public class HttpClientUtils {
/**
* get请求
* @param url url地址
* @param parameters 参数map集
* @param headers header集
* @return
*/
public static String getrequest(String url,Map parameters,Map headers){
String basicUrl=url;
String result=null;
CloseableHttpClient httpclient=getignoreSSLClient();
List formData=new ArrayList<>();
try {
//参数分离
if(!url.contains("=")) {
if(parameters !=null && parameters.size()>0) {
for(Map.Entry entry:parameters.entrySet()) {
String k =entry.getKey();
String v=entry.getValue();
formData.add(new BasicNameValuePair(k, v));
}
}
String urlencoded =EntityUtils.toString(new UrlEncodedFormEntity(formData, Consts.UTF_8));
if(basicUrl.contains("?")) {
basicUrl += urlencoded;
}else {
basicUrl+= "?"+urlencoded;
}
}
//纯url
HttpGet get=new HttpGet(basicUrl);
if(headers !=null && headers.size()>0) {
for(Map.Entry entry:headers.entrySet()) {
get.setHeader(entry.getKey(),entry.getValue());
}
}else {
get.setHeader(null);
}
HttpResponse response=httpclient.execute(get);
return getResult(response);
}catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(httpclient!=null) {
httpclient.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* post(application/form-data)请求
* @param url url地址
* @param body 参数集 如果参数内需要传输文件,传File
* @param headers header配置
* @return
*/
public static String postformData(String url,Map body,Map headers){
CloseableHttpClient httpclient = getignoreSSLClient();
try {
HttpPost post =new HttpPost(url);
//请求表单参数
MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();
if (body != null && body.size() > 0){
for (String key : body.keySet()) {
if (body.get(key) instanceof File){
File file = (File) body.get(key);
FileBody fileBody = new FileBody(file);
reqEntity.addPart(key, fileBody);
}else {
reqEntity.addTextBody(key, body.get(key).toString(),ContentType.TEXT_PLAIN);
}
}
System.out.println("post(application/form-data)请求,构造参数完毕!");
}
post.setEntity(reqEntity.build());
//设置header
if(headers !=null && headers.size()>0) {
for (String key : headers.keySet()) {
post.setHeader(key,headers.get(key));
}
}else {
post.setHeader(null);
}
HttpResponse response = httpclient.execute(post);
return getResult(response);
}catch(Exception e ) {
System.err.println("请求出错\n错误信息:" +e.getMessage());
}finally {
try {
if(httpclient!=null) {
httpclient.close();
}
}catch(Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* post(application/json)
* @param url url地址
* @param body 参数集
* @param headers header配置
* @return
*/
public static String postJsonrequest(String url , Map