成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

HttpURLConnection學(xué)習(xí)

移動(dòng)開發(fā) Android
最常用的Http請(qǐng)求無(wú)非是get和post,get請(qǐng)求可以獲取靜態(tài)頁(yè)面,也可以把參數(shù)放在URL字串后面,傳遞給servlet,post與get的不同之處在于post的參數(shù)不是放在URL字串里面,而是放在http請(qǐng)求的正文內(nèi)。

最常用的Http請(qǐng)求無(wú)非是get和post,get請(qǐng)求可以獲取靜態(tài)頁(yè)面,也可以把參數(shù)放在URL字串后面,傳遞給servlet,post與get的不同之處在于post的參數(shù)不是放在URL字串里面,而是放在http請(qǐng)求的正文內(nèi)。
在Java中可以使用HttpURLConnection發(fā)起這兩種請(qǐng)求,了解此類,對(duì)于了解soap,和編寫servlet的自動(dòng)測(cè)試代碼都有很大的幫助。
下面的代碼簡(jiǎn)單描述了如何使用HttpURLConnection發(fā)起這兩種請(qǐng)求,以及傳遞參數(shù)的方法:

 

  1.  public   class  HttpInvoker { 
  2.  
  3.      public   static   final  String GET_URL  =   " http://localhost:8080/welcome1 " ; 
  4.  
  5.      public   static   final  String POST_URL  =   " http://localhost:8080/welcome1 " ; 
  6.  
  7.      public   static   void  readContentFromGet()  throws  IOException { 
  8.          //  拼湊get請(qǐng)求的URL字串,使用URLEncoder.encode對(duì)特殊和不可見字符進(jìn)行編碼 
  9.         String getURL  =  GET_URL  +   " ?username= " 
  10.                  +  URLEncoder.encode( " fat man " ,  " utf-8 " ); 
  11.         URL getUrl  =   new  URL(getURL); 
  12.          //  根據(jù)拼湊的URL,打開連接,URL.openConnection函數(shù)會(huì)根據(jù)URL的類型, 
  13.          //  返回不同的URLConnection子類的對(duì)象,這里URL是一個(gè)http,因此實(shí)際返回的是HttpURLConnection 
  14.         HttpURLConnection connection  =  (HttpURLConnection) getUrl 
  15.                 .openConnection(); 
  16.          //  進(jìn)行連接,但是實(shí)際上get request要在下一句的connection.getInputStream()函數(shù)中才會(huì)真正發(fā)到 
  17.          //  服務(wù)器 
  18.         connection.connect(); 
  19.          //  取得輸入流,并使用Reader讀取 
  20.         BufferedReader reader  =   new  BufferedReader( new  InputStreamReader( 
  21.                 connection.getInputStream())); 
  22.         System.out.println( " ============================= " ); 
  23.         System.out.println( " Contents of get request " ); 
  24.         System.out.println( " ============================= " ); 
  25.         String lines; 
  26.          while  ((lines  =  reader.readLine())  !=   null ) { 
  27.             System.out.println(lines); 
  28.         } 
  29.         reader.close(); 
  30.          //  斷開連接 
  31.         connection.disconnect(); 
  32.         System.out.println( " ============================= " ); 
  33.         System.out.println( " Contents of get request ends " ); 
  34.         System.out.println( " ============================= " ); 
  35.     } 
  36.  
  37.      public   static   void  readContentFromPost()  throws  IOException { 
  38.          //  Post請(qǐng)求的url,與get不同的是不需要帶參數(shù) 
  39.         URL postUrl  =   new  URL(POST_URL); 
  40.          //  打開連接 
  41.         HttpURLConnection connection  =  (HttpURLConnection) postUrl 
  42.                 .openConnection(); 
  43.          //  Output to the connection. Default is 
  44.          //  false, set to true because post 
  45.          //  method must write something to the 
  46.          //  connection 
  47.          //  設(shè)置是否向connection輸出,因?yàn)檫@個(gè)是post請(qǐng)求,參數(shù)要放在 
  48.          //  http正文內(nèi),因此需要設(shè)為true 
  49.         connection.setDoOutput( true ); 
  50.          //  Read from the connection. Default is true. 
  51.         connection.setDoInput( true ); 
  52.          //  Set the post method. Default is GET 
  53.         connection.setRequestMethod( " POST " ); 
  54.          //  Post cannot use caches 
  55.          //  Post 請(qǐng)求不能使用緩存 
  56.         connection.setUseCaches( false ); 
  57.          //  This method takes effects to 
  58.          //  every instances of this class. 
  59.          //  URLConnection.setFollowRedirects是static函數(shù),作用于所有的URLConnection對(duì)象。 
  60.          //  connection.setFollowRedirects(true); 
  61.  
  62.          //  This methods only 
  63.          //  takes effacts to this 
  64.          //  instance. 
  65.          //  URLConnection.setInstanceFollowRedirects是成員函數(shù),僅作用于當(dāng)前函數(shù) 
  66.         connection.setInstanceFollowRedirects( true ); 
  67.          //  Set the content type to urlencoded, 
  68.          //  because we will write 
  69.          //  some URL-encoded content to the 
  70.          //  connection. Settings above must be set before connect! 
  71.          //  配置本次連接的Content-type,配置為application/x-www-form-urlencoded的 
  72.          //  意思是正文是urlencoded編碼過(guò)的form參數(shù),下面我們可以看到我們對(duì)正文內(nèi)容使用URLEncoder.encode 
  73.          //  進(jìn)行編碼 
  74.         connection.setRequestProperty( " Content-Type " , 
  75.                  " application/x-www-form-urlencoded " ); 
  76.          //  連接,從postUrl.openConnection()至此的配置必須要在connect之前完成, 
  77.          //  要注意的是connection.getOutputStream會(huì)隱含的進(jìn)行connect。 
  78.         connection.connect(); 
  79.         DataOutputStream out  =   new  DataOutputStream(connection 
  80.                 .getOutputStream()); 
  81.          //  The URL-encoded contend 
  82.          //  正文,正文內(nèi)容其實(shí)跟get的URL中'?'后的參數(shù)字符串一致 
  83.         String content  =   " firstname= "   +  URLEncoder.encode( " 一個(gè)大肥人 " ,  " utf-8 " ); 
  84.          //  DataOutputStream.writeBytes將字符串中的16位的unicode字符以8位的字符形式寫道流里面 
  85.         out.writeBytes(content);  
  86.  
  87.         out.flush(); 
  88.         out.close();  //  flush and close 
  89.         BufferedReader reader  =   new  BufferedReader( new  InputStreamReader( 
  90.                 connection.getInputStream())); 
  91.         String line; 
  92.         System.out.println( " ============================= " ); 
  93.         System.out.println( " Contents of post request " ); 
  94.         System.out.println( " ============================= " ); 
  95.          while  ((line  =  reader.readLine())  !=   null ) { 
  96.             System.out.println(line); 
  97.         } 
  98.         System.out.println( " ============================= " ); 
  99.         System.out.println( " Contents of post request ends " ); 
  100.         System.out.println( " ============================= " ); 
  101.         reader.close(); 
  102.         connection.disconnect(); 
  103.     } 
  104.  
  105.      /**   *//** 
  106.      *  @param  args 
  107.       */ 
  108.      public   static   void  main(String[] args) { 
  109.          //  TODO Auto-generated method stub 
  110.          try  { 
  111.             readContentFromGet(); 
  112.             readContentFromPost(); 
  113.         }  catch  (IOException e) { 
  114.              //  TODO Auto-generated catch block 
  115.             e.printStackTrace(); 
  116.         } 
  117.     } 
  118.  

上面的readContentFromGet()函數(shù)產(chǎn)生了一個(gè)get請(qǐng)求,傳給servlet一個(gè)username參數(shù),值為"fat man"。
readContentFromPost()函數(shù)產(chǎn)生了一個(gè)post請(qǐng)求,傳給servlet一個(gè)firstname參數(shù),值為"一個(gè)大肥人"。
HttpURLConnection.connect函數(shù),實(shí)際上只是建立了一個(gè)與服務(wù)器的tcp連接,并沒(méi)有實(shí)際發(fā)送http請(qǐng)求。無(wú)論是post還是get,http請(qǐng)求實(shí)際上直到HttpURLConnection.getInputStream()這個(gè)函數(shù)里面才正式發(fā)送出去。

在readContentFromPost() 中,順序是重中之重,對(duì)connection對(duì)象的一切配置(那一堆set函數(shù))都必須要在connect()函數(shù)執(zhí)行之前完成。而對(duì) outputStream的寫操作,又必須要在inputStream的讀操作之前。這些順序?qū)嶋H上是由http請(qǐng)求的格式?jīng)Q定的。

http 請(qǐng)求實(shí)際上由兩部分組成,一個(gè)是http頭,所有關(guān)于此次http請(qǐng)求的配置都在http頭里面定義,一個(gè)是正文content,在connect()函數(shù)里面,會(huì)根據(jù)HttpURLConnection對(duì)象的配置值生成http頭,因此在調(diào)用connect函數(shù)之前,就必須把所有的配置準(zhǔn)備好。

緊接著http頭的是http請(qǐng)求的正文,正文的內(nèi)容通過(guò)outputStream寫入,實(shí)際上outputStream不是一個(gè)網(wǎng)絡(luò)流,充其量是個(gè)字符串流,往里面寫入的東西不會(huì)立即發(fā)送到網(wǎng)絡(luò),而是在流關(guān)閉后,根據(jù)輸入的內(nèi)容生成http正文。

至此,http請(qǐng)求的東西已經(jīng)準(zhǔn)備就緒。在getInputStream()函數(shù)調(diào)用的時(shí)候,就會(huì)把準(zhǔn)備好的http請(qǐng)求正式發(fā)送到服務(wù)器了,然后返回一個(gè)輸入流,用于讀取服務(wù)器對(duì)于此次http請(qǐng)求的返回信息。由于http請(qǐng)求在getInputStream的時(shí)候已經(jīng)發(fā)送出去了(包括http頭和正文),因此在getInputStream()函數(shù)之后對(duì)connection對(duì)象進(jìn)行設(shè)置(對(duì)http頭的信息進(jìn)行修改)或者寫入 outputStream(對(duì)正文進(jìn)行修改)都是沒(méi)有意義的了,執(zhí)行這些操作會(huì)導(dǎo)致異常的發(fā)生
上節(jié)說(shuō)道,post請(qǐng)求的OutputStream實(shí)際上不是網(wǎng)絡(luò)流,而是寫入內(nèi)存,在getInputStream中才真正把寫道流里面的內(nèi)容作為正文與根據(jù)之前的配置生成的http request頭合并成真正的http request,并在此時(shí)才真正向服務(wù)器發(fā)送。

HttpURLConnection.setChunkedStreamingMode 函數(shù)可以改變這個(gè)模式,設(shè)置了ChunkedStreamingMode后,不再等待OutputStream關(guān)閉后生成完整的http request一次過(guò)發(fā)送,而是先發(fā)送http request頭,正文內(nèi)容則是網(wǎng)路流的方式實(shí)時(shí)傳送到服務(wù)器。實(shí)際上是不告訴服務(wù)器http正文的長(zhǎng)度,這種模式適用于向服務(wù)器傳送較大的或者是不容易獲取長(zhǎng)度的數(shù)據(jù),如文件。

 

  1. public   static   void  readContentFromChunkedPost()  throws  IOException { 
  2.         URL postUrl  =   new  URL(POST_URL); 
  3.         HttpURLConnection connection  =  (HttpURLConnection) postUrl 
  4.                 .openConnection(); 
  5.         connection.setDoOutput( true ); 
  6.         connection.setDoInput( true ); 
  7.         connection.setRequestMethod( " POST " ); 
  8.         connection.setUseCaches( false ); 
  9.         connection.setInstanceFollowRedirects( true ); 
  10.         connection.setRequestProperty( " Content-Type " , 
  11.                  " application/x-www-form-urlencoded " ); 
  12.          /** //* 
  13.          * 與readContentFromPost()***的不同,設(shè)置了塊大小為5字節(jié) 
  14.           */ 
  15.         connection.setChunkedStreamingMode( 5 ); 
  16.         connection.connect(); 
  17.          /** //* 
  18.          * 注意,下面的getOutputStream函數(shù)工作方式于在readContentFromPost()里面的不同 
  19.          * 在readContentFromPost()里面該函數(shù)仍在準(zhǔn)備http request,沒(méi)有向服務(wù)器發(fā)送任何數(shù)據(jù) 
  20.          * 而在這里由于設(shè)置了ChunkedStreamingMode,getOutputStream函數(shù)會(huì)根據(jù)connect之前的配置 
  21.          * 生成http request頭,先發(fā)送到服務(wù)器。 
  22.           */ 
  23.         DataOutputStream out  =   new  DataOutputStream(connection 
  24.                 .getOutputStream()); 
  25.         String content  =   " firstname= "   +  URLEncoder.encode( " 一個(gè)大肥人                                                                                "   + 
  26.                  "                                            "   + 
  27.                  " asdfasfdasfasdfaasdfasdfasdfdasfs " ,  " utf-8 " ); 
  28.         out.writeBytes(content);  
  29.  
  30.         out.flush(); 
  31.         out.close();  //  到此時(shí)服務(wù)器已經(jīng)收到了完整的http request了,而在readContentFromPost()函數(shù)里,要等到下一句服務(wù)器才能收到http請(qǐng)求。 
  32.         BufferedReader reader  =   new  BufferedReader( new  InputStreamReader( 
  33.                 connection.getInputStream())); 
  34.          
  35.         out.flush(); 
  36.         out.close();  //  flush and close 
  37.         String line; 
  38.         System.out.println( " ============================= " ); 
  39.         System.out.println( " Contents of post request " ); 
  40.         System.out.println( " ============================= " ); 
  41.          while  ((line  =  reader.readLine())  !=   null ) { 
  42.             System.out.println(line); 
  43.         } 
  44.         System.out.println( " ============================= " ); 
  45.         System.out.println( " Contents of post request ends " ); 
  46.         System.out.println( " ============================= " ); 
  47.         reader.close(); 
  48.         connection.disconnect(); 
  49.     } 

 

責(zé)任編輯:chenqingxiang 來(lái)源: oschina
相關(guān)推薦

2014-08-15 13:11:03

HttpURLConn

2016-12-15 08:28:34

HttpURLConn上傳文件

2010-01-25 11:09:58

Android Htt

2016-03-24 13:57:59

JavaHttpURLConn

2024-05-09 08:30:57

OkHttpHTTP客戶端

2022-04-08 08:26:03

JavaHTTP請(qǐng)求

2022-01-14 12:28:18

架構(gòu)OpenFeign遠(yuǎn)程

2021-03-08 11:28:59

人工智能深度學(xué)習(xí)Python

2020-10-09 09:57:26

深度學(xué)習(xí)技術(shù)人工智能

2020-12-16 15:56:26

機(jī)器學(xué)習(xí)人工智能Python

2011-07-22 15:50:06

Cocoa MVC 視圖

2018-03-26 20:12:42

深度學(xué)習(xí)

2020-03-02 17:03:32

深度學(xué)習(xí)人工智能機(jī)器學(xué)習(xí)

2017-11-24 10:43:43

Madlib機(jī)器學(xué)習(xí)

2020-06-18 16:05:20

機(jī)器學(xué)習(xí)人工智能算法

2009-10-16 15:48:43

如何學(xué)習(xí)Java

2015-08-19 15:05:04

oracle

2020-11-26 16:25:45

AI

2016-11-01 15:32:43

深度學(xué)習(xí)

2020-11-06 09:00:00

機(jī)器學(xué)習(xí)集成學(xué)習(xí)人工智能
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 国产成人小视频 | 久久久无码精品亚洲日韩按摩 | av免费网| 成人av在线播放 | 四虎影院在线免费观看 | 日韩一区二区三区在线观看视频 | 91在线免费观看 | 色婷婷综合久久久中文字幕 | 欧美综合自拍 | 久久久久欧美 | 激情久久网 | 欧美综合久久久 | 午夜网| 亚洲精品一区二区三区丝袜 | 精品国产乱码久久久久久丨区2区 | 国产精品视频观看 | 亚洲av毛片成人精品 | 亚洲国产精品久久久久婷婷老年 | 亚洲福利在线视频 | 国产91精品久久久久久久网曝门 | 欧美极品在线视频 | 亚洲国产精品久久 | 国产在线小视频 | 在线观看欧美一区 | 国产一区二区免费在线 | 日韩国产精品一区二区三区 | 欧美在线色| av国产精品 | 在线视频中文字幕 | 操久久久| 超级黄色一级片 | 99久久免费精品国产免费高清 | 精品视频在线播放 | 国产高清在线精品一区二区三区 | 狠狠的操 | 亚洲电影第1页 | 97狠狠干| 曰韩三级 | 一级欧美视频 | 久久成人亚洲 | 日本在线免费看最新的电影 |