java發送http get請求,有兩種方式。
第一種用URLConnection:
public static String get(String url) throws IOException { BufferedReader in = null; URL realUrl = new URL(url); // 打開和URL之間的連接 URLConnection connection = realUrl.openConnection(); // 設置通用的請求屬性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); // 建立實際的連接 connection.connect(); // 定義 BufferedReader輸入流來讀取URL的響應 in = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuffer sb = new StringBuffer(); String line; while ((line = in.readLine()) != null) { sb.append(line); } in.close(); return sb.toString(); }
第二種用java HTTP客戶端:HttpGet、HttpClient、HttpResponse等
public static String httpGet(String url, String charset) throws HttpException, IOException { String json = null; HttpGet httpGet = new HttpGet(); // 設置參數 try { httpGet.setURI(new URI(url)); } catch (URISyntaxException e) { throw new HttpException("請求url格式錯誤。"+e.getMessage()); } // 發送請求 HttpClient client=HttpClients.createDefault(); HttpResponse httpResponse = client.execute(httpGet); // 獲取返回的數據 HttpEntity entity = httpResponse.getEntity(); byte[] body = EntityUtils.toByteArray(entity); StatusLine sL = httpResponse.getStatusLine(); int statusCode = sL.getStatusCode(); if (statusCode == 200) { json = new String(body, charset); entity.consumeContent(); } else { throw new HttpException("statusCode="+statusCode); } return json; }
POST請求同理,這里只列出URLConnection方式:
public static String post(String url, String param, Map<String, String> header) throws IOException { PrintWriter out = null; BufferedReader in = null; String result = ""; URL realUrl = new URL(url); // 打開和URL之間的連接 URLConnection conn = realUrl.openConnection(); //設置超時時間 conn.setConnectTimeout(5000); conn.setReadTimeout(15000); // 設置通用的請求屬性 if (header != null) { for (Map.Entry<String, String> entry : header.entrySet()) { conn.setRequestProperty(entry.getKey(), entry.getValue()); } } conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 發送POST請求必須設置如下兩行 conn.setDoOutput(true); conn.setDoInput(true); // 獲取URLConnection對象對應的輸出流 out = new PrintWriter(conn.getOutputStream()); // 發送請求參數 out.print(param); // flush輸出流的緩沖 out.flush(); // 定義BufferedReader輸入流來讀取URL的響應 in = new BufferedReader( new InputStreamReader(conn.getInputStream(), "utf8")); String line; while ((line = in.readLine()) != null) { result += line; } if (out != null) { out.close(); } if (in != null) { in.close(); } return result; }
代理
如果使用代理的話可以按如下編寫一個設置代理的函數configProxy,然后在發送請求前調用一下這個函數就行了:
package com.tpot.DataDownload; import java.util.Properties; public class Configer { public static void configProxy(){ Properties prop=System.getProperties(); prop.setProperty("proxySet","true"); prop.setProperty("http.proxyHost","proxy.xxxx.com"); prop.setProperty("http.proxyPort","8080"); prop.setProperty("http.proxyUser","xxxxx"); prop.setProperty("http.proxyPassword","xxxxx"); prop.setProperty("https.proxyHost","proxy.xxxxx.com"); prop.setProperty("https.proxyPort","8080"); prop.setProperty("https.proxyUser","xxxxx"); prop.setProperty("https.proxyPassword","xxxxx"); } }
