httpclient工具使用(org.apache.httpcomponents.httpclient)
引入依賴
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.5</version> </dependency>
get請求
public static void main(String[] args) throws Exception {
//創建httpclient對象
CloseableHttpClient httpClient = HttpClients.createDefault();
//創建Http get請求
HttpGet httpGet = new HttpGet("http://www.xxx.com/rest/content?categoryId=4&page=1&rows=20");
//接收返回值
CloseableHttpResponse response = null;
try {
//請求執行
response = httpClient.execute(httpGet);
if(response.getStatusLine().getStatusCode()==200){
String content = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println("--------->" + content);
}
}finally{
if(response!=null){
response.close();
}
httpClient.close();
}
get帶參數請求
public static void main(String[] args) throws Exception{
//創建httpClient對象
CloseableHttpClient httpClient = HttpClients.createDefault();
//定義http get參數
URI uri = new URIBuilder("http://www.xxxx.com/rest/content").setParameter("categoryId", "4")
.setParameter("page", "1").setParameter("rows", "30").build();
System.out.println(uri);
//創建http get請求
HttpGet httpGet = new HttpGet(uri);
//接受請求返回的定義
CloseableHttpResponse response = null;
try {
//執行get請求
response = httpClient.execute(httpGet);
if(response.getStatusLine().getStatusCode()==200){
String content = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println(content);
}
}finally{
if(response!=null){
response.close();
}
httpClient.close();
}
}
http post請求
public static void main(String[] args) throws Exception {
// 創建Httpclient對象
CloseableHttpClient httpclient = HttpClients.createDefault();
// 創建http POST請求
HttpPost httpPost = new HttpPost("http://www.oschina.net/");
// 偽裝成瀏覽器
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36");
CloseableHttpResponse response = null;
try {
// 執行請求
response = httpclient.execute(httpPost);
// 判斷返回狀態是否為200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(content);
}
} finally {
if (response != null) {
response.close();
}
httpclient.close();
}
}
http post帶參數請求
public static void main(String[] args) throws Exception{
//創建httpclient
CloseableHttpClient httpClient = HttpClients.createDefault();
//創建http post
HttpPost httpPost = new HttpPost("http://www.oschina.net/search");
//模擬瀏覽器設置頭
httpPost.setHeader(
"User-Agent",
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36");
//設置請求數據
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("scope", "project"));
params.add(new BasicNameValuePair("q", "java"));
params.add(new BasicNameValuePair("fromerr", "7nXH76r7"));
//構建表單
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params);
//將表達請求放入到httpost
httpPost.setEntity(formEntity);
//返回類型
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
String content = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println(content);
}finally{
if(response!=null){
response.close();
}
httpClient.close();
}
}
