csusky

          常用鏈接

          統計

          最新評論

          SOCKET基礎 學習筆記

           

            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             //批量打印出請求頭信息
           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           //對字符串"chenxiaoyu"進行  Base64 編碼
           35           System.out.println(test.base64Encode("chenxiaoyu"));
           36           
           37           
           38            //按條件打印出相關的請求頭信息
           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 是字節到字符的一個橋梁,可以制定字符的字符編碼
           48             //下面的方法也是解決網絡編程中中文亂碼常用的方法
           49             //例如:已知某個response的編碼為utf-8
           50             //則要正確顯示返回的信息,需要把下面的 gbk 參數換為 utf-8 ,否則會出現亂碼或者無法編碼的問題
           51             BufferedReader br=new BufferedReader(new InputStreamReader(con.getInputStream(),"gbk"));
           52             
           53             String s;
           54             //下面的方法打印出新浪網首頁的信息
           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   //下面是一個Base64編碼的實現方式,所謂Base64編碼是一種加密方式,旨在使人們一眼不能看出信息的真是內容,并不能防止被破解
           81  //采用的是對每24個bit(3個字節)進行編碼,方法是按每6個bit位編碼成一個字節 則3個字節的真實數據加密后形成4個字節的數據
           82  //(在高兩位補0) 每個字節的10進制數據對應固定數組  enconding 的下標 ,如果一個新字節的編碼為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            //取第一個字節的前6位又移兩位得到一個新的字節
           94            super.write(toBase64[buffer[0]&0xfc>>2]);   
           95            //取第一個字節的后兩位左移4位得到第二個新的字節的前兩位   然后取第二個字節的前四位右移4位得到第二個新字節的后四位  
           96            //然后將兩次取得的bit位用'|'連接起來  得到第二個新的字節
           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個字節,則采用如下方式進行編碼
          113    public void flush() throws IOException {
          114        //如果只有一個字節
          115        if(i==1{
          116            //第一個新的字節的產生同前
          117            super.write(toBase64[buffer[0]&0xfc>>2]);  //base64編碼
          118            //第二個新字節  取第二個字節的后兩位左移4位得到第二個新的字節,由于后面沒有待編碼的字節,所以在低四補0000
          119            //第二個新的字節的二進制形式為  00XX 0000
          120            super.write(toBase64[(buffer[0]&0x03)<<4]);
          121            //后兩個新字節采用連個'='進行補充
          122            super.write('=');
          123            super.write('=');
          124        }

          125        //如果只有兩個字節
          126        else if (i==2{
          127            //第一、二個新字節的產生
          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 曉宇 閱讀(245) 評論(0)  編輯  收藏 所屬分類: JAVA基礎

          主站蜘蛛池模板: 靖远县| 台中市| 砀山县| 勐海县| 同心县| 宜都市| 灵山县| 临澧县| 云霄县| 峡江县| 北安市| 池州市| 保山市| 黄大仙区| 漳平市| 安图县| 富民县| 龙胜| 彭阳县| 柯坪县| 泸州市| 黄龙县| 罗江县| 保康县| 台北市| 遵义县| 武城县| 汪清县| 芦山县| 新野县| 东乌珠穆沁旗| 镇平县| 吉林省| 安多县| 郓城县| 甘泉县| 巴马| 安远县| 永川市| 大城县| 依兰县|