
Apache HttpClient - Http Post 請求
POST 請求用於向伺服器傳送資料;例如,使用 HTML 表單傳送客戶資訊、上傳檔案等。
HttpClient API 提供了一個名為 HttpPost 的類,該類表示 POST 請求。
按照以下步驟使用 HttpClient 庫傳送一個 HTTP POST 請求。
步驟 1 - 建立一個 HttpClient 物件
HttpClients 類的 createDefault() 方法返回 CloseableHttpClient 類的一個物件,該類是 HttpClient 類的基本實現。
使用此方法建立 HttpClient 物件。
CloseableHttpClient httpClient = HttpClients.createDefault();
步驟 2 - 建立 HttpPost 物件
HttpPost 類表示 HTTP POST 請求。此請求傳送所需資料,並使用 URI 從給定的伺服器獲取資訊。
透過例項化 HttpPost 類並向其建構函式傳遞一個表示 URI 的字串值來建立此請求。
HttpGet httpGet = new HttpGet("https://tutorialspoint.tw/");
步驟 3 - 執行 Get 請求
CloseableHttpClient 物件的 execute() 方法接受一個 HttpUriRequest(介面)物件(即 HttpGet、HttpPost、HttpPut、HttpHead 等),並返回一個響應物件。
HttpResponse httpResponse = httpclient.execute(httpget);
示例
以下是一個演示使用 HttpClient 庫執行 HTTP POST 請求的示例。
import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; public class HttpPostExample { public static void main(String args[]) throws Exception{ //Creating a HttpClient object CloseableHttpClient httpclient = HttpClients.createDefault(); //Creating a HttpGet object HttpPost httppost = new HttpPost("https://tutorialspoint.tw/"); //Printing the method used System.out.println("Request Type: "+httppost.getMethod()); //Executing the Get request HttpResponse httpresponse = httpclient.execute(httppost); Scanner sc = new Scanner(httpresponse.getEntity().getContent()); //Printing the status line System.out.println(httpresponse.getStatusLine()); while(sc.hasNext()) { System.out.println(sc.nextLine()); } } }
輸出
上面的程式將生成以下輸出。
Request Type: POST <!DOCTYPE html> <!--[if IE 8]><html class = "ie ie8"> <![endif]--> <!--[if IE 9]><html class = "ie ie9"> <![endif]--> <!--[if gt IE 9]><!--> <html lang = "en-US"> <!--<![endif]--> <head> <!-- Basic --> <meta charset = "utf-8"> <title>Parallax Scrolling, Java Cryptography, YAML, Python Data Science, Java i18n, GitLab, TestRail, VersionOne, DBUtils, Common CLI, Seaborn, Ansible, LOLCODE, Current Affairs 2018, Apache Commons Collections</title> <meta name = "Description" content = "Parallax Scrolling, Java Cryptography, YAML, Python Data Science, Java i18n, GitLab, TestRail, VersionOne, DBUtils, Common CLI, Seaborn, Ansible, LOLCODE, Current Affairs 2018, Intellij Idea, Apache Commons Collections, Java 9, GSON, TestLink, Inter Process Communication (IPC), Logo, PySpark, Google Tag Manager, Free IFSC Code, SAP Workflow"/> <meta name = "Keywords" content="Python Data Science, Java i18n, GitLab, TestRail, VersionOne, DBUtils, Common CLI, Seaborn, Ansible, LOLCODE, Gson, TestLink, Inter Process Communication (IPC), Logo"/> <meta http-equiv = "X-UA-Compatible" content = "IE = edge"> <meta name = "viewport" conten t= "width = device-width,initial-scale = 1.0,userscalable = yes"> <link href = "https://cdn.muicss.com/mui-0.9.39/extra/mui-rem.min.css" rel = "stylesheet" type = "text/css" /> <link rel = "stylesheet" href = "/questions/css/home.css?v = 3" /> <script src = "/questions/js/jquery.min.js"></script> <script src = "/questions/js/fontawesome.js"></script> <script src = "https://cdn.muicss.com/mui-0.9.39/js/mui.min.js"></script> </head> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . </script> </body> </html>
廣告