X509TrustManager sunX509TrustManager; public MyX509TrustManager() {
try {
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509", "SunJSSE"); sunX509TrustManager = (X509TrustManager)tmf.getTrustManagers()[0];
} catch (Exception exception) {
}
} public X509Certificate [] getAcceptedIssuers() {
try {
if (sunX509TrustManager != null)
return sunX509TrustManager.getAcceptedIssuers(); else
return null;
} catch (Exception exception) {
return null;
}
}
public B2CConnection(boolean useSSL) {
bufLen = 4 * 1024 * 1024; buffer = new byte[bufLen]; try {
if (useSSL) {
TrustManager myTM [] = { new MyX509TrustManager() };
System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
SSLContext context = SSLContext.getInstance("SSLv3");
context.init(null, myTM, null);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public String sendAndReceive(String srcUrl,String srcContent) {
HttpURLConnection connection = null; InputStream in = null;
DataOutputStream out = null; try {
URL url = new URL(srcUrl); connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.setRequestMethod("POST");
out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(srcContent);
out.flush();
out.close();
int contentLen = connection.getContentLength();
in = connection.getInputStream();
if (contentLen <= 0) {
contentLen = bufLen; int offset = 0; do {
int len = in.read(buffer, offset, contentLen - offset); if (len <= 0)
break; offset += len;
}while (true); contentLen = offset;
} else {
if (contentLen > bufLen) {
buffer = new byte[contentLen]; bufLen = contentLen;
} int offset = 0; do {
int len = in.read(buffer, offset, contentLen - offset); if (len <= 0)
break; offset += len;
}while (true); contentLen = offset;
} String resMsg = connection.getHeaderField(0); if (resMsg.toLowerCase().indexOf("ok") < 0) {
return null;
} connection.disconnect();
in.close();
String content = new String(buffer, 0, contentLen,"GBK");
return content;
} catch (Exception e) {
if (connection != null) {
connection.disconnect();
} if (in != null) {
try {
in.close();
} catch (IOException e1) {
}
} e.printStackTrace();
lastErr = e.getMessage();
} return null;
}
如上部分代码! 为什么我客户端ssl请求过去,在服务端防火墙上发现是SSLv2呢! 我服务端ssl是3.0版本的!所以导致
通讯协议版本不对,被拒绝了! 大家帮我分下下!怎么回事! 该怎么检查? windows2003系统 项目在j2sdk1.4 下运行!
try {
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509", "SunJSSE"); sunX509TrustManager = (X509TrustManager)tmf.getTrustManagers()[0];
} catch (Exception exception) {
}
} public X509Certificate [] getAcceptedIssuers() {
try {
if (sunX509TrustManager != null)
return sunX509TrustManager.getAcceptedIssuers(); else
return null;
} catch (Exception exception) {
return null;
}
}
public B2CConnection(boolean useSSL) {
bufLen = 4 * 1024 * 1024; buffer = new byte[bufLen]; try {
if (useSSL) {
TrustManager myTM [] = { new MyX509TrustManager() };
System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
SSLContext context = SSLContext.getInstance("SSLv3");
context.init(null, myTM, null);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public String sendAndReceive(String srcUrl,String srcContent) {
HttpURLConnection connection = null; InputStream in = null;
DataOutputStream out = null; try {
URL url = new URL(srcUrl); connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.setRequestMethod("POST");
out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(srcContent);
out.flush();
out.close();
int contentLen = connection.getContentLength();
in = connection.getInputStream();
if (contentLen <= 0) {
contentLen = bufLen; int offset = 0; do {
int len = in.read(buffer, offset, contentLen - offset); if (len <= 0)
break; offset += len;
}while (true); contentLen = offset;
} else {
if (contentLen > bufLen) {
buffer = new byte[contentLen]; bufLen = contentLen;
} int offset = 0; do {
int len = in.read(buffer, offset, contentLen - offset); if (len <= 0)
break; offset += len;
}while (true); contentLen = offset;
} String resMsg = connection.getHeaderField(0); if (resMsg.toLowerCase().indexOf("ok") < 0) {
return null;
} connection.disconnect();
in.close();
String content = new String(buffer, 0, contentLen,"GBK");
return content;
} catch (Exception e) {
if (connection != null) {
connection.disconnect();
} if (in != null) {
try {
in.close();
} catch (IOException e1) {
}
} e.printStackTrace();
lastErr = e.getMessage();
} return null;
}
如上部分代码! 为什么我客户端ssl请求过去,在服务端防火墙上发现是SSLv2呢! 我服务端ssl是3.0版本的!所以导致
通讯协议版本不对,被拒绝了! 大家帮我分下下!怎么回事! 该怎么检查? windows2003系统 项目在j2sdk1.4 下运行!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货