中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

用SSLSocketFactory 連接https的地址

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
package com.wtf.demo.socket;
 
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.*;
 
/**
 * Created by wtf on 2015/12/28.
 */
public class HTTPSClient {
 
    public static void main(String[] args) {
        int port = 443;
        String host = "sp0.baidu.com";
 
        SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
        SSLSocket socket  = null;
        try {
            socket = (SSLSocket)factory.createSocket(host,port);
 
            //啟用密碼組
            String[] supportedCipherSuites = socket.getSupportedCipherSuites();
            socket.setEnabledCipherSuites(supportedCipherSuites);
 
            Writer out = new OutputStreamWriter(socket.getOutputStream(),"UTF-8");
 
            //https在get中需要完全的URL
            out.write("GET https://" + host + "/ HTTP/1.1\r\n");
            out.write("Host:" + host + "\r\n");
            out.write("\r\n");
            out.flush();
 
 
            //讀取相應(yīng)
 
            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
 
            //讀取首部
            String s;
            while(!(s=reader.readLine()).equals("")){
                System.out.println(s);
            }
            System.out.println();
 
            //讀取長(zhǎng)度
            String contentLength = reader.readLine();
            int length = Integer.MAX_VALUE;
            try{
                length = Integer.parseInt(contentLength.trim(),16);
            }catch (NumberFormatException e){
               // e.printStackTrace();
                //這個(gè)服務(wù)器在響應(yīng)題的第一行沒(méi)有發(fā)送content-length
            }
 
            int c ;
            int i =0 ;
 
            while ((c= reader.read())!=-1 && i++ <length){
                System.out.write(c);
            }
 
            System.out.println();
 
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try{
                if(socket!=null){
                  socket.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
 
}

標(biāo)簽: ssl 服務(wù)器

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:使用TransitionDrawable實(shí)現(xiàn)漸變效果

下一篇: android-cardview簡(jiǎn)單使用