public static String sendHttpsUrlRequestPost(String url, String content, String chatset)
{
long start = System.currentTimeMillis();
String ret = null;
HostnameVerifier hv = new HostnameVerifier()
{
@Override
public boolean verify(String hostname, SSLSession session)
{
return hostname.equals(session.getPeerHost());
}
};
if (StringUtils.isBlank(chatset))
{
chatset = CommonConstant.ENCODING_UTF8;
}
HttpsURLConnection.setDefaultHostnameVerifier(hv);
HttpURLConnection urlConnect = null;
try
{
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]
{ new X509TrustManager()
{
@Override
public X509Certificate[] getAcceptedIssuers()
{
return null;
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType)
{
// System.out.println("checkServerTrusted");
}
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType)
{
// System.out.println("checkClientTrusted");
}
} };
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("TLS", "SunJSSE");
sc.getProvider();
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(hv);
// Now you can access an https URL without having the certificate in
// the truststore
URL urlClass = new URL(url);
urlConnect = (HttpURLConnection) urlClass.openConnection();
urlConnect.setRequestMethod("POST");
urlConnect.setConnectTimeout(new Integer(IniConstant.getIniValueByName(
IniKey.LOGIN_THIRDPARTY_ALIPAYAPI_TIMEOUT.getKey(), "30000")));
urlConnect.setReadTimeout(new Integer(IniConstant.getIniValueByName(
IniKey.LOGIN_THIRDPARTY_ALIPAYAPI_TIMEOUT.getKey(), "30000")));
urlConnect.setDoOutput(true);
urlConnect.setRequestProperty("Content-Length", Integer.toString(content.length())); // 设置请求数据长度
byte[] byteContent = content.getBytes(chatset);
urlConnect.getOutputStream().write(byteContent, 0, byteContent.length);
urlConnect.getOutputStream().flush();
urlConnect.getOutputStream().close();
InputStream in = urlConnect.getInputStream();
ret = getReturnValueFromInputStream(in, chatset);
in.close();
} catch (Exception e)
{
log.info("请求支付宝异常,请求地址url=" + url + ",请求参数content=" + content);
throw new BusinessException(e); // http通信异常应直接抛出
} finally
{
log.debug("请求所用时间为" + ((System.currentTimeMillis() - start) / 1000.0) + "s");
if (urlConnect != null)
{
urlConnect.disconnect();
}
}
return ret;
}
/**
* 从输入流获取所有数据并以指定字符集拼装成字符串
*
* @param is
* @param charSet
* @return
*/
public static String getReturnValueFromInputStream(InputStream is, String charSet)
{
if (StringUtils.isBlank(charSet))
{
charSet = CommonConstant.ENCODING_UTF8;
}
StringBuilder sb = new StringBuilder();
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(is, charSet));
char[] temp = new char[1024];
int charCount = 0;
while ((charCount = br.read(temp)) != -1)
{
sb.append(new String(temp, 0, charCount));
}
return sb.toString();
} catch (Exception e)
{
log.fatal(e.getMessage(), e);
return null;
}
}
{
long start = System.currentTimeMillis();
String ret = null;
HostnameVerifier hv = new HostnameVerifier()
{
@Override
public boolean verify(String hostname, SSLSession session)
{
return hostname.equals(session.getPeerHost());
}
};
if (StringUtils.isBlank(chatset))
{
chatset = CommonConstant.ENCODING_UTF8;
}
HttpsURLConnection.setDefaultHostnameVerifier(hv);
HttpURLConnection urlConnect = null;
try
{
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]
{ new X509TrustManager()
{
@Override
public X509Certificate[] getAcceptedIssuers()
{
return null;
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType)
{
// System.out.println("checkServerTrusted");
}
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType)
{
// System.out.println("checkClientTrusted");
}
} };
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("TLS", "SunJSSE");
sc.getProvider();
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(hv);
// Now you can access an https URL without having the certificate in
// the truststore
URL urlClass = new URL(url);
urlConnect = (HttpURLConnection) urlClass.openConnection();
urlConnect.setRequestMethod("POST");
urlConnect.setConnectTimeout(new Integer(IniConstant.getIniValueByName(
IniKey.LOGIN_THIRDPARTY_ALIPAYAPI_TIMEOUT.getKey(), "30000")));
urlConnect.setReadTimeout(new Integer(IniConstant.getIniValueByName(
IniKey.LOGIN_THIRDPARTY_ALIPAYAPI_TIMEOUT.getKey(), "30000")));
urlConnect.setDoOutput(true);
urlConnect.setRequestProperty("Content-Length", Integer.toString(content.length())); // 设置请求数据长度
byte[] byteContent = content.getBytes(chatset);
urlConnect.getOutputStream().write(byteContent, 0, byteContent.length);
urlConnect.getOutputStream().flush();
urlConnect.getOutputStream().close();
InputStream in = urlConnect.getInputStream();
ret = getReturnValueFromInputStream(in, chatset);
in.close();
} catch (Exception e)
{
log.info("请求支付宝异常,请求地址url=" + url + ",请求参数content=" + content);
throw new BusinessException(e); // http通信异常应直接抛出
} finally
{
log.debug("请求所用时间为" + ((System.currentTimeMillis() - start) / 1000.0) + "s");
if (urlConnect != null)
{
urlConnect.disconnect();
}
}
return ret;
}
/**
* 从输入流获取所有数据并以指定字符集拼装成字符串
*
* @param is
* @param charSet
* @return
*/
public static String getReturnValueFromInputStream(InputStream is, String charSet)
{
if (StringUtils.isBlank(charSet))
{
charSet = CommonConstant.ENCODING_UTF8;
}
StringBuilder sb = new StringBuilder();
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(is, charSet));
char[] temp = new char[1024];
int charCount = 0;
while ((charCount = br.read(temp)) != -1)
{
sb.append(new String(temp, 0, charCount));
}
return sb.toString();
} catch (Exception e)
{
log.fatal(e.getMessage(), e);
return null;
}
}