Https socket 代理

https直接与服务器通过ssLsocket连接可行

import java.io.InputStream;
import java.io.OutputStream;
import java.security.SecureRandom;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;


public class Ssl {

    
    public static void sslSocket2() throws Exception {    
        SSLContext context = SSLContext.getInstance("SSL");    
                   // 初始化    
        TrustManager[] tm = { new  X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
            public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            }
            public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            }
        } };
        
        context.init(null, tm, new SecureRandom());    
        SSLSocketFactory factory = context.getSocketFactory();    
        SSLSocket s = (SSLSocket) factory.createSocket("mail.163.com", 443);    
        System.out.println(s.getEnabledProtocols());
        System.out.println(s.getEnableSessionCreation());
        System.out.println(s.getUseClientMode());
//        System.out.println(s.getHandshakeSession().toString());
        System.out.println(s.getInetAddress());
        System.out.println(s.getSession().toString());
        System.out.println("ok");    
       // s.startHandshake();
        OutputStream output = s.getOutputStream();    
        InputStream input = s.getInputStream();    
        
        output.write(("POST https://mail.163.com/entry/cgi/ntesdoor?df=mail163_letter&from=web&funcid=loginone&iframe=1&language=-1&passtype=1&product=mail163&net=t&style=-1&race=1139_1154_1123_bj&uid=xj-07@163.com HTTP/1.1"
        +" Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, */*"
                +" User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)"
        +" Content-Type: application/x-www-form-urlencoded"
                +" Accept-Encoding: gzip, deflate"+" Host: mail.163.com"
        +" Content-Length: 106"+" Connection: Keep-Alive"
                +" Cache-Control: no-cache"+" Referer: http://mail.163.com/"
        +" Accept-Language: zh-CN"+" "
            +" savelogin=0&url2=http%3A%2F%2Fmail.163.com%2Ferrorpage%2Ferror163.htm&username=xj-07&password=123456").getBytes());    
        System.out.println("sent: alert");    
        output.flush();    
        
        byte[] buf = new byte[1024];    
        int len = input.read(buf);    
        System.out.println("received:" + new String(buf, 0, len));    
    }  
    public static void main(String[] args) throws Exception {
        
        sslSocket2();
    }
    
}

同样方式将

SSLSocket s = (SSLSocket) factory.createSocket("mail.163.com", 443);   

改为代理就不好使了

SSLSocket s = (SSLSocket) factory.createSocket("127.0.0.1", 8080);   

客户端与代理服务器之间的ssl握手出现了问题,还带进一步分析

原文地址:https://www.cnblogs.com/hua198/p/5223945.html