java模擬http的Get/Post請求,並設置ip與port代理


本文涉及3個基本點:

1、因為很多公司的內網都設有代理,瀏覽器通過ip與port上網,而java代碼模擬http get方式同樣需要外網代理;

2、Java實現http的Get/Post請求代碼;

3、主要是設置HttpURLConnection請求頭里面的屬性
比如Cookie、User-Agent(瀏覽器類型)等等。

比如:http請求中添加Header

conn.setRequestProperty("Authorization", authorization);

:我就在網上找的一段Get/Post模擬請求代碼,添加了下代理的配置,整合完成的。

  1 package com.pasier.quanzi.web.controller;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.IOException;
  5 import java.io.InputStreamReader;
  6 import java.io.PrintWriter;
  7 import java.net.URL;
  8 import java.net.URLConnection;
  9 import java.util.List;
 10 import java.util.Map;
 11 
 12 public class HttpRequest {
 13 
 14     public static void main(String[] args) {
 15         // 如果不設置,只要代理IP和代理端口正確,此項不設置也可以
 16         System.getProperties().setProperty("http.proxyHost", "10.22.40.32");
 17         System.getProperties().setProperty("http.proxyPort", "8080");
 18         // 判斷代理是否設置成功
 19         // 發送 GET 請求
 20         System.out.println(sendGet(
 21                 "http://www.baidu.com",
 22                 "param1=xxx&param2=yyy"));
 23         // 發送 POST 請求
 24     }
 25 
 26     /**
 27      * 向指定URL發送GET方法的請求
 28      * 
 29      * @param url
 30      *            發送請求的URL
 31      * @param param
 32      *            請求參數,請求參數應該是 name1=value1&name2=value2 的形式。
 33      * @return URL 所代表遠程資源的響應結果
 34      */
 35     public static String sendGet(String url, String param) {
 36         String result = "";
 37         BufferedReader in = null;
 38         try {
 39             String urlNameString = url + "?" + param;
 40             URL realUrl = new URL(urlNameString);
 41             // 打開和URL之間的連接
 42             URLConnection connection = realUrl.openConnection();
 43             // 設置通用的請求屬性
 44             connection.setRequestProperty("accept", "*/*");
 45             connection.setRequestProperty("connection", "Keep-Alive");
 46             connection.setRequestProperty("user-agent",
 47                     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
 48             // 建立實際的連接
 49             connection.connect();
 50             // 獲取所有響應頭字段
 51             Map<String, List<String>> map = connection.getHeaderFields();
 52             // 遍歷所有的響應頭字段
 53             for (String key : map.keySet()) {
 54                 System.out.println(key + "--->" + map.get(key));
 55             }
 56             // 定義 BufferedReader輸入流來讀取URL的響應
 57             in = new BufferedReader(new InputStreamReader(
 58                     connection.getInputStream()));
 59             String line;
 60             while ((line = in.readLine()) != null) {
 61                 result += line;
 62             }
 63         } catch (Exception e) {
 64             System.out.println("發送GET請求出現異常!" + e);
 65             e.printStackTrace();
 66         }
 67         // 使用finally塊來關閉輸入流
 68         finally {
 69             try {
 70                 if (in != null) {
 71                     in.close();
 72                 }
 73             } catch (Exception e2) {
 74                 e2.printStackTrace();
 75             }
 76         }
 77         return result;
 78     }
 79 
 80     /**
 81      * 向指定 URL 發送POST方法的請求
 82      * 
 83      * @param url
 84      *            發送請求的 URL
 85      * @param param
 86      *            請求參數,請求參數應該是 name1=value1&name2=value2 的形式。
 87      * @return 所代表遠程資源的響應結果
 88      */
 89     public static String sendPost(String url, String param) {
 90         PrintWriter out = null;
 91         BufferedReader in = null;
 92         String result = "";
 93         try {
 94             URL realUrl = new URL(url);
 95             // 打開和URL之間的連接
 96             URLConnection conn = realUrl.openConnection();
 97             // 設置通用的請求屬性
 98             conn.setRequestProperty("accept", "*/*");
 99             conn.setRequestProperty("connection", "Keep-Alive");
100             conn.setRequestProperty("user-agent",
101                     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
102             // 發送POST請求必須設置如下兩行
103             conn.setDoOutput(true);
104             conn.setDoInput(true);
105             // 獲取URLConnection對象對應的輸出流
106             out = new PrintWriter(conn.getOutputStream());
107             // 發送請求參數
108             out.print(param);
109             // flush輸出流的緩沖
110             out.flush();
111             // 定義BufferedReader輸入流來讀取URL的響應
112             in = new BufferedReader(
113                     new InputStreamReader(conn.getInputStream()));
114             String line;
115             while ((line = in.readLine()) != null) {
116                 result += line;
117             }
118         } catch (Exception e) {
119             System.out.println("發送 POST 請求出現異常!" + e);
120             e.printStackTrace();
121         }
122         // 使用finally塊來關閉輸出流、輸入流
123         finally {
124             try {
125                 if (out != null) {
126                     out.close();
127                 }
128                 if (in != null) {
129                     in.close();
130                 }
131             } catch (IOException ex) {
132                 ex.printStackTrace();
133             }
134         }
135         return result;
136     }
137 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM