概述
packagecom.guyezhai.modules.utils;importjava.io.BufferedReader;importjava.io.DataOutputStream;importjava.io.IOException;importjava.io.InputStreamReader;importjava.net.HttpURLConnection;importjava.net.URL;importjava.net.URLEncoder;importjava.security.cert.X509Certificate;importjava.util.Map;importjava.util.Map.Entry;importjavax.net.ssl.HostnameVerifier;importjavax.net.ssl.HttpsURLConnection;importjavax.net.ssl.SSLContext;importjavax.net.ssl.SSLSession;importjavax.net.ssl.X509TrustManager;public classHttpUtils {/*** POST请求远程http页面
*
*@paramcontentUrl
*@paramparams
*@return
*/
public static String post(String contentUrl, Mapparams) {
StringBuilder contentBuilder= newStringBuilder();
HttpURLConnection connection= null;
DataOutputStream out= null;
BufferedReader reader= null;try{
connection= (HttpURLConnection) newURL(contentUrl).openConnection();//设置Socket超时
connection.setConnectTimeout(10000);
connection.setReadTimeout(20000);//Post 请求不能使用缓存
connection.setUseCaches(false);//URLConnection.setInstanceFollowRedirects是成员函数,仅作用于当前函数
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");
connection.setDoOutput(true);//是否输入参数//要注意的是connection.getOutputStream会隐含的进行connect
connection.connect();
out= newDataOutputStream(connection.getOutputStream());
StringBuilder parambBuilder= newStringBuilder();if (params != null) {for (Entrye : params.entrySet()) {
parambBuilder.append(e.getKey()).append("=").append(URLEncoder.encode(String.valueOf(e.getValue()), "UTF-8")).append("&");
}
parambBuilder.deleteCharAt(parambBuilder.length()- 1);
}//DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写道流里面
out.writeBytes(parambBuilder.toString());
out.flush();
reader= new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));int buffer = 0;while ((buffer = reader.read()) != -1) {
contentBuilder.append((char) buffer);
}
}catch(Exception e) {//异常处理
} finally{if (out != null) {try{
out.close();
}catch(IOException e1) {
e1.printStackTrace();
}
}if (reader != null) {try{
reader.close();
}catch(IOException e1) {
e1.printStackTrace();
}
}if (connection != null) {
connection.disconnect();
}
}returncontentBuilder.toString();
}/*** Get方法请求
*
*@paramcontentUrl
*@return
*/
public staticString get(String contentUrl) {
StringBuilder contentBuilder= newStringBuilder();
HttpURLConnection connection= null;
BufferedReader reader= null;try{
connection= (HttpURLConnection) newURL(contentUrl).openConnection();//设置Socket超时
connection.setConnectTimeout(10000);
connection.setReadTimeout(20000);
connection.setRequestMethod("GET");
connection.connect();
reader= new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));int buffer = 0;while ((buffer = reader.read()) != -1) {
contentBuilder.append((char) buffer);
}
}catch(Exception e) {//异常处理
} finally{if (reader != null) {try{
reader.close();
}catch(IOException e1) {
e1.printStackTrace();
}
}if (connection != null) {
connection.disconnect();
}
}returncontentBuilder.toString();
}/*** POST请求远程https页面
*
*@paramcontentUrl
*@paramparams
*@return
*/
public static String postHttps(String contentUrl, Mapparams) {
StringBuilder contentBuilder= newStringBuilder();
HttpsURLConnection connection= null;
DataOutputStream out= null;
BufferedReader reader= null;try{
HostnameVerifier hnv= newHostnameVerifier() {
@Overridepublic booleanverify(String hostname, SSLSession session) {return true;
}
};
X509TrustManager trustManager= newX509TrustManager() {
@OverridepublicX509Certificate[] getAcceptedIssuers() {return null;
}
@Overridepublic voidcheckServerTrusted(X509Certificate[] chain, String authType) {
}
@Overridepublic voidcheckClientTrusted(X509Certificate[] chain, String authType) {
}
};
X509TrustManager[] xtmArray= newX509TrustManager[] { trustManager };
SSLContext sslContext= SSLContext.getInstance("TLS");
sslContext.init(null, xtmArray, newjava.security.SecureRandom());if (sslContext != null) {
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
}
HttpsURLConnection.setDefaultHostnameVerifier(hnv);
connection= (HttpsURLConnection) newURL(contentUrl).openConnection();//设置Socket超时
connection.setConnectTimeout(10000);
connection.setReadTimeout(20000);//Post 请求不能使用缓存
connection.setUseCaches(false);//URLConnection.setInstanceFollowRedirects是成员函数,仅作用于当前函数
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");
connection.setDoOutput(true);//是否输入参数
connection.connect();
out= newDataOutputStream(connection.getOutputStream());
StringBuilder parambBuilder= newStringBuilder();if (params != null) {for (Entrye : params.entrySet()) {
parambBuilder.append(e.getKey()).append("=").append(URLEncoder.encode(String.valueOf(e.getValue()), "UTF-8")).append("&");
}
parambBuilder.deleteCharAt(parambBuilder.length()- 1);
}//DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写道流里面
out.writeBytes(parambBuilder.toString());
out.flush();
reader= new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));int buffer = 0;while ((buffer = reader.read()) != -1) {
contentBuilder.append((char) buffer);
}
}catch(Exception e) {//异常处理
} finally{if (out != null) {try{
out.close();
}catch(IOException e1) {
e1.printStackTrace();
}
}if (reader != null) {try{
reader.close();
}catch(IOException e1) {
e1.printStackTrace();
}
}if (connection != null) {
connection.disconnect();
}
}returncontentBuilder.toString();
}
}
最后
以上就是大胆大雁为你收集整理的http java ssl_Java进行http请求工具类代码(支持https)的全部内容,希望文章能够帮你解决http java ssl_Java进行http请求工具类代码(支持https)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复