sk_pay/src/main/java/com/jmfy/util/HttpUtil.java

637 lines
17 KiB
Java
Raw Normal View History

2024-07-15 11:45:38 +08:00
/**
* @file HttpUtil.java
*
* @brief
* HttpUtil is a single class containing methods to conveniently perform HTTP
* requests. HttpUtil only uses regular java io and net functionality and does
* not depend on external libraries.
* The class contains methods to perform a get, post, put, and delete request,
* and supports posting forms. Optionally, one can provide headers.
*
* Example usage:
*
* // get
* String res = HttpUtil.get("http://www.google.com");
*
* // post
* String res = HttpUtil.post("http://sendmedata.com", "This is the data");
*
* // post form
* Map<String, String> params = new HashMap<String, String>();
* params.put("firstname", "Joe");
* params.put("lastname", "Smith");
* params.put("age", "28");
* String res = HttpUtil.postForm("http://site.com/newuser", params);
*
* // append query parameters to url
* String url = "http://mydatabase.com/users";
* Map<String, String> params = new HashMap<String, String>();
* params.put("orderby", "name");
* params.put("limit", "10");
* String fullUrl = HttpUtil.appendQueryParams(url, params);
* // fullUrl = "http://mydatabase.com/user?orderby=name&limit=10"
*
* @license
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
* Copyright (c) 2012 Almende B.V.
*
* @author kui_ye
* @date 2012-05-14
*/
package com.jmfy.util;
import javax.net.ssl.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class HttpUtil {
/**
* Send a get request
* @param url
* @return response
* @throws IOException
*/
//fetchcode
static public String get(String url) throws IOException {
return get(url, null);
}
static public String getcode(String url) throws IOException {
return getcode(url, null);
}
/**
* Send a get request
* @param url Url as string
* @param headers Optional map with headers
* @return response Response as string
* @throws IOException
*/
static public String get(String url,
Map<String, String> headers) throws IOException {
return fetch("GET", url, null, headers);
}
static public String getcode(String url,
Map<String, String> headers) throws IOException {
return fetchcode("GET", url, null, headers);
}
/**
* Send a post request
* @param url Url as string
* @param body Request body as string
* @param headers Optional map with headers
* @return response Response as string
* @throws IOException
*/
static public String post(String url, String body,
Map<String, String> headers) throws IOException {
return fetch("POST", url, body, headers);
}
static public String postWithgzip(String url, String body,
Map<String, String> headers) throws IOException {
return fetchWithgzip("POST", url, body, headers);
}
static public String postWithOutSSL(String url, String body,
Map<String, String> headers) throws Exception {
return fetchWihtOutSSL("POST", url, body, headers);
}
/**
* Send a post request
* @param url Url as string
* @param body Request body as string
* @return response Response as string
* @throws IOException
*/
static public String post(String url, String body) throws IOException {
return post(url, body, null);
}
static public String postWithgzip(String url, String body) throws IOException {
return postWithgzip(url, body, null);
}
/**
* Post a form with parameters
* @param url Url as string
* @param params map with parameters/values
* @return response Response as string
* @throws IOException
*/
static public String postForm(String url, Map<String, String> params)
throws IOException {
return postForm(url, params, null);
}
static public String postheaderForm(String url, Map<String, String> params, Map<String, String> paramheard)
throws IOException {
return postForm(url, params, paramheard);
}
/**
* Post a form with parameters
* @param url Url as string
* @param params Map with parameters/values
* @param headers Optional map with headers
* @return response Response as string
* @throws IOException
*/
static public String postForm(String url, Map<String, String> params, Map<String, String> headers) throws IOException {
// set content type
if (headers == null) {
headers = new HashMap<String, String>();
}
headers.put("Content-Type", "application/x-www-form-urlencoded");
// parse parameters
String body = "";
if (params != null) {
boolean first = true;
for (String param : params.keySet()) {
if (first) {
first = false;
}
else {
body += "&";
}
String value = params.get(param);
body += URLEncoder.encode(param, "UTF-8") + "=";
body += URLEncoder.encode(value, "UTF-8");
}
}
return post(url, body, headers);
}
static public String postJsonFormRealName(String url, String params, Map<String, String> headers) throws IOException {
// set content type
if (headers == null) {
headers = new HashMap<String, String>();
}
headers.put("Content-Type", "application/json;charset=utf-8");
//headers.put("timestamps",String.valueOf(System.currentTimeMillis()));
// parse parameters
return post(url, params, headers);
}
static public String postFormWithOutSSL(String url, Map<String, String> params, Map<String, String> headers) throws Exception {
// set content type
if (headers == null) {
headers = new HashMap<String, String>();
}
headers.put("Content-Type", "application/x-www-form-urlencoded");
// parse parameters
String body = "";
if (params != null) {
boolean first = true;
for (String param : params.keySet()) {
if (first) {
first = false;
}
else {
body += "&";
}
String value = params.get(param);
body += URLEncoder.encode(param, "UTF-8") + "=";
body += URLEncoder.encode(value, "UTF-8");
}
}
return postWithOutSSL(url, body, headers);
}
/**
* Send a put request
* @param url Url as string
* @param body Request body as string
* @param headers Optional map with headers
* @return response Response as string
* @throws IOException
*/
static public String put(String url, String body,
Map<String, String> headers) throws IOException {
return fetch("PUT", url, body, headers);
}
/**
* Send a put request
* @param url Url as string
* @return response Response as string
* @throws IOException
*/
static public String put(String url, String body) throws IOException {
return put(url, body, null);
}
/**
* Send a delete request
* @param url Url as string
* @param headers Optional map with headers
* @return response Response as string
* @throws IOException
*/
static public String delete(String url,
Map<String, String> headers) throws IOException {
return fetch("DELETE", url, null, headers);
}
/**
* Send a delete request
* @param url Url as string
* @return response Response as string
* @throws IOException
*/
static public String delete(String url) throws IOException {
return delete(url, null);
}
/**
* Append query parameters to given url
* @param url Url as string
* @param params Map with query parameters
* @return url Url with query parameters appended
* @throws IOException
*/
static public String appendQueryParams(String url,
Map<String, String> params) throws IOException {
String fullUrl = url;
if (params != null) {
boolean first = (fullUrl.indexOf('?') == -1);
for (String param : params.keySet()) {
if (first) {
fullUrl += '?';
first = false;
}
else {
fullUrl += '&';
}
String value = params.get(param);
fullUrl += URLEncoder.encode(param, "UTF-8") + '=';
fullUrl += URLEncoder.encode(value, "UTF-8");
}
}
return fullUrl;
}
/**
* Retrieve the query parameters from given url
* @param url Url containing query parameters
* @return params Map with query parameters
* @throws IOException
*/
static public Map<String, String> getQueryParams(String url)
throws IOException {
Map<String, String> params = new HashMap<String, String>();
int start = url.indexOf('?');
while (start != -1) {
// read parameter name
int equals = url.indexOf('=', start);
String param = "";
if (equals != -1) {
param = url.substring(start + 1, equals);
}
else {
param = url.substring(start + 1);
}
// read parameter value
String value = "";
if (equals != -1) {
start = url.indexOf('&', equals);
if (start != -1) {
value = url.substring(equals + 1, start);
}
else {
value = url.substring(equals + 1);
}
}
params.put(URLDecoder.decode(param, "UTF-8"),
URLDecoder.decode(value, "UTF-8"));
}
return params;
}
/**
* Returns the url without query parameters
* @param url Url containing query parameters
* @return url Url without query parameters
* @throws IOException
*/
static public String removeQueryParams(String url)
throws IOException {
int q = url.indexOf('?');
if (q != -1) {
return url.substring(0, q);
}
else {
return url;
}
}
/**
* Send a request
* @param method HTTP method, for example "GET" or "POST"
* @param url Url as string
* @param body Request body as string
* @param headers Optional map with headers
* @return response Response as string
* @throws IOException
*/
static public String fetch(String method, String url, String body,
Map<String, String> headers) throws IOException {
// connection
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection)u.openConnection();
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
// method
if (method != null) {
conn.setRequestMethod(method);
}
// headers
if (headers != null) {
for(String key : headers.keySet()) {
conn.addRequestProperty(key, headers.get(key));
}
}
// body
if (body != null) {
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write(body.getBytes());
os.flush();
os.close();
}
// response
InputStream is = conn.getInputStream();
String response = streamToString(is);
is.close();
// handle redirects
if (conn.getResponseCode() == 301) {
String location = conn.getHeaderField("Location");
return fetch(method, location, body, headers);
}
return response;
}
static public String fetchWithgzip(String method, String url, String body,
Map<String, String> headers) throws IOException {
// connection
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection)u.openConnection();
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
// method
if (method != null) {
conn.setRequestMethod(method);
}
// headers
if (headers != null) {
for(String key : headers.keySet()) {
conn.addRequestProperty(key, headers.get(key));
}
}
// body
if (body != null) {
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
byte[] bodys=compress(body,"utf-8");
os.write(bodys);
os.flush();
os.close();
}
// response
InputStream is = conn.getInputStream();
String response = gzipUncompress(is);
is.close();
// handle redirects
if (conn.getResponseCode() == 301) {
String location = conn.getHeaderField("Location");
return fetch(method, location, body, headers);
}
return response;
}
static public String fetchWihtOutSSL(String method, String url, String body,
Map<String, String> headers) throws Exception {
// connection
URL u = new URL(url);
if ("https".equalsIgnoreCase(u.getProtocol())) {
miTM.ignoreSsl();
}
HttpURLConnection conn = (HttpURLConnection)u.openConnection();
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
// method
if (method != null) {
conn.setRequestMethod(method);
}
// headers
if (headers != null) {
for(String key : headers.keySet()) {
conn.addRequestProperty(key, headers.get(key));
}
}
// body
if (body != null) {
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write(body.getBytes());
os.flush();
os.close();
}
// response
InputStream is = conn.getInputStream();
String response = streamToString(is);
is.close();
// handle redirects
if (conn.getResponseCode() == 301) {
String location = conn.getHeaderField("Location");
return fetch(method, location, body, headers);
}
return response;
}
static public String fetchcode(String method, String url, String body,
Map<String, String> headers) throws IOException {
// connection
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection)u.openConnection();
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
// method
if (method != null) {
conn.setRequestMethod(method);
}
// headers
if (headers != null) {
for(String key : headers.keySet()) {
conn.addRequestProperty(key, headers.get(key));
}
}
// body
if (body != null) {
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write(body.getBytes());
os.flush();
os.close();
}
// response
InputStream is = conn.getInputStream();
String response = streamToString(is);
is.close();
// handle redirects
if (conn.getResponseCode() == 301) {
String location = conn.getHeaderField("Location");
return fetch(method, location, body, headers);
}
return String.valueOf(conn.getResponseCode());
}
/**
* Read an input stream into a string
* @param in
* @return
* @throws IOException
*/
static public String streamToString(InputStream in) throws IOException {
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = in.read(b)) != -1;) {
out.append(new String(b, 0, n));
}
return out.toString();
}
public static String gzipUncompress(InputStream inputStream) throws IOException{
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPInputStream gunzip = new GZIPInputStream(inputStream);
byte[] buffer = new byte[256];
int n;
while ((n = gunzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
// toString()使用平台默认编码也可以显式的指定如toString("GBK")
return out.toString();
}
public static byte[] compress(String str, String encoding) {
if (str == null || str.length() == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip;
try {
gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes(encoding));
gzip.close();
} catch ( Exception e) {
e.printStackTrace();
}
return out.toByteArray();
}
private static void trustAllHttpsCertificates() throws Exception {
TrustManager[] trustAllCerts = new TrustManager[1];
TrustManager tm = new miTM();
trustAllCerts[0] = tm;
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
static class miTM implements TrustManager, X509TrustManager {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public boolean isServerTrusted(X509Certificate[] certs) {
return true;
}
public boolean isClientTrusted(X509Certificate[] certs) {
return true;
}
public void checkServerTrusted(X509Certificate[] certs, String authType)
throws CertificateException {
return;
}
public void checkClientTrusted(X509Certificate[] certs, String authType)
throws CertificateException {
return;
}
public static void ignoreSsl() throws Exception {
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
System.out.println("Warning: URL Host: " + urlHostName
+ " vs. " + session.getPeerHost());
return true;
}
};
trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier(hv);
}
}
}