標籤:
package com.service; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; public class HttpInvoker { public static final String GET_URL = " http://localhost:8080/demo/ "; public static final String POST_URL = " http://localhost:8080/demo/ "; 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(); // 建立與伺服器的串連,並未發送資料 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(); //開啟讀寫屬性,預設均為false connection.setDoOutput(true); connection.setDoInput(true); // 佈建要求方式,預設為GET connection.setRequestMethod(" POST "); // Post 請求不能使用緩衝 connection.setUseCaches(false); // URLConnection.setFollowRedirects是static 函數,作用於所有的URLConnection對象。 // connection.setFollowRedirects(true); //URLConnection.setInstanceFollowRedirects 是成員函數,僅作用於當前函數 connection.setInstanceFollowRedirects(true); // 配置串連的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()); //本文內容其實跟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(); } public static void main(String[] args) { // TODO Auto-generated method stub try { readContentFromGet(); readContentFromPost(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
java 發送http請求