maven
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.1</version>
<scope>compile</scope>
</dependency>
請求頭 content-type 類型 https://www.runoob.com/http/http-content-type.html
multipart/form-data : 需要在表單中進行文件上傳時,就需要使用該格式
請求頭 Accept代表發送端(客戶端)希望接受的數據類型。
使用
public static String post(String url, String jsonstr, Map<String, String> heads) throws IOException {
String result = "";
CloseableHttpResponse httpResponse = null;
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
try {
// 獲得Http客戶端(可以理解爲:你得先有一個瀏覽器;注意:實際上HttpClient與瀏覽器是不一樣的)
httpClient = HttpClientBuilder.create().build();
String charset = "utf-8";
// 定義請求的參數
//URI uri = new URIBuilder("http://www.baidu.com/s").setParameter("wd", "java").build();
//httpGet = new HttpGet(uri);
// 創建Post請求
httpPost = new HttpPost(url);
//解析對象
StringEntity entity = new StringEntity(jsonstr, charset);
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
if (heads != null) {
for (String headKey : heads.keySet()) {
httpPost.setHeader(headKey, heads.get(headKey));
}
}
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(1000) //連接超時時間
.setConnectionRequestTimeout(1000) //從連接池中取的連接的最長時間
.setSocketTimeout(10 *1000) //數據傳輸的超時時間
.build();
httpResponse = httpClient.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(httpResponse.getEntity(), charset);
}
result = httpResponse.toString();
}finally {
if(httpResponse != null){
try {
httpResponse.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
if(httpPost != null){
httpPost.clone();
}
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
if(httpClient != null){
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
配置API RequestConfig()
protected RequestConfig |
clone() |
static RequestConfig.Builder |
copy(RequestConfig config) |
static RequestConfig.Builder |
custom() |
int |
getConnectionRequestTimeout() 返回從連接管理器請求連接時使用的超時(以毫秒爲單位)。 |
int |
getConnectTimeout() 確定連接建立之前的超時時間(以毫秒爲單位)。 |
String |
getCookieSpec() 確定用於HTTP狀態管理的cookie規範的名稱。 |
InetAddress |
getLocalAddress() 返回用於執行請求的本地地址。 |
int |
getMaxRedirects() 返回要遵循的最大重定向數。 |
org.apache.http.HttpHost |
getProxy() 返回用於請求執行的HTTP代理。 |
Collection<String> |
getProxyPreferredAuthSchemes() 確定使用代理主機進行身份驗證時支持的身份驗證方案的優先順序。 |
int |
getSocketTimeout() 定義套接字超時( SO_TIMEOUT ),以毫秒爲單位,這是等待數據的超時,或者換句話說,是兩個連續數據包之間的最大時間段不活動。 |
Collection<String> |
getTargetPreferredAuthSchemes() 確定使用目標主機進行身份驗證時支持的身份驗證方案的優先順序。 |
boolean |
isAuthenticationEnabled() 確定是否應自動處理身份驗證。 |
boolean |
isCircularRedirectsAllowed() 確定是否應允許循環重定向(重定向到同一位置)。 |
boolean |
isContentCompressionEnabled() 確定是否請求目標服務器壓縮內容。 |
boolean |
isDecompressionEnabled() 不推薦使用。 (4.5)使用 isContentCompressionEnabled() |
boolean |
isExpectContinueEnabled() 確定是否爲實體封裝方法啓用了“期望:100-連續”握手。 |
boolean |
isNormalizeUri() 確定客戶端是否應規範請求中的URI。 |
boolean |
isRedirectsEnabled() 確定是否應自動處理重定向。 |
boolean |
isRelativeRedirectsAllowed() 確定是否應拒絕相對重定向。 |
boolean |
isStaleConnectionCheckEnabled() 不推薦使用。 (4.4)使用 PoolingHttpClientConnectionManager.getValidateAfterInactivity() |
String |
toString() |