hz-zhhq-app-service/greenH5modul/.svn/pristine/91/91bf8f9ba926e936c45bb3a34a7...

145 lines
4.3 KiB
Plaintext
Raw Normal View History

2025-01-21 13:12:35 +08:00
package com.jysoft.weChat.util;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;
import java.util.Map;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
/**
* http 工具类
*/
public class HttpUtil {
public static String postHttpsResponse1(String requestUrl, String params) throws Exception {
String contentType = "application/x-www-form-urlencoded";
return post(requestUrl, contentType, params);
}
// json 方式数据请求
public static String postHttpsResponse(String requestUrl, String params) throws Exception {
String contentType = "application/json";
return post(requestUrl, contentType, params);
}
public static String post(String requestUrl, String contentType, String params) throws Exception {
String encoding = "UTF-8";
if (requestUrl.contains("nlp")) {
encoding = "GBK";
}
URL url = new URL(requestUrl);
// 打开和URL之间的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
// 设置通用的请求属性
connection.setRequestProperty("Content-Type", contentType);
connection.setRequestProperty("Connection", "Keep-Alive");
// 不使用缓冲
connection.setUseCaches(false);
// 允许输出流,即允许上传
connection.setDoOutput(true);
// 允许下载
connection.setDoInput(true);
// 得到请求的输出流对象
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.write(params.getBytes(encoding));
out.flush();
out.close();
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> headers = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : headers.keySet()) {
System.err.println(key + "--->" + headers.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
BufferedReader in = null;
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), encoding));
String result = "";
String getLine;
while ((getLine = in.readLine()) != null) {
result += getLine;
}
in.close();
System.err.println("result:" + result);
return result;
}
// get方式数据请求
public static String getHttpsResponse(String reqUrl, String requestMethod) {
URL url;
InputStream is;
String result = "";
try {
url = new URL(reqUrl);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
TrustManager[] tm = { xtm };
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, tm, null);
con.setSSLSocketFactory(ctx.getSocketFactory());
con.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
});
con.setDoInput(true); // 允许输入流,即允许下载
// 在android中必须将此项设置为false
con.setDoOutput(false); // 允许输出流,即允许上传
con.setUseCaches(false); // 不使用缓冲
if (null != requestMethod && !requestMethod.equals("")) {
con.setRequestMethod(requestMethod); // 使用指定的方式
} else {
con.setRequestMethod("GET"); // 使用get请求
}
is = con.getInputStream(); // 获取输入流,此时才真正建立链接
InputStreamReader isr = new InputStreamReader(is);
BufferedReader bufferReader = new BufferedReader(isr);
String inputLine;
while ((inputLine = bufferReader.readLine()) != null) {
result += inputLine + "\n";
}
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
static X509TrustManager xtm = new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
}
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
}
};
}