實用性工具類——Post請求
通過輸入鍵值對和請求URL,即可返回對應的json資料
- 工具類
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
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;
public class HttpUtil {
private static final CloseableHttpClient httpclient = HttpClients.createDefault();
/**
* 傳送HttpGet請求
*
* @param url
* @return
*/
public static String sendGet(String url) {
HttpGet httpget = new HttpGet(url);
CloseableHttpResponse response = null;
try {
response = httpclient.execute(httpget);
} catch (IOException e1) {
e1.printStackTrace();
}
String result = null;
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity);
}
} catch (ParseException | IOException e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* 傳送HttpPost請求,引數為map
*
* @param url
* @param map
* @return
*/
public static String sendPost(String url, Map<String, String> map) {
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : map.entrySet()) {
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
HttpPost httppost = new HttpPost(url);
httppost.setEntity(entity);
CloseableHttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (IOException e) {
e.printStackTrace();
}
HttpEntity entity1 = response.getEntity();
String result = null;
try {
result = EntityUtils.toString(entity1);
} catch (ParseException | IOException e) {
e.printStackTrace();
}
return result;
}
/**
* 傳送不帶引數的HttpPost請求
*
* @param url
* @return
*/
public static String sendPost(String url) {
HttpPost httppost = new HttpPost(url);
CloseableHttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (IOException e) {
e.printStackTrace();
}
HttpEntity entity = response.getEntity();
String result = null;
try {
result = EntityUtils.toString(entity);
} catch (ParseException | IOException e) {
e.printStackTrace();
}
return result;
}
}
相關文章
- post 請求工具類
- Java Http Get Post 請求工具類JavaHTTP
- HttpClient請求工具類HTTPclient
- linux用curl傳送post請求Linux
- java post 請求Java
- SpringMVC中如何傳送GET請求、POST請求、PUT請求、DELETE請求。SpringMVCdelete
- Postman傳送Post請求Postman
- Java 監聽POST請求Java
- requests 模組 - post 請求
- Java傳送Post請求Java
- python3 實現 get 和 post 請求Python
- vue 發起get請求和post請求Vue
- get請求和post請求的區別
- cURL實現傳送Get和Post請求(PHP)PHP
- 原生js實現Ajax請求,包含get和postJS
- POST發起下載請求
- python傳送HTTP POST請求PythonHTTP
- post請求帶來的option
- axios的post請求爬坑iOS
- SpringCloud OpenFeign Post請求的坑SpringGCCloud
- uniapp之post請求如何用APP
- POST與GET請求區別
- ajax中POST請求與引數(請求體)設定
- 如何使POST請求具有冪等性防止重複提交 - mscharhag
- uni-app的POST請求和GET請求APP
- vue2.0 axios post請求傳參問題(ajax請求)VueiOS
- axios 發get,post 請求小結iOS
- get與post的請求區別
- python介面測試—post請求(二)Python
- go對get、post請求封裝Go封裝
- java傳送GET和post請求Java
- get,post URL加字尾請求
- PHP與Curl採用的GET,POST,JSON方式請求APIPHPJSONAPI
- Python中get、post請求詳解(HTTP請求頭、狀態碼)PythonHTTP
- C#模擬HTTP請求Post JSONC#HTTPJSON
- 如何使用post請求下載檔案
- Linux curl 命令模擬 POST/GET 請求Linux
- go的gin框架使用(五):post請求Go框架
- httprequest- post- get -傳送請求HTTP