實用性工具類——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;
}
}
相關文章
- Java Http Get Post 請求工具類JavaHTTP
- 關於用URL類傳送POST請求的問題?
- HttpClient請求工具類HTTPclient
- 【轉】怎麼用PHP傳送HTTP請求(POST請求、GET請求)?PHPHTTP
- 用Fiddler 傳送post請求
- java post 請求Java
- iOS 同步請求 非同步請求 GET請求 POST請求iOS非同步
- linux用curl傳送post請求Linux
- jQueryAjax:$.post請求示例jQuery
- ajax的post請求,用於asp.net ?ASP.NET
- SpringMVC中如何傳送GET請求、POST請求、PUT請求、DELETE請求。SpringMVCdelete
- Postman傳送Post請求Postman
- Java傳送Post請求Java
- ajax跨域post請求,如何實現呢跨域
- Android Http請求框架一:Get 和 Post 請求AndroidHTTP框架
- Android okHttp網路請求之Get/Post請求AndroidHTTP
- get請求和post請求的區別
- python3 實現 get 和 post 請求Python
- cURL實現傳送Get和Post請求(PHP)PHP
- nodejs實現HTTPS發起POST請求NodeJSHTTP
- 原生js實現Ajax請求,包含get和postJS
- PHP中使用cURL實現Get和Post請求PHP
- POST與GET請求區別
- POST發起下載請求
- post請求帶來的option
- POST請求登入網頁網頁
- HTTP Get,Post請求詳解HTTP
- C# 傳送POST請求C#
- Get和Post請求詳解
- ajax中POST請求與引數(請求體)設定
- java傳送post請求 ,請求資料放到body裡Java
- 如何使POST請求具有冪等性防止重複提交 - mscharhag
- get請求和post請求區別詳解
- uni-app的POST請求和GET請求APP
- java傳送GET和post請求Java
- axios的post請求爬坑iOS
- get與post的請求區別
- go對get、post請求封裝Go封裝