1.http请求第三方接口工具类
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @Date 2022/07/25
* @Author 程序猿~~
*
*/
public class HttpClientUtil {
private static final String DEFAULT_ENCODING = "UTF-8";
/**
* GET方式请求
*
* @param uri
* 服务器的uri要用物理IP或域名,不识别localhost或127.0.0.1形式!
* @return
* @throws IOException
* @throws ClientProtocolException
*/
public static String get(String uri) throws ClientProtocolException,
IOException {
HttpGet httpGet = new HttpGet(uri);
HttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(httpGet);
int statusCode;
if ((statusCode = httpResponse.getStatusLine().getStatusCode()) == 200) {
String result = EntityUtils.toString(httpResponse.getEntity());
return EntityUtils.toString(httpResponse.getEntity());
}
throw new IOException("status is " + statusCode);
}
/**
* GET方式请求
*
* @param uri
* 服务器的uri要用物理IP或域名,不识别localhost或127.0.0.1形式!
* @return
* @throws IOException
* @throws ClientProtocolException
*/
public static String get(String uri, Map<String, String> paramMap)
throws ClientProtocolException, IOException {
StringBuilder sb = new StringBuilder(uri);
if (paramMap != null) {
boolean isBegin = true;
for (String key : paramMap.keySet()) {
if (isBegin) {
sb.append("?").append(key).append("=")
.append(paramMap.get(key));
isBegin = false;
} else {
sb.append("&").append(key).append("=")
.append(paramMap.get(key));
}
}
}
HttpGet httpGet = new HttpGet(sb.toString());
HttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(httpGet);
int statusCode;
if ((statusCode = httpResponse.getStatusLine().getStatusCode()) == 200) {
// String result = EntityUtils.toString(httpResponse.getEntity());
return EntityUtils.toString(httpResponse.getEntity());
}
throw new IOException("status is " + statusCode);
}
/**
* GET方式请求https
*
* @param uri
* 服务器的uri要用物理IP或域名,不识别localhost或127.0.0.1形式!
* @return
* @throws IOException
* @throws ClientProtocolException
*/
public static String httpsGet(String uri, String keyFile, String keyPwd)
throws Exception {
HttpGet httpGet = new HttpGet(uri);
HttpClient httpClient = newHttpsClient(keyFile, keyPwd);
HttpResponse httpResponse = httpClient.execute(httpGet);
int statusCode;
if ((statusCode = httpResponse.getStatusLine().getStatusCode()) == 200) {
// String result = EntityUtils.toString(httpResponse.getEntity(),
// DEFAULT_ENCODING);
return EntityUtils.toString(httpResponse.getEntity(),
DEFAULT_ENCODING);
}
throw new IOException("status is " + statusCode);
}
/**
* POST方式请求
*
* @param uri
* 服务器的uri要用物理IP或域名,不识别localhost或127.0.0.1形式!
* @param paramMap
* @return
* @throws IOException
* @throws ClientProtocolException
*/
public static String post(String uri, Map<String, Object> paramMap)
throws ClientProtocolException, IOException {
HttpPost httpPost = new HttpPost(uri);
JSONObject param2= new JSONObject();
if (paramMap != null) {
for (String key : paramMap.keySet()) {
param2.put(key, paramMap.get(key));
}
StringEntity stringEntity = new StringEntity(param2.toString());
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
}
HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);
int statusCode;
if ((statusCode = httpResponse.getStatusLine().getStatusCode()) == 200) {
return EntityUtils.toString(httpResponse.getEntity(),
DEFAULT_ENCODING);
}
throw new IOException("status is " + statusCode);
}
/**
* POST方式请求,UTF-8编码发送内容
*
* @param uri
* @param paramMap
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String postEncoding(String uri, Map<String, String> paramMap)
throws ClientProtocolException, IOException {
HttpPost httpPost = new HttpPost(uri);
List<NameValuePair> params = new ArrayList<NameValuePair>();
if (paramMap != null) {
for (String key : paramMap.keySet()) {
params.add(new BasicNameValuePair(key, paramMap.get(key)));
}
httpPost.setEntity(new UrlEncodedFormEntity(params,
DEFAULT_ENCODING));
}
HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);
int statusCode;
if ((statusCode = httpResponse.getStatusLine().getStatusCode()) == 200) {
return EntityUtils.toString(httpResponse.getEntity(),
DEFAULT_ENCODING);
}
throw new IOException("status is " + statusCode);
}
/**
* POST方式请求
*
* @param uri
* 服务器的uri要用物理IP或域名,不识别localhost或127.0.0.1形式!
* @param paramMap
* @param headers
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String post(String uri, Map<String, String> paramMap,
Map<String, String> headers) throws ClientProtocolException,
IOException {
HttpPost httpPost = new HttpPost(uri);
if (headers != null) {
for (String key : headers.keySet()) {
httpPost.setHeader(key, headers.get(key));
}
}
List<NameValuePair> params = new ArrayList<NameValuePair>();
if (paramMap != null) {
for (String key : paramMap.keySet()) {
params.add(new BasicNameValuePair(key, paramMap.get(key)));
}
httpPost.setEntity(new UrlEncodedFormEntity(params,
DEFAULT_ENCODING));
}
HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);
int statusCode;
if ((statusCode = httpResponse.getStatusLine().getStatusCode()) == 200) {
return EntityUtils.toString(httpResponse.getEntity(),
DEFAULT_ENCODING);
}
throw new IOException("status is " + statusCode);
}
/**
* POST方式请求https
*
* @param uri
* 服务器的uri要用物理IP或域名,不识别localhost或127.0.0.1形式!
* @param paramMap
* @return
* @throws IOException
* @throws ClientProtocolException
*/
public static String httpsPost(String uri, Map<String, String> paramMap,
String keyFile, String keyPwd) throws ClientProtocolException,
IOException, Exception {
HttpPost httpPost = new HttpPost(uri);
List<NameValuePair> params = new ArrayList<NameValuePair>();
if (paramMap != null) {
for (String key : paramMap.keySet()) {
params.add(new BasicNameValuePair(key, paramMap.get(key)));
}
httpPost.setEntity(new UrlEncodedFormEntity(params,
DEFAULT_ENCODING));
}
HttpResponse httpResponse = newHttpsClient(keyFile, keyPwd).execute(
httpPost);
int statusCode;
if ((statusCode = httpResponse.getStatusLine().getStatusCode()) == 200) {
return EntityUtils.toString(httpResponse.getEntity());
}
throw new IOException("status is " + statusCode);
}
/*
* 新建httpsClient
*/
private static HttpClient newHttpsClient(String keyFile, String keyPwd)
throws Exception {
KeyStore trustStore = KeyStore.getInstance("BKS");
trustStore.load(new FileInputStream(new File(keyFile)),
keyPwd.toCharArray());
SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
Scheme sch = new Scheme("https", socketFactory, 8443);
HttpClient client = new DefaultHttpClient();
client.getConnectionManager().getSchemeRegistry().register(sch);
return client;
}
/**
* post请求发送传输obj对象
*
* @param uri
* @param
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String postOfObject(String uri, Object obj)
throws ClientProtocolException, IOException {
String params = JSON.toJSONString(obj);
return HttpClientUtil.postJson(uri, params);
}
/**
* 针对http传输json数据处理
*
* @param uri
* @param parameters
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String postJson(String uri, String parameters)
throws ClientProtocolException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (parameters != null) {
StringEntity entity = new StringEntity(parameters);
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
}
HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);
int statusCode;
if ((statusCode = httpResponse.getStatusLine().getStatusCode()) == 200) {
return EntityUtils.toString(httpResponse.getEntity(),
DEFAULT_ENCODING);
}
throw new IOException("status is " + statusCode);
}
/**
* 针对http传输json数据处理
*
* @param uri
* @param parameters
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String postByJson(String uri, String parameters)
throws ClientProtocolException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (parameters != null) {
StringEntity entity = new StringEntity(parameters,"UTF-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
}
HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);
int statusCode;
if ((statusCode = httpResponse.getStatusLine().getStatusCode()) == 200) {
return EntityUtils.toString(httpResponse.getEntity(),
DEFAULT_ENCODING);
}
throw new IOException("status is " + statusCode);
}
}
2.http通过post请求第三方接口
主要阐述一下通过post请求第三方接口的实际操作,该请求方式在实际工作中较为常用(个人见解而已,勿喷!!!)
/**
* @Description: http请求
* @Param: [map]
* @Return: java.lang.String
* @Author: 程序猿~~
* @Date: 2022/7/25
*/
public String httpMethod(Map map){
// 第三方接口地址
String url = "http://ip:端口号/要请求的接口";
String retStr ="";
try{
// 这里就是请求的上面的工具类中的post的请求方法
retStr = HttpClientUtil.post(url,map);
}catch (Exception e){
e.printStackTrace();
System.out.println(e.getStackTrace());
return "";
}
return retStr;
}
3.具体业务代码操作(此处例子,仅供参考)
public String test(@RequestBody Test test){
// http接口请求参数
Map<String,Object> paramMap = new HashMap();
// 根据实际业务需求传参
paramMap.put("aaa",test.getAaa());
paramMap.put("bbb",test.getBbb());
String retStr = httpMethod(paramMap);
return retStr;
}
以上三步操作基本能够满足日常开发所用