|
|
@@ -18,67 +18,164 @@ import org.apache.http.entity.StringEntity;
|
|
|
import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
import org.apache.http.message.BasicNameValuePair;
|
|
|
|
|
|
+import javax.net.ssl.HttpsURLConnection;
|
|
|
import javax.net.ssl.SSLContext;
|
|
|
import javax.net.ssl.TrustManager;
|
|
|
import javax.net.ssl.X509TrustManager;
|
|
|
-import java.io.UnsupportedEncodingException;
|
|
|
+import java.io.*;
|
|
|
+import java.net.ConnectException;
|
|
|
+import java.net.URL;
|
|
|
import java.net.URLEncoder;
|
|
|
import java.security.KeyManagementException;
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
+import java.security.SecureRandom;
|
|
|
+import java.security.cert.CertificateException;
|
|
|
import java.security.cert.X509Certificate;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
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<String, String> headers,
|
|
|
- Map<String, String> querys)
|
|
|
- throws Exception {
|
|
|
- HttpClient httpClient = wrapClient(host);
|
|
|
-
|
|
|
- HttpGet request = new HttpGet(buildUrl(host, path, querys));
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 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<String, String> headers, Map<String, String> querys) throws Exception {
|
|
|
+ HttpClient httpClient = wrapClient(host);
|
|
|
+
|
|
|
+ HttpGet request = new HttpGet(buildUrl(host, path, querys));
|
|
|
for (Map.Entry<String, String> e : headers.entrySet()) {
|
|
|
- request.addHeader(e.getKey(), e.getValue());
|
|
|
+ request.addHeader(e.getKey(), e.getValue());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return httpClient.execute(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<String, String> headers,
|
|
|
- Map<String, String> querys,
|
|
|
- Map<String, String> bodys)
|
|
|
- throws Exception {
|
|
|
- HttpClient httpClient = wrapClient(host);
|
|
|
-
|
|
|
- HttpPost request = new HttpPost(buildUrl(host, path, querys));
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param host
|
|
|
+ * @param path
|
|
|
+ * @param method
|
|
|
+ * @param headers
|
|
|
+ * @param querys
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String doGetNew(String host, String path, String method, Map<String, String> headers, Map<String, String> querys) {
|
|
|
+ HttpsURLConnection conn = null;
|
|
|
+ InputStream inputStream = null;
|
|
|
+ InputStreamReader inputStreamReader = null;
|
|
|
+ BufferedReader bufferedReader = null;
|
|
|
+ String resultStr = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ //检查证书
|
|
|
+ TrustManager[] tm = checkTrustManager();
|
|
|
+ SSLContext ctx = SSLContext.getInstance("TLS");
|
|
|
+ ctx.init(null, tm, new SecureRandom());
|
|
|
+ javax.net.ssl.SSLSocketFactory ssf = ctx.getSocketFactory();
|
|
|
+ URL url = new URL(buildUrl(host, path, querys));
|
|
|
+ conn = (HttpsURLConnection) url.openConnection();
|
|
|
+ conn.setSSLSocketFactory(ssf);
|
|
|
+ //设置从HttpsURLConnection读入
|
|
|
+ conn.setDoInput(true);
|
|
|
+ //设置向HttpsURLConnection输出
|
|
|
+ conn.setDoOutput(false);
|
|
|
+ //不使用缓存
|
|
|
+ conn.setUseCaches(false);
|
|
|
+ //请求方式
|
|
|
+ conn.setRequestMethod(method);
|
|
|
+ //超时3000毫秒
|
|
|
+ conn.setConnectTimeout(3000);
|
|
|
+ conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
|
|
|
+ for (Map.Entry<String, String> e : headers.entrySet()) {
|
|
|
+ conn.setRequestProperty(e.getKey(), e.getValue());
|
|
|
+ }
|
|
|
+ if (conn.getResponseCode() == 200) {
|
|
|
+ //从输入流读取返回的内容
|
|
|
+ inputStream = conn.getInputStream();
|
|
|
+ inputStreamReader = new InputStreamReader(inputStream, "utf-8");
|
|
|
+ bufferedReader = new BufferedReader(inputStreamReader);
|
|
|
+ StringBuffer buffer = new StringBuffer();
|
|
|
+ String str = null;
|
|
|
+ while ((str = bufferedReader.readLine()) != null) {
|
|
|
+ buffer.append(str);
|
|
|
+ }
|
|
|
+ resultStr = buffer.toString();
|
|
|
+ }
|
|
|
+ } catch (ConnectException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ } finally {
|
|
|
+ //释放链接,关闭流
|
|
|
+ if (conn != null) {
|
|
|
+ conn.disconnect();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ if (bufferedReader != null) {
|
|
|
+ bufferedReader.close();
|
|
|
+ }
|
|
|
+ if (inputStreamReader != null) {
|
|
|
+ inputStreamReader.close();
|
|
|
+ }
|
|
|
+ if (inputStream != null) {
|
|
|
+ inputStream.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return resultStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static TrustManager[] checkTrustManager() {
|
|
|
+ TrustManager[] trustManagers = {new X509TrustManager() {
|
|
|
+ //检查客户端证书
|
|
|
+ @Override
|
|
|
+ public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //检查服务器端证书
|
|
|
+ @Override
|
|
|
+ public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //返回受信任的x509证书数组
|
|
|
+ @Override
|
|
|
+ public X509Certificate[] getAcceptedIssuers() {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }};
|
|
|
+ return trustManagers;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 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<String, String> headers, Map<String, String> querys, Map<String, String> bodys) throws Exception {
|
|
|
+ HttpClient httpClient = wrapClient(host);
|
|
|
+
|
|
|
+ HttpPost request = new HttpPost(buildUrl(host, path, querys));
|
|
|
for (Map.Entry<String, String> e : headers.entrySet()) {
|
|
|
- request.addHeader(e.getKey(), e.getValue());
|
|
|
+ request.addHeader(e.getKey(), e.getValue());
|
|
|
}
|
|
|
|
|
|
if (bodys != null) {
|
|
|
@@ -93,210 +190,195 @@ public class HttpUtils {
|
|
|
}
|
|
|
|
|
|
return httpClient.execute(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<String, String> headers,
|
|
|
- Map<String, String> querys,
|
|
|
- String body)
|
|
|
- throws Exception {
|
|
|
- HttpClient httpClient = wrapClient(host);
|
|
|
-
|
|
|
- HttpPost request = new HttpPost(buildUrl(host, path, querys));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 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<String, String> headers, Map<String, String> querys, String body) throws Exception {
|
|
|
+ HttpClient httpClient = wrapClient(host);
|
|
|
+
|
|
|
+ HttpPost request = new HttpPost(buildUrl(host, path, querys));
|
|
|
for (Map.Entry<String, String> e : headers.entrySet()) {
|
|
|
- request.addHeader(e.getKey(), e.getValue());
|
|
|
+ request.addHeader(e.getKey(), e.getValue());
|
|
|
}
|
|
|
|
|
|
if (StringUtils.isNotBlank(body)) {
|
|
|
- request.setEntity(new StringEntity(body, "utf-8"));
|
|
|
+ request.setEntity(new StringEntity(body, "utf-8"));
|
|
|
}
|
|
|
|
|
|
return httpClient.execute(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<String, String> headers,
|
|
|
- Map<String, String> querys,
|
|
|
- byte[] body)
|
|
|
- throws Exception {
|
|
|
- HttpClient httpClient = wrapClient(host);
|
|
|
-
|
|
|
- HttpPost request = new HttpPost(buildUrl(host, path, querys));
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 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<String, String> headers, Map<String, String> querys, byte[] body) throws Exception {
|
|
|
+ HttpClient httpClient = wrapClient(host);
|
|
|
+
|
|
|
+ HttpPost request = new HttpPost(buildUrl(host, path, querys));
|
|
|
for (Map.Entry<String, String> e : headers.entrySet()) {
|
|
|
- request.addHeader(e.getKey(), e.getValue());
|
|
|
+ request.addHeader(e.getKey(), e.getValue());
|
|
|
}
|
|
|
|
|
|
if (body != null) {
|
|
|
- request.setEntity(new ByteArrayEntity(body));
|
|
|
+ request.setEntity(new ByteArrayEntity(body));
|
|
|
}
|
|
|
|
|
|
return httpClient.execute(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<String, String> headers,
|
|
|
- Map<String, String> querys,
|
|
|
- String body)
|
|
|
- throws Exception {
|
|
|
- HttpClient httpClient = wrapClient(host);
|
|
|
-
|
|
|
- HttpPut request = new HttpPut(buildUrl(host, path, querys));
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 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<String, String> headers, Map<String, String> querys, String body) throws Exception {
|
|
|
+ HttpClient httpClient = wrapClient(host);
|
|
|
+
|
|
|
+ HttpPut request = new HttpPut(buildUrl(host, path, querys));
|
|
|
for (Map.Entry<String, String> e : headers.entrySet()) {
|
|
|
- request.addHeader(e.getKey(), e.getValue());
|
|
|
+ request.addHeader(e.getKey(), e.getValue());
|
|
|
}
|
|
|
|
|
|
if (StringUtils.isNotBlank(body)) {
|
|
|
- request.setEntity(new StringEntity(body, "utf-8"));
|
|
|
+ request.setEntity(new StringEntity(body, "utf-8"));
|
|
|
}
|
|
|
|
|
|
return httpClient.execute(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<String, String> headers,
|
|
|
- Map<String, String> querys,
|
|
|
- byte[] body)
|
|
|
- throws Exception {
|
|
|
- HttpClient httpClient = wrapClient(host);
|
|
|
-
|
|
|
- HttpPut request = new HttpPut(buildUrl(host, path, querys));
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 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<String, String> headers, Map<String, String> querys, byte[] body) throws Exception {
|
|
|
+ HttpClient httpClient = wrapClient(host);
|
|
|
+
|
|
|
+ HttpPut request = new HttpPut(buildUrl(host, path, querys));
|
|
|
for (Map.Entry<String, String> e : headers.entrySet()) {
|
|
|
- request.addHeader(e.getKey(), e.getValue());
|
|
|
+ request.addHeader(e.getKey(), e.getValue());
|
|
|
}
|
|
|
|
|
|
if (body != null) {
|
|
|
- request.setEntity(new ByteArrayEntity(body));
|
|
|
+ request.setEntity(new ByteArrayEntity(body));
|
|
|
}
|
|
|
|
|
|
return httpClient.execute(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<String, String> headers,
|
|
|
- Map<String, String> querys)
|
|
|
- throws Exception {
|
|
|
- HttpClient httpClient = wrapClient(host);
|
|
|
-
|
|
|
- HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 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<String, String> headers, Map<String, String> querys) throws Exception {
|
|
|
+ HttpClient httpClient = wrapClient(host);
|
|
|
+
|
|
|
+ HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
|
|
|
for (Map.Entry<String, String> e : headers.entrySet()) {
|
|
|
- request.addHeader(e.getKey(), e.getValue());
|
|
|
+ request.addHeader(e.getKey(), e.getValue());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return httpClient.execute(request);
|
|
|
}
|
|
|
-
|
|
|
- private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException {
|
|
|
- StringBuilder sbUrl = new StringBuilder();
|
|
|
- sbUrl.append(host);
|
|
|
- if (!StringUtils.isBlank(path)) {
|
|
|
- sbUrl.append(path);
|
|
|
+
|
|
|
+ private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException {
|
|
|
+ StringBuilder sbUrl = new StringBuilder();
|
|
|
+ sbUrl.append(host);
|
|
|
+ if (!StringUtils.isBlank(path)) {
|
|
|
+ sbUrl.append(path);
|
|
|
}
|
|
|
- if (null != querys) {
|
|
|
- StringBuilder sbQuery = new StringBuilder();
|
|
|
- for (Map.Entry<String, String> query : querys.entrySet()) {
|
|
|
- if (0 < sbQuery.length()) {
|
|
|
- sbQuery.append("&");
|
|
|
- }
|
|
|
- if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
|
|
|
- sbQuery.append(query.getValue());
|
|
|
+ if (null != querys) {
|
|
|
+ StringBuilder sbQuery = new StringBuilder();
|
|
|
+ for (Map.Entry<String, String> query : querys.entrySet()) {
|
|
|
+ if (0 < sbQuery.length()) {
|
|
|
+ sbQuery.append("&");
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
|
|
|
+ sbQuery.append(query.getValue());
|
|
|
}
|
|
|
- if (!StringUtils.isBlank(query.getKey())) {
|
|
|
- sbQuery.append(query.getKey());
|
|
|
- if (!StringUtils.isBlank(query.getValue())) {
|
|
|
- sbQuery.append("=");
|
|
|
- sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
|
|
|
- }
|
|
|
+ if (!StringUtils.isBlank(query.getKey())) {
|
|
|
+ sbQuery.append(query.getKey());
|
|
|
+ if (!StringUtils.isBlank(query.getValue())) {
|
|
|
+ sbQuery.append("=");
|
|
|
+ sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- if (0 < sbQuery.length()) {
|
|
|
- sbUrl.append("?").append(sbQuery);
|
|
|
- }
|
|
|
+ }
|
|
|
+ if (0 < sbQuery.length()) {
|
|
|
+ sbUrl.append("?").append(sbQuery);
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- return sbUrl.toString();
|
|
|
+
|
|
|
+ return sbUrl.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static HttpClient wrapClient(String host) {
|
|
|
+ HttpClient httpClient = new DefaultHttpClient();
|
|
|
+ if (host.startsWith("https://")) {
|
|
|
+ sslClient(httpClient);
|
|
|
+ }
|
|
|
+
|
|
|
+ return httpClient;
|
|
|
}
|
|
|
-
|
|
|
- private static HttpClient wrapClient(String host) {
|
|
|
- HttpClient httpClient = new DefaultHttpClient();
|
|
|
- if (host.startsWith("https://")) {
|
|
|
- sslClient(httpClient);
|
|
|
- }
|
|
|
-
|
|
|
- return httpClient;
|
|
|
- }
|
|
|
-
|
|
|
- private static void sslClient(HttpClient httpClient) {
|
|
|
+
|
|
|
+ private static void sslClient(HttpClient httpClient) {
|
|
|
try {
|
|
|
SSLContext ctx = SSLContext.getInstance("TLS");
|
|
|
X509TrustManager tm = new X509TrustManager() {
|
|
|
public X509Certificate[] getAcceptedIssuers() {
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
public void checkClientTrusted(X509Certificate[] xcs, String str) {
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
+
|
|
|
public void checkServerTrusted(X509Certificate[] xcs, String str) {
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
};
|
|
|
- ctx.init(null, new TrustManager[] { tm }, null);
|
|
|
+ ctx.init(null, new TrustManager[]{tm}, null);
|
|
|
SSLSocketFactory ssf = new SSLSocketFactory(ctx);
|
|
|
ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
|
|
ClientConnectionManager ccm = httpClient.getConnectionManager();
|
|
|
@@ -305,7 +387,7 @@ public class HttpUtils {
|
|
|
} catch (KeyManagementException ex) {
|
|
|
throw new RuntimeException(ex);
|
|
|
} catch (NoSuchAlgorithmException ex) {
|
|
|
- throw new RuntimeException(ex);
|
|
|
+ throw new RuntimeException(ex);
|
|
|
}
|
|
|
}
|
|
|
}
|