<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 495,comments - 227,trackbacks - 0
    http://blog.csdn.net/totogogo/article/details/1831588
    http://blog.csdn.net/pandazxx/article/details/1657109
    http://blog.csdn.net/pandazxx/article/details/1660008


    最常用的Http請求無非是get和post,get請求可以獲取靜態頁面,也可以把參數放在URL字串后面,傳遞給servlet,post與get的不同之處在于post的參數不是放在URL字串里面,而是放在http請求的正文內。
    在Java中可以使用HttpURLConnection發起這兩種請求,了解此類,對于了解soap,和編寫servlet的自動測試代碼都有很大的幫助。
    下面的代碼簡單描述了如何使用HttpURLConnection發起這兩種請求,以及傳遞參數的方法:
    public class HttpInvoker {

        
    public static final String GET_URL = "http://localhost:8080/welcome1";

        
    public static final String POST_URL = "http://localhost:8080/welcome1";

        
    public static void readContentFromGet() throws IOException {
            
    // 拼湊get請求的URL字串,使用URLEncoder.encode對特殊和不可見字符進行編碼
            String getURL = GET_URL + "?username="
                    
    + URLEncoder.encode("fat man""utf-8");
            URL getUrl 
    = new URL(getURL);
            
    // 根據拼湊的URL,打開連接,URL.openConnection函數會根據URL的類型,
            
    // 返回不同的URLConnection子類的對象,這里URL是一個http,因此實際返回的是HttpURLConnection
            HttpURLConnection connection = (HttpURLConnection) getUrl
                    .openConnection();
            
    // 進行連接,但是實際上get request要在下一句的connection.getInputStream()函數中才會真正發到
            
    // 服務器
            connection.connect();
            
    // 取得輸入流,并使用Reader讀取
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            System.out.println(
    "=============================");
            System.out.println(
    "Contents of get request");
            System.out.println(
    "=============================");
            String lines;
            
    while ((lines = reader.readLine()) != null{
                System.out.println(lines);
            }

            reader.close();
            
    // 斷開連接
            connection.disconnect();
            System.out.println(
    "=============================");
            System.out.println(
    "Contents of get request ends");
            System.out.println(
    "=============================");
        }


        
    public static void readContentFromPost() throws IOException {
            
    // Post請求的url,與get不同的是不需要帶參數
            URL postUrl = new URL(POST_URL);
            
    // 打開連接
            HttpURLConnection connection = (HttpURLConnection) postUrl
                    .openConnection();
            
    // Output to the connection. Default is
            
    // false, set to true because post
            
    // method must write something to the
            
    // connection
            
    // 設置是否向connection輸出,因為這個是post請求,參數要放在
            
    // http正文內,因此需要設為true
            connection.setDoOutput(true);
            
    // Read from the connection. Default is true.
            connection.setDoInput(true);
            
    // Set the post method. Default is GET
            connection.setRequestMethod("POST");
            
    // Post cannot use caches
            
    // Post 請求不能使用緩存
            connection.setUseCaches(false);
            
    // This method takes effects to
            
    // every instances of this class.
            
    // URLConnection.setFollowRedirects是static函數,作用于所有的URLConnection對象。
            
    // connection.setFollowRedirects(true);

            
    // This methods only
            
    // takes effacts to this
            
    // instance.
            
    // URLConnection.setInstanceFollowRedirects是成員函數,僅作用于當前函數
            connection.setInstanceFollowRedirects(true);
            
    // Set the content type to urlencoded,
            
    // because we will write
            
    // some URL-encoded content to the
            
    // connection. Settings above must be set before connect!
            
    // 配置本次連接的Content-type,配置為application/x-www-form-urlencoded的
            
    // 意思是正文是urlencoded編碼過的form參數,下面我們可以看到我們對正文內容使用URLEncoder.encode
            
    // 進行編碼
            connection.setRequestProperty("Content-Type",
                    
    "application/x-www-form-urlencoded");
            
    // 連接,從postUrl.openConnection()至此的配置必須要在connect之前完成,
            
    // 要注意的是connection.getOutputStream會隱含的進行connect。
            connection.connect();
            DataOutputStream out 
    = new DataOutputStream(connection
                    .getOutputStream());
            
    // The URL-encoded contend
            
    // 正文,正文內容其實跟get的URL中'?'后的參數字符串一致
            String content = "firstname=" + URLEncoder.encode("一個大肥人""utf-8");
            
    // DataOutputStream.writeBytes將字符串中的16位的unicode字符以8位的字符形式寫道流里面
            out.writeBytes(content); 

            out.flush();
            out.close(); 
    // flush and close
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            System.out.println(
    "=============================");
            System.out.println(
    "Contents of post request");
            System.out.println(
    "=============================");
            
    while ((line = reader.readLine()) != null{
                System.out.println(line);
            }

            System.out.println(
    "=============================");
            System.out.println(
    "Contents of post request ends");
            System.out.println(
    "=============================");
            reader.close();
            connection.disconnect();
        }


        
    /**
         * 
    @param args
         
    */

        
    public static void main(String[] args) {
            
    // TODO Auto-generated method stub
            try {
                readContentFromGet();
                readContentFromPost();
            }
     catch (IOException e) {
                
    // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }


    }

    上面的readContentFromGet()函數產生了一個get請求,傳給servlet一個username參數,值為"fat man"。
    readContentFromPost()函數產生了一個post請求,傳給servlet一個firstname參數,值為"一個大肥人"。
    HttpURLConnection.connect函數,實際上只是建立了一個與服務器的tcp連接,并沒有實際發送http請求。無論是post還是get,http請求實際上直到HttpURLConnection.getInputStream()這個函數里面才正式發送出去。

    readContentFromPost() 中,順序是重中之重,對connection對象的一切配置(那一堆set函數)都必須要在connect()函數執行之前完成。而對 outputStream的寫操作,又必須要在inputStream的讀操作之前。這些順序實際上是由http請求的格式決定的。

    http 請求實際上由兩部分組成,一個是http頭,所有關于此次http請求的配置都在http頭里面定義,一個是正文content,在connect()函 數里面,會根據HttpURLConnection對象的配置值生成http頭,因此在調用connect函數之前,就必須把所有的配置準備好。

    緊接著http頭的是http請求的正文,正文的內容通過outputStream寫入,實際上outputStream不是一個網絡流,充其量是個字符串流,往里面寫入的東西不會立即發送到網絡,而是在流關閉后,根據輸入的內容生成http正文。

    至 此,http請求的東西已經準備就緒。在getInputStream()函數調用的時候,就會把準備好的http請求正式發送到服務器了,然后返回一個 輸入流,用于讀取服務器對于此次http請求的返回信息。由于http請求在getInputStream的時候已經發送出去了(包括http頭和正 文),因此在getInputStream()函數之后對connection對象進行設置(對http頭的信息進行修改)或者寫入 outputStream(對正文進行修改)都是沒有意義的了,執行這些操作會導致異常的發生。
    posted on 2013-11-14 17:03 SIMONE 閱讀(441) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 免费国产成人高清在线观看网站| 中文字幕亚洲情99在线| 四虎永久在线精品免费网址| 美女扒开尿口给男人爽免费视频| 亚洲AV无码欧洲AV无码网站| 国产乱人免费视频| 亚洲精品岛国片在线观看| aa级一级天堂片免费观看| 18禁止看的免费污网站| 国产无限免费观看黄网站| 亚洲香蕉久久一区二区三区四区| 久久久久久亚洲精品成人| 老司机亚洲精品影院无码 | 亚洲一本综合久久| 久久久久久亚洲精品| 久久国产亚洲电影天堂| 亚洲国产成人久久综合碰碰动漫3d| 亚洲人成无码网站| 91情国产l精品国产亚洲区| 亚洲日产2021三区在线| 亚洲男人天堂2022| 免费一级全黄少妇性色生活片| 国产精品亚洲综合| 久久久精品视频免费观看 | 亚洲成a人片在线观看播放| 亚洲精品中文字幕无码A片老| 大桥未久亚洲无av码在线| AAAAA级少妇高潮大片免费看| 国产成人精品无码免费看| 好先生在线观看免费播放| 亚洲精品成人区在线观看| 亚洲日本乱码一区二区在线二产线 | 国产免费啪嗒啪嗒视频看看| 国产亚洲真人做受在线观看| 亚洲人成网站色在线观看| 国产精品免费AV片在线观看| 成年女性特黄午夜视频免费看| 亚洲av午夜福利精品一区 | 国产亚洲?V无码?V男人的天堂| 2020年亚洲天天爽天天噜| 国产免费阿v精品视频网址|