HttpClient使用教程

1.使用HttpClient發送get請求(不帶參數)java

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.HttpResponse;
import org.apache.http.HttpEntity;

// 建立一個HttpClient實例
CloseableHttpClient httpClient = null;
try{
    httpClient = HttpClients.createDefault();
    // 建立一個HttpGet實例
    HttpGet httpGet = new HttpGet("http://127.0.0.1:8080/test/demo");
    // 發送GET請求
    HttpResponse execute = httpClient.execute(httpGet);
    //獲取狀態碼
    int statusCode = execute.getStatusLine().getStatusCode();
    System.out.println(statusCode);
    HttpEntity entity = execute.getEntity();
    String resutlt = EntityUtils.toString(entity, "utf-8");
    System.out.println(resutlt);
}catch (Exception e) {
	e.printStackTrace();
}finally{
   if (httpClient != null) {
	try {
		httpClient.close();
	} catch (IOException e) {
		e.printStackTrace();
	}
   }
}

2.使用HttpClient發送get請求(帶參數)apache

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.NameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.Consts;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.HttpResponse;
import org.apache.http.HttpEntity;
import java.util.ArrayList;
import java.util.List;

// 建立一個HttpClient實例
CloseableHttpClient httpClient = null;
try{
    httpClient  = HttpClients.createDefault();
    // 裝填參數
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("id", "1"));  //字符串參數
    // 轉換爲鍵值對
    String str = EntityUtils.toString(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
    // 建立一個HttpGet實例
    HttpGet httpGet = new HttpGet("http://127.0.0.1:8080/test/demo?"+str);
    // 發送GET請求
    HttpResponse execute = httpClient.execute(httpGet);
    //獲取狀態碼
    int statusCode = execute.getStatusLine().getStatusCode();
    System.out.println(statusCode);
    HttpEntity entity = execute.getEntity();
    String resutlt = EntityUtils.toString(entity, "utf-8");
    System.out.println(resutlt);
}catch (Exception e) {
	e.printStackTrace();
} finally {
	if (httpClient != null) {
		try {
			httpClient.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

3.使用HttpClient發送post請求app

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.NameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.Consts;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpEntity;
import java.util.ArrayList;
import java.util.List;


// 建立HttpClient實例
CloseableHttpClient httpClient = null;
// 建立HttpPost實例
HttpPost httpPost = new HttpPost("http://127.0.0.1/test/demo");
// 裝填參數
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", "1")); //字符串參數 能夠循環添加多個
try {
		httpClient = HttpClients.createDefault();
                // 設置請求參數
		httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
		// 設置header信息
		httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		httpPost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:56.0) Gecko/20100101 Firefox/56.0");
		// 執行請求
		HttpResponse httpResponse = httpClient.execute(httpPost);
		HttpEntity resEntity = httpResponse.getEntity();
		// 按指定編碼轉換結果實體爲String類型
		String result = EntityUtils.toString(resEntity, "utf-8");
		System.out.println(result);
} catch (Exception e) {
		e.printStackTrace();
} finally {
		if (httpClient != null) {
			try {
				httpClient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
}

參考:http://blog.csdn.net/xiaoxian8023/article/details/49863967maven

maven 下載包post

<dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
 </dependency>編碼

轉載請註明來源:https://my.oschina.net/u/3183495/blog/1574130url

相關文章
相關標籤/搜索