package neoe.test;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
public class Test12 {
public static void main(String args[]) throws Exception {
Socket so = new Socket("proxy-host", proxy-port);
{
OutputStream out = so.getOutputStream();
String s = "CONNECT www.xxxxxx.com:443 HTTP/1.1\\
Host: www.xxxxxx.com:443\\
\\
";
out.write(s.getBytes());
out.flush();
System.out.println(s);
}
{
InputStream in = so.getInputStream();
BufferedReader in2 = new BufferedReader(new InputStreamReader(in));
System.out.println(in2.readLine());
}
Socket so2 = createSSLConnection(so, "www.xxxxx.com", 443);
{
OutputStream out = so2.getOutputStream();
String s = "GET / HTTP/1.1\\
Host: www.xxxxx.com:443\\
\\
";
out.write(s.getBytes());
out.flush();
System.out.println(s);
}
{
InputStream in = so2.getInputStream();
BufferedReader in2 = new BufferedReader(new InputStreamReader(in,
"GBK"));
String line;
while ((line = in2.readLine()) != null) {
System.out.println(line);
}
}
}
public static Socket createSSLConnection(Socket s0, String host, int port)
throws Exception {
boolean debug = true;
// Socket s0 = null;
// try {
// s0 = new Socket(host, port);
// s0.setSoTimeout(1000 * 60);
// } catch (Exception e) {
// throw new Exception("Cannot connect to %1 on port %2" + host + ""
// + port, e);
// }
try {
SSLSocketFactory ssf = (SSLSocketFactory) SSLSocketFactory
.getDefault();
SSLSocket sconnection = (SSLSocket) ssf.createSocket(s0, host,
port, true);
sconnection.setSoTimeout(1000 * 60);
sconnection.setEnabledProtocols(sconnection.getEnabledProtocols());
sconnection.setEnabledCipherSuites(sconnection
.getEnabledCipherSuites());
sconnection.setUseClientMode(true);
sconnection.setNeedClientAuth(false);
sconnection.setWantClientAuth(false);
sconnection.setEnableSessionCreation(true);
sconnection.startHandshake();
SSLSession sSLSession = sconnection.getSession();
if (debug && sSLSession != null) {
String cipherSuite = sSLSession.getCipherSuite();
System.out.println("Cipher suite used = " + cipherSuite);
String proto = sSLSession.getProtocol();
System.out.println("Protocol = " + proto);
}
return sconnection;
} catch (Exception e) {
throw new Exception(e);
}
}
}
-----
没有评论:
发表评论