刚写出来的,还未经测试,
HttpUtil.java
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
import mons.lang.StringUtils;
import mons.logging.Log;
import mons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.client.utils.URLEncodedUtils;
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.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
/**
* HTTP工具类
* 发送http/https协议get/post请求,发送map,json,xml,txt数据
* @author happyqing
* @since -04-08
*/
public final class HttpUtil {
private static Log log = LogFactory.getLog(HttpUtil.class);
/**
* 执行一个http/https get请求,返回请求响应的文本数据
*
* @param url请求的URL地址,可以带参数?param1=a&parma2=b
* @param headerMap请求头参数map,可以为null
* @param paramMap请求参数map,可以为null
* @param charset字符集
* @param pretty是否美化
* @return 返回请求响应的文本数据
*/
public static String doGet(String url, Map headerMap, Map paramMap, String charset, boolean pretty) {
//StringBuffer contentSb = new StringBuffer();
String responseContent = "";
// http客户端
CloseableHttpClient httpclient = null;
// Get请求
HttpGet httpGet = null;
// http响应
CloseableHttpResponse response = null;
try {
if(url.startsWith("https")){
httpclient = HttpsSSLClient.createSSLInsecureClient();
} else {
httpclient = HttpClients.createDefault();
}
// 设置参数
if (paramMap != null) {
List nvps = new ArrayList ();
for (Map.Entry entry : paramMap.entrySet()) {
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
//EntityUtils.toString(new UrlEncodedFormEntity(nvps, charset));
url = url + "?" + URLEncodedUtils.format(nvps, charset);
}
// Get请求
httpGet = new HttpGet(url); // HttpUriRequest httpGet
// 设置header
if (headerMap != null) {
for (Map.Entry entry : headerMap.entrySet()) {
httpGet.addHeader(entry.getKey(), entry.getValue());
}
}
// 发送请求,返回响应
response = httpclient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
//BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), charset));
// String line;
// while ((line = reader.readLine()) != null) {
// if (pretty)
// contentSb.append(line).append(System.getProperty("line.separator"));
// else
// contentSb.append(line);
// }
//reader.close();
responseContent = EntityUtils.toString(entity, charset);
}
// EntityUtils.consume(entity);
} catch (ClientProtocolException e) {
log.error(e.getMessage(), e);
} catch (IOException e) {
log.error(e.getMessage(), e);
} catch (ParseException e) {
log.error(e.getMessage(), e);
} finally {
try {
if(response!=null){
response.close();
}
if(httpGet!=null){
httpGet.releaseConnection();
}
if(httpclient!=null){
httpclient.close();
}
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
return responseContent;
}
/**
* 执行一个http/https post请求,返回请求响应的文本数据
*
* @param url请求的URL地址,可以带参数?param1=a&parma2=b
* @param headerMap请求头参数map,可以为null
* @param paramMap请求参数map,可以为null
* @param charset字符集
* @param pretty是否美化
* @return 返回请求响应的文本数据
*/
public static String doPost(String url, Map headerMap, Map paramMap, String charset, boolean pretty) {
//StringBuffer contentSb = new StringBuffer();
String responseContent = "";
// http客户端
CloseableHttpClient httpclient = null;
// Post请求
HttpPost httpPost = null;
// http响应
CloseableHttpResponse response = null;
try {
if(url.startsWith("https")){
httpclient = HttpsSSLClient.createSSLInsecureClient();
} else {
httpclient = HttpClients.createDefault();
}
// Post请求
httpPost = new HttpPost(url);
// 设置header
if (headerMap != null) {
for (Map.Entry entry : headerMap.entrySet()) {
httpPost.addHeader(entry.getKey(), entry.getValue());
}
}
// 设置参数
if (paramMap != null) {
List nvps = new ArrayList ();
for (Map.Entry entry : paramMap.entrySet()) {
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps, charset));
}
// 发送请求,返回响应
response = httpclient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
//BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), charset));
// String line;
// while ((line = reader.readLine()) != null) {
// if (pretty)
// contentSb.append(line).append(System.getProperty("line.separator"));
// else
// contentSb.append(line);
// }
//reader.close();
responseContent = EntityUtils.toString(entity, charset);
}
// EntityUtils.consume(entity);
} catch (UnsupportedEncodingException e) {
log.error(e.getMessage(), e);
} catch (ClientProtocolException e) {
log.error(e.getMessage(), e);
} catch (IOException e) {
log.error(e.getMessage(), e);
} catch (ParseException e) {
log.error(e.getMessage(), e);
} finally {
try {
if(response!=null){
response.close();
}
if(httpPost!=null){
httpPost.releaseConnection();
}
if(httpclient!=null){
httpclient.close();
}
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
return responseContent;
}
/**
* 执行一个http/https post请求, 直接写数据 json,xml,txt
*
* @param url请求的URL地址
* @param headerMap请求头参数map,可以为null
* @param contentjson或xml等字符串内容
* @param charset字符集
* @param pretty是否美化
* @return返回请求响应的文本数据
*/
public static String writePost(String url, Map headerMap, String content, String charset, boolean pretty) {
//StringBuffer contentSb = new StringBuffer();
String responseContent = "";
// http客户端
CloseableHttpClient httpclient = null;
// Post请求
HttpPost httpPost = null;
// http响应
CloseableHttpResponse response = null;
try {
if(url.startsWith("https")){
httpclient = HttpsSSLClient.createSSLInsecureClient();
} else {
httpclient = HttpClients.createDefault();
}
// Post请求
httpPost = new HttpPost(url);
// 设置header
if (headerMap != null) {
for (Map.Entry entry : headerMap.entrySet()) {
httpPost.addHeader(entry.getKey(), entry.getValue());
}
}
// 字符串Entity
StringEntity stringEntity = new StringEntity(content, charset);
stringEntity.setContentType("text/plain"); //application/json,text/xml,text/plain
httpPost.setEntity(stringEntity);
// 发送请求,返回响应
response = httpclient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
//BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), charset));
// String line;
// while ((line = reader.readLine()) != null) {
// if (pretty)
// contentSb.append(line).append(System.getProperty("line.separator"));
// else
// contentSb.append(line);
// }
//reader.close();
responseContent = EntityUtils.toString(entity, charset);
}
// EntityUtils.consume(entity);
} catch (UnsupportedEncodingException e) {
log.error(e.getMessage(), e);
} catch (ClientProtocolException e) {
log.error(e.getMessage(), e);
} catch (IOException e) {
log.error(e.getMessage(), e);
} catch (ParseException e) {
log.error(e.getMessage(), e);
} finally {
try {
if(response!=null){
response.close();
}
if(httpPost!=null){
httpPost.releaseConnection();
}
if(httpclient!=null){
httpclient.close();
}
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
return responseContent;
}
// 通过code获取openId
public static String getOpenId(String code) {
if (StringUtils.isNotEmpty(code)) {
String appid = PropertiesUtil.getProperty("wx.appid");
String secret = PropertiesUtil.getProperty("wx.appsecret");
String result = HttpUtil.doGet("https://api./sns/oauth2/access_token?appid=" + appid + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code", null, null, "UTF-8",true);
if (StringUtils.isNotEmpty(result)) {
JSONObject json = JSONObject.fromObject(result);
if (json.get("openid") != null) {
return json.get("openid").toString();
}
}
}
return "";
}
public static void main(String[] args) {
try {
String y = doGet("/life/tips.html", null, null, "GBK", true);
System.out.println(y);
// String accessToken = HttpUtil.doGet("https://api./cgi-bin/token?grant_type=client_credential&appid=appid&secret=secret", null, null, "UTF-8", true);
// System.out.println(accessToken);
// Map params = new HashMap();
// params.put("param1", "value1");
// params.put("json", "{\"aa\":\"11\"}");
// String j = doPost("http://localhost/uplat/manage/test.do?reqCode=add", null, params, "UTF-8", true);
// System.out.println(j);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
HttpsSSLClient.java
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import .ssl.HostnameVerifier;
import .ssl.SSLContext;
import .ssl.SSLSession;
import .ssl.TrustManager;
import .ssl.X509TrustManager;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
/**
* 创建httpsclient
* @author happyqing
* @since -04-07
*/
public class HttpsSSLClient {
/**
* 获取Https 请求客户端
* @return
*/
public static CloseableHttpClient createSSLInsecureClient() {
SSLContext sslcontext = createSSLContext();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new HostnameVerifier() {
@Override
public boolean verify(String paramString, SSLSession paramSSLSession) {
return true;
}
});
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
return httpclient;
}
/**
* 获取初始化SslContext
* @return
*/
private static SSLContext createSSLContext() {
SSLContext sslcontext = null;
try {
sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, new TrustManager[] {new TrustAnyTrustManager()}, new SecureRandom());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
return sslcontext;
}
/**
* 自定义静态私有类
*/
private static class TrustAnyTrustManager implements X509TrustManager {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[] {};
}
}
}
参考:
httpclient4.x处理https协议请求
/code/snippet_2376242_50816#74195
HttpClient_4 用法 由HttpClient_3 升级到 HttpClient_4 必看
/loveyakamoz/archive//07/21/2113252.html
用httpclient4.x 发送http get post请求。
/sunny243788557/article/details/8106265
HttpClient-4.3.X 中get和post方法使用
/520playboy/p/6344940.html
HttpGet &&HttpPost方法发送header,params, Content
/fhlkm/article/details/7844509
如果觉得《httpf发送 json_Java用HttpClient4发送http/https协议get/post请求 发送map json xml txt数据...》对你有帮助,请点赞、收藏,并留下你的观点哦!