国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Java httpcomponents發送get post請求代碼實例

瀏覽: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>

實現的工具類為:

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() { } /** * 發起POST請求 * * @param url url * @param paramMap 參數的Map格式 */ public static void sendPost(String url, Map<String, String> paramMap) { logger.info('開始發起POST請求,請求地址為{},參數為{}', url, JSON.toJSON(paramMap)); CloseableHttpResponse response = null; try (CloseableHttpClient httpClient = HttpClients.createDefault()) { String encoding = 'utf-8'; //創建post請求對象 HttpPost httpPost = new HttpPost(url); //裝填請求參數 List<NameValuePair> list = new ArrayList<>(); for (Map.Entry<String, String> entry : paramMap.entrySet()) {list.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } //設置參數到請求對象中 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請求發出失敗,請求的地址為{},參數為{},錯誤信息為{}', url, JSON.toJSON(paramMap), e.getMessage(), e); } finally { try {if (response != null) { response.close();} } catch (IOException e) {logger.error('POST請求response關閉異常,錯誤信息為{}', e.getMessage(), e); } } } /** * 發起GET請求 * * @param urlParam url請求,包含參數 */ public static void sendGet(String urlParam) { logger.info('開始發起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請求發出成功,請求的地址為{},返回狀態為{}', urlParam, status); } catch (IOException e) { logger.error('GET請求發出失敗,請求的地址為{},錯誤信息為{}', urlParam, e.getMessage(), e); } finally { try {if (response != null) { response.close();} } catch (IOException e) {logger.error('GET請求response關閉異常,錯誤信息為{}', e.getMessage(), e); } } } public static void main(String[] args) { String url = 'https://jiashubing.cn/tencenttest'; //需要傳入的參數 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請求想要發送Json 格式的數據,只需要修改成這樣:

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 欧美特黄aaaaaa | 伊人五月天婷婷琪琪综合 | 成人看片黄a免费 | 精品国产日韩久久亚洲 | 性欧美video另类bd | 亚洲视频在线网 | 精品一区二区三区视频 | 国产精品三级一区二区 | 国产在线视频网址 | 日产日韩亚洲欧美综合搜索 | 国产成人亚洲精品77 | 理伦毛片| 99国产精品免费观看视频 | 九九99香蕉在线视频网站 | 精品久久久影院 | 成年女人黄小视频 | 久久欧美精品欧美久久欧美 | 成人性生免费视频 | 久久久久久99精品 | 国产成人精品视频免费 | 亚洲欧洲精品国产二码 | 亚洲一区二区三区四区五区六区 | 日韩视频在线观看中字 | 国产一级一国产一级毛片 | 久久久久久网址 | 悟空影视大全免费影视 | 亚洲精品久久99久久一区 | 欧美精品一区视频 | 偷拍自拍第一页 | 欧美一级欧美一级毛片 | 欧美一级毛片激情 | 久草精彩视频 | 国产成人理在线观看视频 | 久久在视频 | 国内精品免费一区二区三区 | 久久精品国产欧美成人 | 国产高清成人mv在线观看 | 视频二区好吊色永久视频 | 国产精品夜色视频一区二区 | 成年人免费黄色 | 久久韩国 |