java 过滤https证书验证

package com.test;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.security.GeneralSecurityException;
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.X509TrustManager;

public class HttpsTest {

private static X509TrustManager xtm = new X509TrustManager() {

@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
// TODO Auto-generated method stub

}

@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
// System.out.println("---cert: " + arg0[0].toString() + ", 认证方式: "
// + arg1);

}

@Override
public X509Certificate[] getAcceptedIssuers() {
// TODO Auto-generated method stub
return null;
}

};

// Create an class to trust all hosts 46
private static HostnameVerifier hnv = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
// System.out.println("hostname:111111111111111111111 "
// + hostname);
return true;
}
};

public static void main(String[] args) {

SSLContext sslContext = null;

try {
sslContext = SSLContext.getInstance("TLS");
X509TrustManager[] xtmArray = new X509TrustManager[] { xtm };
sslContext.init(null, xtmArray, new java.security.SecureRandom());
} catch (GeneralSecurityException gse) {
// 打印出一些错误信息和处理这个异常
gse.printStackTrace();
}
if (sslContext != null) {
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext
.getSocketFactory());
}
HttpsURLConnection.setDefaultHostnameVerifier(hnv);

String data = "adsfasdfasd";
String data2="";

ByteArrayOutputStream out1 = new ByteArrayOutputStream();
try {
FileInputStream fis=null;
File f=new File("c://tes.bb");
fis=new FileInputStream(f);
byte[] buf1 = new byte[1024];
int len = 0;
while ((len = fis.read(buf1)) > -1) {
out1.write(buf1, 0, len);
}
} catch (Exception e1) {

e1.printStackTrace();
}
data=new String(out1.toByteArray());
System.out.println("发送数据:\n"+data);
OutputStream dos = null;
InputStream dis = null;

try {
URL url = new URL("https://58.246.226.97:8443/easypay/entry");
HttpsURLConnection connection = (HttpsURLConnection) url
.openConnection();
// causes the VM to display a dialog when connecting
// to untrusted servers
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setAllowUserInteraction(true);
connection.setRequestProperty("Content-Type",
"application/xml;charset=UTF-8");
byte[] bytesData = data.getBytes("UTF-8");
connection.setRequestProperty("Content-Length",
String.valueOf(bytesData.length));


// connection.setRequestMethod();

dos = connection.getOutputStream();
for (int i = 0; i < bytesData.length; i++) {
dos.write(bytesData[i]);
}
dis = connection.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while ((len = dis.read(buf)) > -1) {
out.write(buf, 0, len);
}
data2 = new String(out.toByteArray());
System.out.println("返回数据:\n"+data2);
} catch (Exception e) {
e.printStackTrace();
}

}
}

你可能感兴趣的:(网络)