国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Java發送http請求的示例(get與post方法請求)

瀏覽:4日期:2022-08-18 17:57:52

package com.jiucool.www.struts.action; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder;public class post_request { public static final String GET_URL = 'http://www.cngolon.com/request.action?key=j0r56u2'; public static final String POST_URL = 'http://www.cngolon.com/request.action'; //get()請求 public static void readContentFromGet() throws IOException { // 拼湊get請求的URL字串,使用URLEncoder.encode對特殊和不可見字符進行編碼 String getURL = GET_URL + '&activatecode=' + URLEncoder.encode('中國聚龍', '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(), 'utf-8') ); //設置編碼,否則中文亂碼 System.out.println('============================='); System.out.println('Contents of get request'); System.out.println('============================='); String lines; while ((lines = reader.readLine()) != null) { //lines = new String(lines.getBytes(), 'utf-8'); System.out.println(lines); } reader.close(); // 斷開連接 connection.disconnect(); System.out.println('============================='); System.out.println('Contents of get request ends'); System.out.println('============================='); } //post()請求 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 = 'key=j0r53nmbbd78x7m1pqml06u2&type=1&[email protected]' + '&activatecode=' + 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(), 'utf-8') ); //設置編碼,否則中文亂碼 String line = ''; System.out.println('============================='); System.out.println('Contents of post request'); System.out.println('============================='); while ((line = reader.readLine()) != null) { //line = new String(line.getBytes(), 'utf-8'); System.out.println(line); } System.out.println('============================='); System.out.println('Contents of post request ends'); System.out.println('============================='); reader.close(); connection.disconnect(); }}

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(對正文進行修改)都是沒有意義的了,執行這些操作會導致異常的發生。

以上就是Java發送http請求的示例(get與post方法請求)的詳細內容,更多關于Java發送http請求的資料請關注好吧啦網其它相關文章!

標簽: Java
相關文章:
主站蜘蛛池模板: 久久久久久久网站 | 黄 色 三 级 网站 | 国产成人a一在线观看 | 日韩乱淫| 国产一级毛片卡 | 欧美影院网站视频观看 | 97精品福利视频在线 | 俄罗斯极品美女毛片免费播放 | 国产粉嫩高中生无套第一次 | 亚洲日本中文字幕在线 | 在线视频99 | 96精品免费视频大全 | 日本韩国台湾香港三级 | 日本在线不卡免 | 窝窝午夜看片七次郎青草视频 | 成人91| 免费女人18毛片a级毛片视频 | 99久久免费看精品国产一区 | 成年免费大片黄在线观看一 | 国产美女精品视频 | 成年视频国产免费观看 | 99精品久久99久久久久久 | 精品一区二区在线欧美日韩 | 亚洲欧美一区二区视频 | 国产91九色刺激露脸对白 | 国产一区二区在线观看视频 | 国产成人啪精品午夜在线观看 | 美女视频在线观看黄 | 91成年人免费视频 | 国产短裙黑色丝袜在线观看下 | 一级成人a做片免费 | 亚洲 午夜在线一区 | 日韩视频在线观看 | 亚洲视频一区在线 | 日本韩国一级 | 久久夜色精品国产噜噜亚洲a | 仑乱高清在线一级播放 | 日韩欧美国产高清在线观看 | 乱子伦xxxx | 热久久伊人 | 国产偷国产偷亚洲高清午夜 |