1.get請求
@Test
public void sendGet() {
// 獲取連接客戶端工具
CloseableHttpClient httpClient = HttpClients.createDefault();
String entityStr = null;
CloseableHttpResponse response = null;
try {
/*
* 由於GET請求的參數都是拼裝在URL地址後方,所以我們要構建一個URL,帶參數
*/
URIBuilder uriBuilder = new URIBuilder("http://www.baidu.com");
/** 第一種添加參數的形式 */
/*uriBuilder.addParameter("name", "root");
uriBuilder.addParameter("password", "123456");*/
/** 第二種添加參數的形式 */
List<NameValuePair> list = new LinkedList<>();
BasicNameValuePair param1 = new BasicNameValuePair("name", "root");
BasicNameValuePair param2 = new BasicNameValuePair("password", "123456");
list.add(param1);
list.add(param2);
uriBuilder.setParameters(list);
// 根據帶參數的URI對象構建GET請求對象
HttpGet httpGet = new HttpGet(uriBuilder.build());
/*
* 添加請求頭信息
*/
// 瀏覽器表示
httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)");
// 傳輸的類型
httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded");
// 執行請求
response = httpClient.execute(httpGet);
// 獲得響應的實體對象
HttpEntity entity = response.getEntity();
// 使用Apache提供的工具類進行轉換成字符串
entityStr = EntityUtils.toString(entity, "UTF-8");
} catch (ClientProtocolException e) {
System.err.println("Http協議出現問題");
e.printStackTrace();
} catch (ParseException e) {
System.err.println("解析錯誤");
e.printStackTrace();
} catch (URISyntaxException e) {
System.err.println("URI解析異常");
e.printStackTrace();
} catch (IOException e) {
System.err.println("IO異常");
e.printStackTrace();
} finally {
// 釋放連接
if (null != response) {
try {
response.close();
httpClient.close();
} catch (IOException e) {
System.err.println("釋放連接出錯");
e.printStackTrace();
}
}
}
// 打印響應內容
System.out.println(entityStr);
}
2.POST請求
@Test
public void sendPost() {
// 獲取連接客戶端工具
CloseableHttpClient httpClient = HttpClients.createDefault();
String entityStr = null;
CloseableHttpResponse response = null;
try {
// 創建POST請求對象
HttpPost httpPost = new HttpPost("http://www.baidu.com");
/*
* 添加請求參數
*/
// 創建請求參數
List<NameValuePair> list = new LinkedList<>();
BasicNameValuePair param1 = new BasicNameValuePair("name", "root");
BasicNameValuePair param2 = new BasicNameValuePair("password", "123456");
list.add(param1);
list.add(param2);
// 使用URL實體轉換工具
UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(list, "UTF-8");
httpPost.setEntity(entityParam);
/*
* 添加請求頭信息
*/
// 瀏覽器表示
httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)");
// 傳輸的類型
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
// 執行請求
response = httpClient.execute(httpPost);
// 獲得響應的實體對象
HttpEntity entity = response.getEntity();
// 使用Apache提供的工具類進行轉換成字符串
entityStr = EntityUtils.toString(entity, "UTF-8");
// System.out.println(Arrays.toString(response.getAllHeaders()));
} catch (ClientProtocolException e) {
System.err.println("Http協議出現問題");
e.printStackTrace();
} catch (ParseException e) {
System.err.println("解析錯誤");
e.printStackTrace();
} catch (IOException e) {
System.err.println("IO異常");
e.printStackTrace();
} finally {
// 釋放連接
if (null != response) {
try {
response.close();
httpClient.close();
} catch (IOException e) {
System.err.println("釋放連接出錯");
e.printStackTrace();
}
}
}
// 打印響應內容
System.out.println(entityStr);
}