標籤:dir tostring false invoke throws lin bst buffer 遠端連線
1 import java.io.BufferedReader; 2 import java.io.DataOutputStream; 3 import java.io.IOException; 4 import java.io.InputStreamReader; 5 import java.net.HttpURLConnection; 6 import java.net.URL; 7 import java.util.Map; 8 9 /**10 * HTTP請求類11 * @author 12 */13 public class HttpInvoker {14 15 /**16 * GET請求17 * @param getUrl18 * @throws IOException19 * @return 提取HTTP響應報文包體,以字串形式返回20 */21 public static String httpGet(String getUrl) throws IOException { 22 //1 把字串形式的網址轉成 java.net.URL對象23 URL getURL = new URL(getUrl); 24 /*25 * 2 通過java.net.URL開啟一個串連(擷取一個代表與遠程對象的URLConnection對象),並轉為 HttpURLConnection ,26 Returns a URLConnection object that represents a connection 27 to the remote object referred to by the URL.28 */29 HttpURLConnection connection = (HttpURLConnection) getURL.openConnection(); 30 //3 通過HttpURLConnection對象真正擷取串連31 connection.connect();32 //4 串連後那麼這個遠端連線對象就可以通過自身的一些方法讀取到串連的資訊了,這裡是把流轉成字串33 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));34 StringBuilder sbStr = new StringBuilder();35 String line;36 while ((line = bufferedReader.readLine()) != null) { 37 sbStr.append(line); 38 } 39 bufferedReader.close();40 connection.disconnect(); 41 return new String(sbStr.toString().getBytes(),"utf-8");42 }43 44 45 public static void main(String[] args) throws IOException {46 String url = "https://www.hao123.com/?tn=99682755_hao_pg";47 String rtn_json = httpGet(url);48 System.out.println(""+rtn_json);49 }50 51 /**52 * POST請求53 * @param postUrl54 * @param postHeaders55 * @param postEntity56 * @throws IOException57 * @return 提取HTTP響應報文包體,以字串形式返回58 */59 public static String httpPost(String postUrl,Map<String, String> postHeaders, String postEntity) throws IOException {60 61 URL postURL = new URL(postUrl); 62 HttpURLConnection httpURLConnection = (HttpURLConnection) postURL.openConnection(); 63 httpURLConnection.setDoOutput(true); 64 httpURLConnection.setDoInput(true); 65 httpURLConnection.setRequestMethod("POST"); 66 httpURLConnection.setUseCaches(false); 67 httpURLConnection.setInstanceFollowRedirects(true); 68 httpURLConnection.setRequestProperty(" Content-Type ", " application/x-www-form-urlencoded ");69 70 if(postHeaders != null) {71 for(String pKey : postHeaders.keySet()) {72 httpURLConnection.setRequestProperty(pKey, postHeaders.get(pKey));73 }74 }75 if(postEntity != null) {76 DataOutputStream out = new DataOutputStream(httpURLConnection.getOutputStream()); 77 out.writeBytes(postEntity); 78 out.flush(); 79 out.close(); // flush and close 80 }81 //connection.connect(); 82 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); 83 StringBuilder sbStr = new StringBuilder();84 String line;85 while ((line = bufferedReader.readLine()) != null) { 86 sbStr.append(line); 87 } 88 bufferedReader.close();89 httpURLConnection.disconnect(); 90 return new String(sbStr.toString().getBytes(),"utf-8");91 } 92 93 }
HttpInvoker http請求工具類