2024年2月27日发(作者:)
import ortedEncodingException;import oder;import agementException;import AlgorithmException;import .X509Certificate;import ist;import ;import ;
import text;import anager;import .X509TrustManager;
import Utils;import sponse;import luePair;import ient;import odedFormEntity;import lete;import t;import st;import t;import ConnectionManager;import ;import Registry;import ketFactory;import rayEntity;import Entity;import tHttpClient;import ameValuePair;
public class HttpUtils {
/** * get *
* @param host * @param path * @param method * @param headers * @param querys * @return * @throws Exception */ public static HttpResponse doGet(String host, String path, String method,
Map
Map
HttpClient httpClient = wrapClient(host);
HttpGet request = new HttpGet(buildUrl(host, path, querys)); for (
return e(request); }
/** * post form *
* @param host * @param path * @param method * @param headers * @param querys * @param bodys * @return * @throws Exception */ public static HttpResponse doPost(String host, String path, String method,
Map
Map
Map
HttpClient httpClient = wrapClient(host);
HttpPost request = new HttpPost(buildUrl(host, path, querys)); for (
if (bodys != null) { List
for (String key : ()) { (new BasicNameValuePair(key, (key))); } UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8"); tentType("application/x-www-form-urlencoded; charset=UTF-8"); ity(formEntity); }
return e(request); }
/** * Post String *
* @param host * @param path * @param method * @param headers * @param querys * @param body * @return * @throws Exception */ public static HttpResponse doPost(String host, String path, String method,
Map
Map
String body) throws Exception {
HttpClient httpClient = wrapClient(host);
HttpPost request = new HttpPost(buildUrl(host, path, querys)); for (
if (lank(body)) { ity(new StringEntity(body, "utf-8")); }
return e(request); }
/** * Post stream *
* @param host * @param path * @param method * @param headers * @param querys * @param body * @return * @throws Exception */ public static HttpResponse doPost(String host, String path, String method,
Map
Map
byte[] body) throws Exception {
HttpClient httpClient = wrapClient(host);
HttpPost request = new HttpPost(buildUrl(host, path, querys)); for (
if (body != null) { ity(new ByteArrayEntity(body)); }
return e(request); }
/** * Put String * @param host * @param path * @param method * @param headers * @param querys * @param body * @return * @throws Exception */ public static HttpResponse doPut(String host, String path, String method,
Map
Map
String body) throws Exception {
HttpClient httpClient = wrapClient(host);
HttpPut request = new HttpPut(buildUrl(host, path, querys)); for (
if (lank(body)) { ity(new StringEntity(body, "utf-8")); }
return e(request); }
/** * Put stream * @param host * @param path * @param method * @param headers * @param querys * @param body * @return * @throws Exception */ public static HttpResponse doPut(String host, String path, String method,
Map
Map
byte[] body) throws Exception {
HttpClient httpClient = wrapClient(host);
HttpPut request = new HttpPut(buildUrl(host, path, querys)); for (
if (body != null) { ity(new ByteArrayEntity(body)); }
return e(request); }
/** * Delete *
* @param host * @param path * @param method * @param headers * @param querys * @return
* @throws Exception */ public static HttpResponse doDelete(String host, String path, String method,
Map
Map
HttpClient httpClient = wrapClient(host);
HttpDelete request = new HttpDelete(buildUrl(host, path, querys)); for (
return e(request); }
private static String buildUrl(String host, String path, Map
} } if (0 < ()) { ("?").append(sbQuery); } }
return ng(); }
private static HttpClient wrapClient(String host) { HttpClient httpClient = new DefaultHttpClient(); if (With("")) { sslClient(httpClient); }
return httpClient; }
private static void sslClient(HttpClient httpClient) { try { SSLContext ctx = tance("TLS"); X509TrustManager tm = new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] xcs, String str) {
} public void checkServerTrusted(X509Certificate[] xcs, String str) {
} }; (null, new TrustManager[] { tm }, null); SSLSocketFactory ssf = new SSLSocketFactory(ctx); tnameVerifier(_ALL_HOSTNAME_VERIFIER); ClientConnectionManager ccm = nectionManager(); SchemeRegistry registry = emeRegistry(); er(new Scheme("https", 443, ssf));
发布评论