一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術(shù)|正則表達式|

服務(wù)器之家 - 編程語言 - JAVA教程 - Java httpcomponents發(fā)送get post請求代碼實例

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

2020-09-17 14:50賈樹丙 JAVA教程

這篇文章主要介紹了Java httpcomponents發(fā)送get post請求代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

引入的包為:

?
1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.5.8</version>
</dependency>

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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ù)為{},錯誤信息為{}", url, JSON.toJSON(paramMap), e.getMessage(), e);
    } finally {
      try {
        if (response != null) {
          response.close();
        }
      } catch (IOException e) {
        logger.error("POST請求response關(guān)閉異常,錯誤信息為{}", 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ā)出失敗,請求的地址為{},錯誤信息為{}", urlParam, e.getMessage(), e);
    } finally {
      try {
        if (response != null) {
          response.close();
        }
      } catch (IOException e) {
        logger.error("GET請求response關(guān)閉異常,錯誤信息為{}", 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ù)器之家。

原文鏈接:https://www.cnblogs.com/acm-bingzi/p/httpcomponents.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲黄网站wwwwww | 亚洲午夜天堂 | 日本在线色 | 18性夜影院午夜寂寞影院免费 | 国产精品嫩草影院在线 | 天色综合| 亚洲成人福利网站 | 国产在线影院 | 无限观看社区在线视频 | 国产欧美一区二区精品性色99 | xxxxx大片在线观看 | 日本久久啪啪婷婷激情五月 | 国产午夜精品一区二区三区不卡 | 娇妻在床上迎合男人 | 欧美日韩中文字幕一区二区高清 | 亚洲国产精品久久久久久 | 九九免费高清在线观看视频 | 咪咪爱在线视频 | 成年女人免费 | 亚洲欧美日韩精品高清 | 色天天综合网色鬼综合 | 久久这里都是精品 | 亚洲国产中文字幕在线视频综合 | 亚洲天堂一区二区在线观看 | 亚洲国产精品自产在线播放 | free嫩白的12sex性自由 | 碰91精品国产91久久婷婷 | 欧美激情精品久久久久久不卡 | 亚洲AV无码国产精品午夜久久 | 半挠脚心半黄的网站 | 成人性生交大片免费看软件 | 亚洲丰满女人ass硕大 | 变态 另类 人妖小说 | 日本捏胸吃奶视频免费 | 国产一级一级一级成人毛片 | 欧美最猛性xxxxx69交 | 熟睡中的麻麻大白屁股小说 | 韩国三级年轻的小婊孑 | 99影视在线视频免费观看 | 操破苍穹小说 | 青草视频在线观看免费网站 |