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

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

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

服務(wù)器之家 - 編程語言 - JAVA教程 - Java commons-httpclient如果實(shí)現(xiàn)get及post請(qǐng)求

Java commons-httpclient如果實(shí)現(xiàn)get及post請(qǐng)求

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

這篇文章主要介紹了Java commons-httpclient如果實(shí)現(xiàn)get及post請(qǐng)求,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

PS:這個(gè)jar包,在2007年之后就沒有更新過了, 是比較老的版本了。追求新的版本 用HttpComponents 比較好

引入的jar包為:

?
1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
<dependency>
  <groupId>commons-httpclient</groupId>
  <artifactId>commons-httpclient</artifactId>
  <version>3.1</version>
</dependency>

具體實(shí)現(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
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.IOException;
 
public class HttpClientHelper {
  private static Logger logger = LoggerFactory.getLogger(HttpClientHelper.class);
 
  private HttpClientHelper() {
 
  }
 
  /**
   * 發(fā)起POST請(qǐng)求
   *
   * @param url    url
   * @param paramJson 參數(shù)的json格式
   */
  public static String sendPost(String url, String paramJson) {
    logger.info("開始發(fā)起POST請(qǐng)求,請(qǐng)求地址為{},參數(shù)為{}", url, paramJson);
 
    // 創(chuàng)建httpClient實(shí)例對(duì)象
    HttpClient httpClient = new HttpClient();
    // 設(shè)置httpClient連接主機(jī)服務(wù)器超時(shí)時(shí)間:15000毫秒
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
    // 創(chuàng)建post請(qǐng)求方法實(shí)例對(duì)象
    PostMethod postMethod = new PostMethod(url);
    // 設(shè)置post請(qǐng)求超時(shí)時(shí)間
    postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
    postMethod.addRequestHeader("Content-Type", "application/json");
    try {
      //json格式的參數(shù)解析
      RequestEntity entity = new StringRequestEntity(paramJson, "application/json", "UTF-8");
      postMethod.setRequestEntity(entity);
 
      httpClient.executeMethod(postMethod);
      String result = postMethod.getResponseBodyAsString();
      postMethod.releaseConnection();
      return result;
    } catch (IOException e) {
      logger.error("POST請(qǐng)求發(fā)出失敗,請(qǐng)求的地址為{},參數(shù)為{},錯(cuò)誤信息為{}", url, paramJson, e.getMessage(), e);
    }
    return null;
  }
 
  /**
   * 發(fā)起GET請(qǐng)求
   *
   * @param urlParam url請(qǐng)求,包含參數(shù)
   */
  public static String sendGet(String urlParam) {
    logger.info("開始發(fā)起GET請(qǐng)求,請(qǐng)求地址為{}", urlParam);
    // 創(chuàng)建httpClient實(shí)例對(duì)象
    HttpClient httpClient = new HttpClient();
    // 設(shè)置httpClient連接主機(jī)服務(wù)器超時(shí)時(shí)間:15000毫秒
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
    // 創(chuàng)建GET請(qǐng)求方法實(shí)例對(duì)象
    GetMethod getMethod = new GetMethod(urlParam);
    // 設(shè)置post請(qǐng)求超時(shí)時(shí)間
    getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
    getMethod.addRequestHeader("Content-Type", "application/json");
    try {
      httpClient.executeMethod(getMethod);
      String result = getMethod.getResponseBodyAsString();
      getMethod.releaseConnection();
      logger.info("返回信息為{}", result);
      return result;
    } catch (IOException e) {
      logger.error("GET請(qǐng)求發(fā)出失敗,請(qǐng)求的地址為{},錯(cuò)誤信息為{}", urlParam, e.getMessage(), e);
    }
    return null;
  }
 
  public static void main(String[] args) {
    String url = "https://jiashubing.cn/tencenttest";
    String param = "{\"aaa\":\"bbbbbbb\"}";
    sendPost(url, param);
    String urlParam = "https://jiashubing.cn/talk/document?fileid=1234";
    sendGet(urlParam);
  }
 
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩在线免费 | 香蕉久久一区二区三区 | 好大好硬好长好爽a网站 | 欧美激情影音先锋 | www.99精品| 成人福利网站含羞草 | 韩国免费特一级毛片 | 亚洲 欧美 中文字幕 在线 | 青草视频免费观看 | 国产亚洲女人久久久久久 | 九九精品免视看国产成人 | 青青操在线播放 | 免费二区 | 无码一区二区三区视频 | cosplay 极品videos| 全彩成人18h漫画 | 天天狠天天天天透在线 | 范冰冰特黄xx大片 | 视频一区二区在线 | 午夜毛片在线观看 | 青柠影视在线播放观看高清 | 国产精品一区久久精品 | 激情乱文| 美女脱了内裤打开腿让人桶网站o | 欧美专区亚洲 | 精品视频在线播放 | 四虎影视紧急入口地址大全 | 动漫美女人物被黄漫在线看 | 亚洲狼人综合干 | 日本不卡一区二区三区在线观看 | 农村妇女野外牲交一级毛片 | 福利一区在线观看 | 国产五月天在线 | 亚洲精品一区二区久久这里 | 美人的淫事[纯hh] | 亚洲精品91| 欧美最猛性xxxxx男男 | 久久综合给会久久狠狠狠 | 成人福利在线 | 男人猛进女人屁股免费 | 美女林柏欣21p人体之仓之梦 |