HTTPS Java
2016-05-30 10:15
423 查看
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;
}
}
相关文章推荐
- Android Listview异步动态加载网络图片
- c 网络字节序和本机字节序转换
- Chp6 TCP连接中的三次握手四次挥手协议
- 浅谈HTTP中Get与Post的区别
- 常见的HTTP状态码深入理解
- 命令行抓取IPhone设备网络报文
- 命令:netstat 常用操作记录
- HttpClient 模拟登陆知乎
- 使用HttpClient调用RESTful Web服务
- HTTP in iOS你看我就够
- 使用HttpURLConnection调用RESTful Web服务
- TCP的三次握手和四次挥手(转)
- Windows 网络命令
- TCP : two different sockets sharing a port?
- Linux网络配置
- Java 网络编程之 Socket
- iOS_直播类app_HTTP Live Streaming
- objective-c -- 网络数据库 - 进程和线程--GCD
- 关于我的httpUrlConnection 出错的蠢事
- CS231n 卷积神经网络与计算机视觉 11 卷积神经网络的 迁移学习 和微调