import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Security;
import java.security.cert.CertificateException;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.crypto.Cipher;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.commons.codec.digest.Md5Crypt;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.params.CoreConnectionPNames;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.log4j.Logger;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.soofa.apache.commons.io.output.StringBuilderWriter;
import com.alibaba.dubbo.remoting.TimeoutException;
import com.fintech.third.model.PaymentResponse;
import com.fintech.third.model.ThirdProperties;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import sun.misc.BASE64Decoder;
public class HttpUtil {
private static final MediaType String = null;
private static final String APPLICATION_JSON = "application/json";
private static Logger logger = Logger.getLogger(HttpUtil.class);
/**
* 使用HttpClient发送post请求
*
* @param url
* @param jsonStr
* @return
* @throws Exception
*/
public static String httpPostTongDun(String url, String jsonStr) throws TimeoutException, Exception {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost();
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 6000);
httpPost.addHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded");
StringEntity se = new StringEntity(jsonStr, Charset.forName("UTF-8"));
httpPost.setEntity(se);
httpPost.setURI(new URI(url));
StringBuilder result = new StringBuilder();
HttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(
new InputStreamReader(entity.getContent(), Charset.forName("UTF-8")));
String string = "";
while ((string = reader.readLine()) != null) {
result.append(string);
}
reader.close();
}
return result.toString();
}
public static String httpPost(String url) throws TimeoutException, Exception {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost();
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 6000);
httpPost.addHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded");
httpPost.setURI(new URI(url));
StringBuilder result = new StringBuilder();
HttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(
new InputStreamReader(entity.getContent(), Charset.forName("UTF-8")));
String string = "";
while ((string = reader.readLine()) != null) {
result.append(string);
}
reader.close();
}
return result.toString();
}
/**
* 使用HttpClient发送get请求
*
* @param arg
* @return
* @throws IOException
*/
public static String httpGetTongDun(String url) throws TimeoutException, Exception {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 6000);
StringBuilder result = new StringBuilder();
httpGet.addHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded");
HttpResponse response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
String str;
while ((str = reader.readLine()) != null) {
result.append(str);
}
reader.close();
}
return result.toString();
}
/**
* 使用HttpClient发送post请求
*
* @param url
* @param jsonStr
* @return
* @throws Exception
*/
public static String httpPost(String url, String jsonStr) throws TimeoutException, Exception {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost();
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 6000);
httpPost.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON);
StringEntity se = new StringEntity(jsonStr, Charset.forName("UTF-8"));
se.setContentType("text/json");
httpPost.setEntity(se);
httpPost.setURI(new URI(url));
StringBuilder result = new StringBuilder();
HttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(
new InputStreamReader(entity.getContent(), Charset.forName("UTF-8")));
String string = "";
while ((string = reader.readLine()) != null) {
result.append(string);
}
reader.close();
}
return result.toString();
}
/**
* 使用HttpClient发送post请求(忽略证书)
*
* @param url
* @param jsonStr
* @return
* @throws Exception
*/
public static String httpPost2(String url, String jsonStr) throws TimeoutException, Exception {
HttpClient httpClient = new DefaultHttpClient();
enableSSL(httpClient);
HttpPost httpPost = new HttpPost();
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 6000);
httpPost.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON);
StringEntity se = new StringEntity(jsonStr, Charset.forName("UTF-8"));
se.setContentType("text/json");
httpPost.setEntity(se);
httpPost.setURI(new URI(url));
StringBuilder result = new StringBuilder();
HttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(
new InputStreamReader(entity.getContent(), Charset.forName("UTF-8")));
String string = "";
while ((string = reader.readLine()) != null) {
result.append(string);
}
reader.close();
}
return result.toString();
}
/**
* 使用HttpClient发送get请求
*
* @param arg
* @return
* @throws IOException
*/
public static String httpGet(String url) throws TimeoutException, Exception {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 6000);
StringBuilder result = new StringBuilder();
httpGet.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON);
HttpResponse response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
String str;
while ((str = reader.readLine()) != null) {
result.append(str);
}
reader.close();
}
return result.toString();
}
/**
* 使用HttpClient发送get请求
*
* @param arg
* @return
* @throws IOException
*/
public static String httpGetTimeSet(String url) throws TimeoutException, Exception {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000);
httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(1, true));
StringBuilder result = new StringBuilder();
httpGet.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON);
HttpResponse response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
String str;
while ((str = reader.readLine()) != null) {
result.append(str);
}
reader.close();
}
return result.toString();
}
}