<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          java-requestsHTTP 請(qǐng)求庫(kù)

          聯(lián)合創(chuàng)作 · 2023-09-29 08:39

          Java的世界里,HttpClient 是一個(gè)功能強(qiáng)大的Http請(qǐng)求庫(kù),然而接口非常復(fù)雜,設(shè)計(jì)上遵從正交性,簡(jiǎn)單的請(qǐng)求也需要寫(xiě)比較多的代碼,更不要說(shuō)隱藏在各種細(xì)節(jié)里面的高級(jí)用法了。

          Requests,  是一個(gè)模仿python requests 模塊來(lái)設(shè)計(jì)的Http lib,擁有簡(jiǎn)單而靈活的API,在容易使用的同時(shí),又能夠滿足各種高級(jí)定制的使用,可是說(shuō)是當(dāng)前最好用的Java Http Client Lib。

          簡(jiǎn)單的請(qǐng)求示例:

          String url = ...;
          Response<String> resp = Requests.get(url).text();
          
          // post 和其他方法
          resp = Requests.post(url).text();
          resp = Requests.head(url).text();
          
          //讀取Http Response 
          int statusCode = resp.getStatusCode();
          Headers headers = resp.getHeaders();
          Cookies cookies = resp.getCookies();
          String body = resp.getBody();
          
          //response 返回其他類(lèi)型
          resp = Requests.get(url).text("UTF-8");
          
          // get response as bytes
          Response<byte[]> resp1 = Requests.get(url).bytes();
          
          // save response as file 
          Response<File> resp2 = Requests.get(url).file("/path/to/save/file");
          
          // url 參數(shù):
          Map<String, Object> map = new HashMap<>();
          map.put("k1", "v1");
          map.put("k2", "v2");
          Response<String> resp = Requests.get(url).param("key1", "value1").params(map)
                  //.params(new Parameter(...), new Parameter(...))
                  .text();
          
          // 請(qǐng)求頭
          Response<String> resp = Requests.get(url).header("key1", "value1").headers(map)
                  //.headers(new Header(...), new Header(...))
                  .text();
          
          // 添加Cookie:
          Map<String, Object> cookies = new HashMap<>();
          Response<String> resp = Requests.get(url).cookie("key1", "value1").cookies(map)
                  //.cookies(new Cookie(...), new Cookie(...))
                  .text();
          
          //  設(shè)置 userAgent
          Response<String> resp = Requests.get(url).userAgent(userAgent).text();
          
          // 增加請(qǐng)求數(shù)據(jù)(post, put, patch方法)
          
          // send form-encoded data. x-www-form-urlencoded header will be send automatically
          Response<String> resp = Requests.post(url).data(map).text();
          
          // send string data
          String str = ...;
          resp = Requests.post(url).data(str, "UTF-8").text();
          
          // send from inputStream
          InputStream in = ...
          resp = Requests.post(url).data(in).text();
          
          // multipart 請(qǐng)求, 用于文件上傳:
          Response<String> resp = Requests.post(url).data(map).multiPart("ufile", "/path/to/file")
                  .multiPart(..., ...).text();

          請(qǐng)求設(shè)置:

          //禁止自動(dòng)重定向
          Response<String> resp = Requests.get(url).allowRedirects(false).text();
          
          //超時(shí)
          // both connec timeout, and socket timeout
          Response<String> resp = Requests.get(url).timeout(30_1000).text();
          
          // set connect timeout and socket timeout
          resp = Requests.get(url).timeout(30_1000, 30_1000).text();
          
          //禁止使用gzip
          Response<String> resp = Requests.get(url).gzip(false).text();
          
          // 不檢查https 證書(shū)
          Response<String> resp = Requests.get(url).verify(false).text();
          
          // Http Basic 驗(yàn)證
          Response<String> resp = Requests.get(url).auth("user", "passwd").verify(false).text();
          
          //代理,支持http, https, socks 代理
          Response<String> resp = Requests.get("http://www.baidu.com/")
                  .proxy(Proxy.httpProxy("127.0.0.1", 8080))
                  .text();

          Session. session 可以用來(lái)維持http 會(huì)話,自動(dòng)處理cookie, basic auth 等信息:

          Session session = Requests.session();
          Response<String> resp1 = session.get(url1).text();
          Response<String> resp2 = session.get(url2).text();

          連接池. 連接池可以用來(lái)在多個(gè)請(qǐng)求之間復(fù)用Http 連接:

          ConnectionPool connectionPool = ConnectionPool.custom().verify(false)
                 .maxPerRoute(20)
                 .maxTotal(100)
                 //.proxy(...)
                 .build();
          Response<String> resp1 = Requests.get(url1).connectionPool(connectionPool).text();
          Response<String> resp2 = Requests.get(url2).connectionPool(connectionPool).text();
          connectionPool.close();

          如果使用了連接池,verify 和 proxy 需要在連接池中設(shè)置,Requests 中的設(shè)置無(wú)效。

          瀏覽 27
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  五月婷婷操逼网 | 成人做爰黄 片免费观看18 | 国产精品久久久久久高潮 | 国产伦精品一区二区三区成人片 | 午夜美女福利视频 |