本帖最后由 大秦 于 2021-9-16 09:04 编辑
我的活字格应用发布的是HTTPS,代码如下
package com;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TestToken {
private static Logger LOGGER = LoggerFactory.getLogger(TestToken.class);
public static void main(String[] args) throws NoSuchAlgorithmException, KeyManagementException {
HttpsURLConnection.setDefaultHostnameVerifier(new TestToken().new NullHostNameVerifier());
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpURLConnection httpURLConnection = null;
String userName = "administrator";
String password = "xxx"; // 这个密码是单点登录密码
try {
// 1. 获取访问地址URL
URL url = new URL("https://xxxx/SSO/GetUserToken");
// 2. 创建HttpURLConnection对象
httpURLConnection = (HttpURLConnection) url.openConnection();
/* 3. 设置请求参数等 */
// 请求方式 默认 GET
httpURLConnection.setRequestMethod("POST");
// 超时时间
httpURLConnection.setConnectTimeout(3000);
// 设置是否输出
httpURLConnection.setDoOutput(true);
// 设置是否读入
httpURLConnection.setDoInput(true);
// 设置是否使用缓存
httpURLConnection.setUseCaches(false);
// 设置此 HttpURLConnection 实例是否应该自动执行 HTTP 重定向
httpURLConnection.setInstanceFollowRedirects(true);
// 设置请求头
// httpURLConnection.addRequestProperty("sysId", "sysId");
// 设置使用标准编码格式编码参数的名-值对
httpURLConnection.setRequestProperty("Content-Type", "application/json");
// 连接
httpURLConnection.connect();
/* 4. 处理输入输出 */
// 写入参数到请求中
String params = "{userName:\"" + userName + "\", password:\"" + password + "\"}";
OutputStream out = httpURLConnection.getOutputStream();
out.write(params.getBytes());
// 简化
//httpURLConnection.getOutputStream().write(params.getBytes());
out.flush();
out.close();
// 从连接中读取响应信息
String msg = "";
int code = httpURLConnection.getResponseCode();
if (code == 200) {
BufferedReader reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
msg += line + "\n";
}
reader.close();
}
// 处理结果
LOGGER.info(msg);
} catch (IOException e) {
LOGGER.error("转发出错,错误信息:" + e.getLocalizedMessage() + ";" + e.getClass());
} finally {
// 5. 断开连接
if (null != httpURLConnection) {
try {
httpURLConnection.disconnect();
} catch (Exception e) {
LOGGER.info("httpURLConnection 流关闭异常:" + e.getLocalizedMessage());
}
}
}
}
static TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
};
public class NullHostNameVerifier implements HostnameVerifier {
@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
}
}
|