【HttpClient】httpclient之post 方法(引數為Map型別)
1.概述
httpclient之post 方法(引數為Map型別)
package httpclient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
/**
* Created by jiangcui on 2018/5/21.
*/
public class HttpClientPostForMap {
public static void main(String[] args) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("username", "jiangcui");
map.put("password", "chenzx0918");
String url = "https://passport.cnblogs.com/user/signin?ReturnUrl=https%3A%2F%2Fwww.cnblogs.com%2F";
String result = HttpClientPostForMap.doPost(url, map);
System.out.println(result);
}
// 靜態方法,類名可直接呼叫
public static String doPost(String url, Map<String, Object> paramsMap) {
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
//配置連線超時時間
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.setSocketTimeout(5000)
.setRedirectsEnabled(true)
.build();
HttpPost httpPost = new HttpPost(url);
//設定超時時間
httpPost.setConfig(requestConfig);
//裝配post請求引數
List<NameValuePair> list = new ArrayList<NameValuePair>();
for (String key : paramsMap.keySet()) {
list.add(new BasicNameValuePair(key, String.valueOf(paramsMap.get(key))));
}
try {
//將引數進行編碼為合適的格式,如將鍵值對編碼為param1=value1¶m2=value2
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list, "utf-8");
httpPost.setEntity(urlEncodedFormEntity);
//執行 post請求
CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(httpPost);
String strRequest = "";
if (null != closeableHttpResponse && !"".equals(closeableHttpResponse)) {
System.out.println(closeableHttpResponse.getStatusLine().getStatusCode());
if (closeableHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity httpEntity = closeableHttpResponse.getEntity();
strRequest = EntityUtils.toString(httpEntity);
} else {
strRequest = "Error Response" + closeableHttpResponse.getStatusLine().getStatusCode();
}
}
return strRequest;
} catch (ClientProtocolException e) {
e.printStackTrace();
return "協議異常";
} catch (ParseException e) {
e.printStackTrace();
return "解析異常";
} catch (IOException e) {
e.printStackTrace();
return "傳輸異常";
} finally {
try {
if (closeableHttpClient != null) {
closeableHttpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
相關文章
- 聊聊jdk httpclient的retry引數JDKHTTPclient
- Apache之HttpClientApacheHTTPclient
- HttpclientHTTPclient
- Java用HttpClient3傳送http/https協議get/post請求,傳送map,jsoJavaHTTPclient協議JS
- 為HttpClient開啟HTTP/2HTTPclient
- httpclient 4.5.3HTTPclient
- Apache httpclient的execute方法除錯ApacheHTTPclient除錯
- Java用HttpClient3傳送http/https協議get/post請求,傳送map,json,xml,txt資料JavaHTTPclient協議JSONXML
- 工具篇:apache-httpClient 和 jdk11-HttpClient的使用ApacheHTTPclientJDK
- OKHttp原始碼學習--HttpURLConnection HttpClient OKHttp Get and post Demo用法對比HTTP原始碼client
- Go - httpclient 常用操作GoHTTPclient
- IntelliJ IDEA 2020.1新增HttpClient類庫的方法IntelliJIdeaHTTPclient
- 把HttpClient換成IHttpClientFactory之後,放心多了HTTPclient
- HttpClient引發的執行緒數過多導致應用崩潰HTTPclient執行緒應用崩潰
- java apache commons HttpClient傳送get和post請求的學習整理JavaApacheHTTPclient
- 聊聊jdk httpclient的executorJDKHTTPclient
- HttpClient 下載檔案HTTPclient
- Httpclient 介面自動化HTTPclient
- HttpClient請求工具類HTTPclient
- .Netcore HttpClient原始碼探究NetCoreHTTPclient原始碼
- HttpClient 進行soap請求HTTPclient
- .NET Core HttpClient原始碼探究HTTPclient原始碼
- RestTemplate和 apache HttpClient 使用方式RESTApacheHTTPclient
- 解析型別引數型別
- HttpClient多檔案上傳程式碼及普通引數中文亂碼問題解決HTTPclient
- Winform中使用HttpClient實現呼叫http的post介面並設定傳參content-type為application/json示例ORMHTTPclientAPPJSON
- [Jmeter] Implementation 中 JAVA 與 HTTPClient4 如何探尋兩者之間的區別JMeterJavaHTTPclient
- Java11 HttpClient小試牛刀JavaHTTPclient
- HttpClient4.5中文教程HTTPclient
- 小心 HttpClient 中的 FormUrlEncodeContent 的 bugHTTPclientORM
- 優雅通過HttpClientFactory使用HttpClientHTTPclient
- java httpclient傳送中文亂碼JavaHTTPclient
- C# httpclient上傳檔案C#HTTPclient
- 【傳輸協議】HttpClient基本使用協議HTTPclient
- Apache HttpClient使用和原始碼分析ApacheHTTPclient原始碼
- 【Scala之旅】型別引數型別
- Python 序列型別以及函式引數型別Python型別函式
- std::packaged_task<返回型別(引數型別)>Package型別