//信任HTTPS
private static void trustAllHttpsCertificates() throws Exception {
javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
javax.net.ssl.TrustManager tm = new HttpsIgnore();
trustAllCerts[0] = tm;
javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext
.getInstance("SSL");
sc.init(null, trustAllCerts, null);
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc
.getSocketFactory());
}
/**
* 发送HttpPost请求
* <p/>
* 服务地址
*
* @param params json字符串,例如: "{ \"id\":\"12345\" }" ;其中属性名必须带双引号
* @Return 成功:返回json字符串
*/
public static String postMobileMsg(String params) {
try {
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
logger.info("Warning: URL Host: " + urlHostName + " vs. "
+ session.getPeerHost());
return true;
}
};
if (chk_json.toUpperCase().equals("TRUE")) {
//信任https
trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier(hv);
URL url = new URL(WebUrl);// 创建连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestMethod("POST"); // 设置请求方式
connection.setRequestProperty("ContentType", "text/xml;charset=utf-8");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // 设置发送数据的格式
connection.setRequestProperty("charset", "UTF-8");
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
connection.connect();
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码
out.append(params);
out.flush();
out.close();
// 读取响应
int length = (int) connection.getContentLength();// 获取长度
InputStream is = connection.getInputStream();
if (length != -1) {
byte[] data = new byte[length];
byte[] temp = new byte[512];
int readLen = 0;
int destPos = 0;
while ((readLen = is.read(temp)) > 0) {
System.arraycopy(temp, 0, data, destPos, readLen);
destPos += readLen;
}
String result = new String(data, "UTF-8"); // utf-8编码
String resultM = result.replace("\n", "").replace(" ", "");
System.out.println(resultM);
logger.info(resultM);
connection.disconnect();
return result;
}
} else {
trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier(hv);
String urlpath = WebUrl + "?" + params;
URL url = new URL(urlpath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoInput(true);//有输入
conn.setDoOutput(true);//有输出
InputStreamReader r = new InputStreamReader(conn.getInputStream(), "UTF-8");
BufferedReader reader = new BufferedReader(r);
String line;
StringBuffer sb = new StringBuffer();
while ((line = reader.readLine()) != null) {
sb.append(line);
}
conn.disconnect();
String resultM = sb.toString().replace("\n", "").replace(" ", "");
return resultM;
}
} catch (IOException e) {
e.printStackTrace();
logger.error("post" + e);
return "NG:"+e.toString();
} catch (Exception e) {
e.printStackTrace();
logger.error("post" + e);
return "NG:"+e.toString();
}
return "NG"; // 自定义错误信息
}
private static void trustAllHttpsCertificates() throws Exception {
javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
javax.net.ssl.TrustManager tm = new HttpsIgnore();
trustAllCerts[0] = tm;
javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext
.getInstance("SSL");
sc.init(null, trustAllCerts, null);
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc
.getSocketFactory());
}
/**
* 发送HttpPost请求
* <p/>
* 服务地址
*
* @param params json字符串,例如: "{ \"id\":\"12345\" }" ;其中属性名必须带双引号
* @Return 成功:返回json字符串
*/
public static String postMobileMsg(String params) {
try {
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
logger.info("Warning: URL Host: " + urlHostName + " vs. "
+ session.getPeerHost());
return true;
}
};
if (chk_json.toUpperCase().equals("TRUE")) {
//信任https
trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier(hv);
URL url = new URL(WebUrl);// 创建连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestMethod("POST"); // 设置请求方式
connection.setRequestProperty("ContentType", "text/xml;charset=utf-8");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // 设置发送数据的格式
connection.setRequestProperty("charset", "UTF-8");
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
connection.connect();
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码
out.append(params);
out.flush();
out.close();
// 读取响应
int length = (int) connection.getContentLength();// 获取长度
InputStream is = connection.getInputStream();
if (length != -1) {
byte[] data = new byte[length];
byte[] temp = new byte[512];
int readLen = 0;
int destPos = 0;
while ((readLen = is.read(temp)) > 0) {
System.arraycopy(temp, 0, data, destPos, readLen);
destPos += readLen;
}
String result = new String(data, "UTF-8"); // utf-8编码
String resultM = result.replace("\n", "").replace(" ", "");
System.out.println(resultM);
logger.info(resultM);
connection.disconnect();
return result;
}
} else {
trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier(hv);
String urlpath = WebUrl + "?" + params;
URL url = new URL(urlpath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoInput(true);//有输入
conn.setDoOutput(true);//有输出
InputStreamReader r = new InputStreamReader(conn.getInputStream(), "UTF-8");
BufferedReader reader = new BufferedReader(r);
String line;
StringBuffer sb = new StringBuffer();
while ((line = reader.readLine()) != null) {
sb.append(line);
}
conn.disconnect();
String resultM = sb.toString().replace("\n", "").replace(" ", "");
return resultM;
}
} catch (IOException e) {
e.printStackTrace();
logger.error("post" + e);
return "NG:"+e.toString();
} catch (Exception e) {
e.printStackTrace();
logger.error("post" + e);
return "NG:"+e.toString();
}
return "NG"; // 自定义错误信息
}