csusky

          常用鏈接

          統(tǒng)計(jì)

          最新評(píng)論

          SOCKET基礎(chǔ) 學(xué)習(xí)筆記

           

            1package com;
            2
            3import java.io.BufferedReader;
            4import java.io.ByteArrayOutputStream;
            5import java.io.FilterOutputStream;
            6import java.io.IOException;
            7import java.io.InputStreamReader;
            8import java.io.OutputStream;
            9import java.net.URL;
           10import java.net.URLConnection;
           11import java.util.Date;
           12import java.util.List;
           13import java.util.Map;
           14
           15public class URLConnectionTest {
           16        
           17     public static void main(String[] args) {
           18        
           19         try {
           20             URL url=new URL("http://www.sina.com");
           21             URLConnection con=url.openConnection();
           22             con.connect();
           23             //批量打印出請(qǐng)求頭信息
           24             Map<String,List<String>> map=con.getHeaderFields();
           25             for(Map.Entry<String, List<String>> entry:map.entrySet()) {
           26                 String key=entry.getKey();
           27                 for(String value:entry.getValue()) {
           28                     System.out.println(key+"-------------"+value);
           29                 }

           30             }

           31             
           32           
           33           URLConnectionTest test=new URLConnectionTest();
           34           //對(duì)字符串"chenxiaoyu"進(jìn)行  Base64 編碼
           35           System.out.println(test.base64Encode("chenxiaoyu"));
           36           
           37           
           38            //按條件打印出相關(guān)的請(qǐng)求頭信息
           39             System.out.println("------------------");
           40             System.out.println("ContentType():"+con.getContentType());
           41             System.out.println("getContentLength():"+con.getContentLength());
           42             System.out.println("getContentEncoding():"+con.getContentEncoding());
           43             System.out.println("getDate():"+con.getDate());
           44             System.out.println("getLastModified():"+new Date(con.getLastModified()));
           45             System.out.println("------------------");
           46             
           47             //inputStreamReader 是字節(jié)到字符的一個(gè)橋梁,可以制定字符的字符編碼
           48             //下面的方法也是解決網(wǎng)絡(luò)編程中中文亂碼常用的方法
           49             //例如:已知某個(gè)response的編碼為utf-8
           50             //則要正確顯示返回的信息,需要把下面的 gbk 參數(shù)換為 utf-8 ,否則會(huì)出現(xiàn)亂碼或者無法編碼的問題
           51             BufferedReader br=new BufferedReader(new InputStreamReader(con.getInputStream(),"gbk"));
           52             
           53             String s;
           54             //下面的方法打印出新浪網(wǎng)首頁(yè)的信息
           55             while((s=br.readLine())!=null{
           56                 System.out.println(s);
           57             }

           58             
           59             
           60         }
           catch(Exception e) {
           61             e.printStackTrace();
           62         }

           63         
           64     }

           65     
           66     public  String base64Encode(String s){
           67         OutputStream bao=new ByteArrayOutputStream();
           68         Base64OutputStream b64out=new Base64OutputStream(bao);
           69         try {
           70             b64out.write(s.getBytes());
           71             b64out.flush();
           72             }
           catch(Exception e) {
           73                 e.printStackTrace();
           74             }

           75             return bao.toString();
           76         }

           77         
           78     }

           79     
           80   //下面是一個(gè)Base64編碼的實(shí)現(xiàn)方式,所謂Base64編碼是一種加密方式,旨在使人們一眼不能看出信息的真是內(nèi)容,并不能防止被破解
           81  //采用的是對(duì)每24個(gè)bit(3個(gè)字節(jié))進(jìn)行編碼,方法是按每6個(gè)bit位編碼成一個(gè)字節(jié) 則3個(gè)字節(jié)的真實(shí)數(shù)據(jù)加密后形成4個(gè)字節(jié)的數(shù)據(jù)
           82  //(在高兩位補(bǔ)0) 每個(gè)字節(jié)的10進(jìn)制數(shù)據(jù)對(duì)應(yīng)固定數(shù)組  enconding 的下標(biāo) ,如果一個(gè)新字節(jié)的編碼為i則其編碼字符為enconding[i]
           83class Base64OutputStream extends FilterOutputStream {
           84    
           85    public Base64OutputStream(OutputStream os) {
           86        super(os);
           87    }

           88    
           89    public void write(int c) throws IOException {
           90        buffer[i]=c;
           91        i++;
           92        if(i==3{
           93            //取第一個(gè)字節(jié)的前6位又移兩位得到一個(gè)新的字節(jié)
           94            super.write(toBase64[buffer[0]&0xfc>>2]);   
           95            //取第一個(gè)字節(jié)的后兩位左移4位得到第二個(gè)新的字節(jié)的前兩位   然后取第二個(gè)字節(jié)的前四位右移4位得到第二個(gè)新字節(jié)的后四位  
           96            //然后將兩次取得的bit位用'|'連接起來  得到第二個(gè)新的字節(jié)
           97            super.write(toBase64[((buffer[0]&0x03)<<4)|((buffer[1])&0xf0>>4)]);
           98        
           99            super.write(toBase64[((buffer[1]&0x0f)<<2)|((buffer[2]&0xc0)>>6)]);
          100            super.write(toBase64[(buffer[2]&0x3f)]);
          101            col+=4;
          102            i=0;
          103            if(col>=76{
          104                super.write('\n');
          105                col=0;
          106            }

          107            
          108        }
                  
          109         
          110    }

          111    
          112    //如果待編碼的字符串不足3個(gè)字節(jié),則采用如下方式進(jìn)行編碼
          113    public void flush() throws IOException {
          114        //如果只有一個(gè)字節(jié)
          115        if(i==1{
          116            //第一個(gè)新的字節(jié)的產(chǎn)生同前
          117            super.write(toBase64[buffer[0]&0xfc>>2]);  //base64編碼
          118            //第二個(gè)新字節(jié)  取第二個(gè)字節(jié)的后兩位左移4位得到第二個(gè)新的字節(jié),由于后面沒有待編碼的字節(jié),所以在低四補(bǔ)0000
          119            //第二個(gè)新的字節(jié)的二進(jìn)制形式為  00XX 0000
          120            super.write(toBase64[(buffer[0]&0x03)<<4]);
          121            //后兩個(gè)新字節(jié)采用連個(gè)'='進(jìn)行補(bǔ)充
          122            super.write('=');
          123            super.write('=');
          124        }

          125        //如果只有兩個(gè)字節(jié)
          126        else if (i==2{
          127            //第一、二個(gè)新字節(jié)的產(chǎn)生
          128            super.write(toBase64[buffer[0]&0xfc]>>2);
          129            super.write(toBase64[((buffer[0]&0x03)<<4)|((buffer[1])&0xf0>>4)]);
          130            super.write(toBase64[((buffer[1]&0x0f)<<2)]);
          131            super.write('=');            
          132        }

          133    }

          134    
          135    
          136    
          137    private static char[] toBase64=
          138    {
          139     'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
          140     'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
          141     'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
          142     'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
          143     }
          ;
          144    
          145    private int i=0;
          146    
          147    private int col=0;
          148    
          149    private int[] buffer=new int[3];
          150    
          151    
          152    
          153}

          154
          155
          156
          157
          158
          159
          160
          161

          posted on 2008-02-22 16:26 曉宇 閱讀(244) 評(píng)論(0)  編輯  收藏 所屬分類: JAVA基礎(chǔ)

          主站蜘蛛池模板: 饶平县| 两当县| 吉水县| 邓州市| 东丽区| 延长县| 宁海县| 敖汉旗| 桂阳县| 甘德县| 仁寿县| 凉城县| 松桃| 博爱县| 辽中县| 五华县| 江达县| 德惠市| 上饶县| 石首市| 确山县| 米易县| 伊宁县| 谢通门县| 容城县| 鄂伦春自治旗| 麻阳| 凤山县| 雅安市| 南召县| 沁源县| 定兴县| 岚皋县| 米易县| 杭锦旗| 新化县| 灵武市| 卢湾区| 红原县| 榆林市| 怀化市|