package com.byd.mes2.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class HttpNtmlRequest {
public static void main(String[] args) throws KeyManagementException, NoSuchAlgorithmException {
test();
}
public static void test() throws KeyManagementException, NoSuchAlgorithmException {
String urlStr = "https://test.test.com";
String formData = "";
try {
urlStr += URLEncoder.encode(formData, "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
System.out.println(urlStr);
String domain = ""; // May also be referred as realm
String userName = "user";
String password = "password";
String responseText = null;
try {
// responseText = getAuthenticatedResponse(urlStr, domain, userName, password);
responseText = postAuthenticatedResponse(urlStr, domain, userName, password);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("response: " + responseText);
}
private static String getAuthenticatedResponse(final String urlStr, final String domain, final String userName,
final String password) throws IOException {
StringBuilder response = new StringBuilder();
Authenticator.setDefault(new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(domain + "\" + userName, password.toCharArray());
}
});
URL urlRequest = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) urlRequest.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("GET");
System.out.println(conn.getContentLengthLong());
System.out.println(conn.getResponseMessage());
InputStream stream = conn.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(stream));
String str = "";
while ((str = in.readLine()) != null) {
response.append(str);
}
in.close();
return response.toString();
}
private static String postAuthenticatedResponse(final String urlStr, final String domain, final String userName,
final String password) throws IOException, KeyManagementException, NoSuchAlgorithmException {
StringBuilder response = new StringBuilder();
Authenticator.setDefault(new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(domain + "\" + userName, password.toCharArray());
}
});
URL urlRequest = new URL(urlStr);
//创建SSLContext对象,并使用我们指定的信任管理器初始化
X509TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs, String string)
throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] xcs, String string)
throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
//jdk1.7默认为TLSv1.0,需要修改为TLSv1.2
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(null, new TrustManager[] { tm }, null);
//从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
// 创建HttpsURLConnection对象,并设置其SSLSocketFactory对象
HttpsURLConnection conn = (HttpsURLConnection) urlRequest.openConnection();
//创建HttpsURLConnection对象,并设置其SSLSocketFactory对象
conn.setSSLSocketFactory(ssf);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
OutputStream out = conn.getOutputStream();
// 写入请求的字符串
String obj = "";
out.write((obj.toString()).getBytes("UTF-8"));
out.flush();
InputStream in = conn.getInputStream();
// read .....
System.out.println("Responce Code: " + conn.getResponseCode());
System.out.println("Responce Message: " + conn.getResponseMessage());
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String str = "";
while ((str = br.readLine()) != null) {
response.append(str);
}
out.close();
in.close();
return response.toString();
}
}