HttpURLConnection學(xué)習(xí)
最常用的Http請求無非是get和post,get請求可以獲取靜態(tài)頁面,也可以把參數(shù)放在URL字串后面,傳遞給servlet,post與get的不同之處在于post的參數(shù)不是放在URL字串里面,而是放在http請求的正文內(nèi)。
在Java中可以使用HttpURLConnection發(fā)起這兩種請求,了解此類,對于了解soap,和編寫servlet的自動測試代碼都有很大的幫助。
下面的代碼簡單描述了如何使用HttpURLConnection發(fā)起這兩種請求,以及傳遞參數(shù)的方法:
- 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);
- // 根據(jù)拼湊的URL,打開連接,URL.openConnection函數(shù)會根據(jù)URL的類型,
- // 返回不同的URLConnection子類的對象,這里URL是一個http,因此實際返回的是HttpURLConnection
- HttpURLConnection connection = (HttpURLConnection) getUrl
- .openConnection();
- // 進行連接,但是實際上get request要在下一句的connection.getInputStream()函數(shù)中才會真正發(fā)到
- // 服務(wù)器
- 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不同的是不需要帶參數(shù)
- 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
- // 設(shè)置是否向connection輸出,因為這個是post請求,參數(shù)要放在
- // http正文內(nèi),因此需要設(shè)為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函數(shù),作用于所有的URLConnection對象。
- // connection.setFollowRedirects(true);
- // This methods only
- // takes effacts to this
- // instance.
- // URLConnection.setInstanceFollowRedirects是成員函數(shù),僅作用于當(dāng)前函數(shù)
- 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參數(shù),下面我們可以看到我們對正文內(nèi)容使用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
- // 正文,正文內(nèi)容其實跟get的URL中'?'后的參數(shù)字符串一致
- 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()函數(shù)產(chǎn)生了一個get請求,傳給servlet一個username參數(shù),值為"fat man"。
readContentFromPost()函數(shù)產(chǎn)生了一個post請求,傳給servlet一個firstname參數(shù),值為"一個大肥人"。
HttpURLConnection.connect函數(shù),實際上只是建立了一個與服務(wù)器的tcp連接,并沒有實際發(fā)送http請求。無論是post還是get,http請求實際上直到HttpURLConnection.getInputStream()這個函數(shù)里面才正式發(fā)送出去。
在readContentFromPost() 中,順序是重中之重,對connection對象的一切配置(那一堆set函數(shù))都必須要在connect()函數(shù)執(zhí)行之前完成。而對 outputStream的寫操作,又必須要在inputStream的讀操作之前。這些順序?qū)嶋H上是由http請求的格式?jīng)Q定的。
http 請求實際上由兩部分組成,一個是http頭,所有關(guān)于此次http請求的配置都在http頭里面定義,一個是正文content,在connect()函數(shù)里面,會根據(jù)HttpURLConnection對象的配置值生成http頭,因此在調(diào)用connect函數(shù)之前,就必須把所有的配置準備好。
緊接著http頭的是http請求的正文,正文的內(nèi)容通過outputStream寫入,實際上outputStream不是一個網(wǎng)絡(luò)流,充其量是個字符串流,往里面寫入的東西不會立即發(fā)送到網(wǎng)絡(luò),而是在流關(guān)閉后,根據(jù)輸入的內(nèi)容生成http正文。
至此,http請求的東西已經(jīng)準備就緒。在getInputStream()函數(shù)調(diào)用的時候,就會把準備好的http請求正式發(fā)送到服務(wù)器了,然后返回一個輸入流,用于讀取服務(wù)器對于此次http請求的返回信息。由于http請求在getInputStream的時候已經(jīng)發(fā)送出去了(包括http頭和正文),因此在getInputStream()函數(shù)之后對connection對象進行設(shè)置(對http頭的信息進行修改)或者寫入 outputStream(對正文進行修改)都是沒有意義的了,執(zhí)行這些操作會導(dǎo)致異常的發(fā)生
上節(jié)說道,post請求的OutputStream實際上不是網(wǎng)絡(luò)流,而是寫入內(nèi)存,在getInputStream中才真正把寫道流里面的內(nèi)容作為正文與根據(jù)之前的配置生成的http request頭合并成真正的http request,并在此時才真正向服務(wù)器發(fā)送。
HttpURLConnection.setChunkedStreamingMode 函數(shù)可以改變這個模式,設(shè)置了ChunkedStreamingMode后,不再等待OutputStream關(guān)閉后生成完整的http request一次過發(fā)送,而是先發(fā)送http request頭,正文內(nèi)容則是網(wǎng)路流的方式實時傳送到服務(wù)器。實際上是不告訴服務(wù)器http正文的長度,這種模式適用于向服務(wù)器傳送較大的或者是不容易獲取長度的數(shù)據(jù),如文件。
- public static void readContentFromChunkedPost() throws IOException {
- URL postUrl = new URL(POST_URL);
- HttpURLConnection connection = (HttpURLConnection) postUrl
- .openConnection();
- connection.setDoOutput( true );
- connection.setDoInput( true );
- connection.setRequestMethod( " POST " );
- connection.setUseCaches( false );
- connection.setInstanceFollowRedirects( true );
- connection.setRequestProperty( " Content-Type " ,
- " application/x-www-form-urlencoded " );
- /** //*
- * 與readContentFromPost()***的不同,設(shè)置了塊大小為5字節(jié)
- */
- connection.setChunkedStreamingMode( 5 );
- connection.connect();
- /** //*
- * 注意,下面的getOutputStream函數(shù)工作方式于在readContentFromPost()里面的不同
- * 在readContentFromPost()里面該函數(shù)仍在準備http request,沒有向服務(wù)器發(fā)送任何數(shù)據(jù)
- * 而在這里由于設(shè)置了ChunkedStreamingMode,getOutputStream函數(shù)會根據(jù)connect之前的配置
- * 生成http request頭,先發(fā)送到服務(wù)器。
- */
- DataOutputStream out = new DataOutputStream(connection
- .getOutputStream());
- String content = " firstname= " + URLEncoder.encode( " 一個大肥人 " +
- " " +
- " asdfasfdasfasdfaasdfasdfasdfdasfs " , " utf-8 " );
- out.writeBytes(content);
- out.flush();
- out.close(); // 到此時服務(wù)器已經(jīng)收到了完整的http request了,而在readContentFromPost()函數(shù)里,要等到下一句服務(wù)器才能收到http請求。
- BufferedReader reader = new BufferedReader( new InputStreamReader(
- connection.getInputStream()));
- out.flush();
- out.close(); // flush and close
- 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();
- }