色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

Java httpcomponents發(fā)送get post請求代碼實(shí)例

瀏覽:3日期:2022-08-24 13:44:52

引入的包為:

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --><dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.8</version></dependency>

實(shí)現(xiàn)的工具類為:

import com.alibaba.fastjson.JSON;import org.apache.http.NameValuePair;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.slf4j.Logger;import org.slf4j.LoggerFactory;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class HttpClientHelper { private static Logger logger = LoggerFactory.getLogger(HttpClientHelper.class); private HttpClientHelper() { } /** * 發(fā)起POST請求 * * @param url url * @param paramMap 參數(shù)的Map格式 */ public static void sendPost(String url, Map<String, String> paramMap) { logger.info('開始發(fā)起POST請求,請求地址為{},參數(shù)為{}', url, JSON.toJSON(paramMap)); CloseableHttpResponse response = null; try (CloseableHttpClient httpClient = HttpClients.createDefault()) { String encoding = 'utf-8'; //創(chuàng)建post請求對象 HttpPost httpPost = new HttpPost(url); //裝填請求參數(shù) List<NameValuePair> list = new ArrayList<>(); for (Map.Entry<String, String> entry : paramMap.entrySet()) {list.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } //設(shè)置參數(shù)到請求對象中 httpPost.setEntity(new UrlEncodedFormEntity(list, encoding)); httpPost.setHeader('Content-type', 'application/x-www-form-urlencoded'); httpPost.setHeader('User-Agent', 'Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)'); response = httpClient.execute(httpPost); } catch (IOException e) { logger.error('POST請求發(fā)出失敗,請求的地址為{},參數(shù)為{},錯(cuò)誤信息為{}', url, JSON.toJSON(paramMap), e.getMessage(), e); } finally { try {if (response != null) { response.close();} } catch (IOException e) {logger.error('POST請求response關(guān)閉異常,錯(cuò)誤信息為{}', e.getMessage(), e); } } } /** * 發(fā)起GET請求 * * @param urlParam url請求,包含參數(shù) */ public static void sendGet(String urlParam) { logger.info('開始發(fā)起GET請求,請求地址為{}', urlParam); HttpGet httpGet = new HttpGet(urlParam); CloseableHttpResponse response = null; try (CloseableHttpClient httpClient = HttpClients.createDefault()) { response = httpClient.execute(httpGet); int status = response.getStatusLine().getStatusCode(); logger.error('GET請求發(fā)出成功,請求的地址為{},返回狀態(tài)為{}', urlParam, status); } catch (IOException e) { logger.error('GET請求發(fā)出失敗,請求的地址為{},錯(cuò)誤信息為{}', urlParam, e.getMessage(), e); } finally { try {if (response != null) { response.close();} } catch (IOException e) {logger.error('GET請求response關(guān)閉異常,錯(cuò)誤信息為{}', e.getMessage(), e); } } } public static void main(String[] args) { String url = 'https://jiashubing.cn/tencenttest'; //需要傳入的參數(shù) Map<String, String> map = new HashMap<>(); map.put('code', 'js'); map.put('day', '0'); map.put('city', '北京'); map.put('dfc', '1'); map.put('charset', 'utf-8'); sendPost(url, map); String urlParam = 'https://jiashubing.cn/talk/document?fileid=1234ji賈樹丙'; sendGet(urlParam); }}

如果POST請求想要發(fā)送Json 格式的數(shù)據(jù),只需要修改成這樣:

String json = JSON.toJSONString(paramMap);StringEntity requestEntity = new StringEntity(json, 'utf-8');httpPost.setEntity(requestEntity);

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 国产成人小视频在线观看 | 国产手机在线小视频免费观看 | 久久久久久国产精品免费 | 韩国一级免费视频 | 性欧美成人依依影院 | 国产啪精品视频网免费 | 手机日韩理论片在线播放 | 日韩精品一区二区三区免费观看 | 国产91专区 | 丝袜精品 欧美 亚洲 自拍 | 亚洲精品美女国产一区 | 国产一区二区三区欧美精品 | 国产精品午夜国产小视频 | 色在线免费视频 | 美国一级毛片oo | 97se狠狠狠狠狠亚洲综合网 | 亚洲欧美视频在线 | 国内成人自拍 | 久久伊人成人网 | 午夜精品影院 | 美女被强行扒开双腿激情视频 | 国产aaa女人十八毛片 | 91精品国产福利尤物免费 | 日韩在线播放中文字幕 | 91香蕉国产亚洲一区二区三区 | 精品国产看高清国产毛片 | 久久久久久久久免费视频 | 亚洲成a人片在线看 | 撸天堂 | 久草视 | 国产成人免费在线观看 | 国产伦精品一区三区视频 | 日本巨乳中文字幕 | 久草天堂| 在线视频一区二区三区四区 | 国产精品正在播放 | 姐姐真漂亮在线视频中文版 | 亚洲视频在线播放 | a毛片a毛片a视频 | 91精品国产薄丝高跟在线看 | 毛片视频网站在线观看 |