欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2020年09月17日 11:05:10   作者:賈樹(shù)丙  
這篇文章主要介紹了Java httpcomponents發(fā)送get post請(qǐng)求代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

引入的包為:

<!-- 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)的工具類(lèi)為:

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請(qǐng)求
   *
   * @param url   url
   * @param paramMap 參數(shù)的Map格式
   */
  public static void sendPost(String url, Map<String, String> paramMap) {
    logger.info("開(kāi)始發(fā)起POST請(qǐng)求,請(qǐng)求地址為{},參數(shù)為{}", url, JSON.toJSON(paramMap));
    CloseableHttpResponse response = null;

    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
      String encoding = "utf-8";
      //創(chuàng)建post請(qǐng)求對(duì)象
      HttpPost httpPost = new HttpPost(url);
      //裝填請(qǐng)求參數(shù)
      List<NameValuePair> list = new ArrayList<>();
      for (Map.Entry<String, String> entry : paramMap.entrySet()) {
        list.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
      }
      //設(shè)置參數(shù)到請(qǐng)求對(duì)象中
      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請(qǐng)求發(fā)出失敗,請(qǐng)求的地址為{},參數(shù)為{},錯(cuò)誤信息為{}", url, JSON.toJSON(paramMap), e.getMessage(), e);
    } finally {
      try {
        if (response != null) {
          response.close();
        }
      } catch (IOException e) {
        logger.error("POST請(qǐng)求response關(guān)閉異常,錯(cuò)誤信息為{}", e.getMessage(), e);
      }
    }
  }

  /**
   * 發(fā)起GET請(qǐng)求
   *
   * @param urlParam url請(qǐng)求,包含參數(shù)
   */
  public static void sendGet(String urlParam) {
    logger.info("開(kāi)始發(fā)起GET請(qǐng)求,請(qǐng)求地址為{}", 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請(qǐng)求發(fā)出成功,請(qǐng)求的地址為{},返回狀態(tài)為{}", urlParam, status);
    } catch (IOException e) {
      logger.error("GET請(qǐng)求發(fā)出失敗,請(qǐng)求的地址為{},錯(cuò)誤信息為{}", urlParam, e.getMessage(), e);
    } finally {
      try {
        if (response != null) {
          response.close();
        }
      } catch (IOException e) {
        logger.error("GET請(qǐng)求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賈樹(shù)丙";
    sendGet(urlParam);
  }
}

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

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

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

相關(guān)文章

  • 使用Servlet Filter實(shí)現(xiàn)系統(tǒng)登錄權(quán)限

    使用Servlet Filter實(shí)現(xiàn)系統(tǒng)登錄權(quán)限

    這篇文章主要為大家詳細(xì)介紹了使用Servlet Filter實(shí)現(xiàn)系統(tǒng)登錄權(quán)限,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • java使用JDBC動(dòng)態(tài)創(chuàng)建數(shù)據(jù)表及SQL預(yù)處理的方法

    java使用JDBC動(dòng)態(tài)創(chuàng)建數(shù)據(jù)表及SQL預(yù)處理的方法

    這篇文章主要介紹了java使用JDBC動(dòng)態(tài)創(chuàng)建數(shù)據(jù)表及SQL預(yù)處理的方法,涉及JDBC操作數(shù)據(jù)庫(kù)的連接、創(chuàng)建表、添加數(shù)據(jù)、查詢(xún)等相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-08-08
  • 關(guān)于maven install報(bào)錯(cuò)原因揭秘:parent.relativePath指向錯(cuò)誤的本地POM文件

    關(guān)于maven install報(bào)錯(cuò)原因揭秘:parent.relativePath指向錯(cuò)誤的本地POM文件

    在使用Maven進(jìn)行項(xiàng)目構(gòu)建時(shí),如果遇到'parent.relativePath'指向錯(cuò)誤的本地POM文件的問(wèn)題,可能會(huì)導(dǎo)致構(gòu)建失敗,這通常是由于父項(xiàng)目POM文件的相對(duì)路徑設(shè)置錯(cuò)誤、本地POM文件與父項(xiàng)目POM文件版本或內(nèi)容不一致所致,解決方法包括檢查并修正父項(xiàng)目POM文件中的相對(duì)路徑設(shè)置
    2024-09-09
  • Spring 自動(dòng)裝配的二義性實(shí)例解析

    Spring 自動(dòng)裝配的二義性實(shí)例解析

    這篇文章主要介紹了Spring 自動(dòng)裝配的二義性實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Java多線(xiàn)程案例之阻塞隊(duì)列詳解

    Java多線(xiàn)程案例之阻塞隊(duì)列詳解

    阻塞隊(duì)列是一種特殊的隊(duì)列.?也遵守?“先進(jìn)先出”?的原則.阻塞隊(duì)列能是一種線(xiàn)程安全的數(shù)據(jù)結(jié)構(gòu)。本文將通過(guò)一些示例為大家詳細(xì)講講阻塞隊(duì)列的原理與使用,感興趣的小伙伴可以學(xué)習(xí)一下
    2022-10-10
  • Java數(shù)組的聲明與創(chuàng)建示例詳解

    Java數(shù)組的聲明與創(chuàng)建示例詳解

    這篇文章主要介紹了Java數(shù)組的聲明與創(chuàng)建示例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 淺析Spring Boot中的spring-boot-load模塊

    淺析Spring Boot中的spring-boot-load模塊

    spring-boot-loader模塊允許我們使用java -jar archive.jar運(yùn)行包含嵌套依賴(lài)jar的jar或者war文件,它提供了三種類(lèi)啟動(dòng)器。下面通過(guò)本文給大家介紹spring-boot-load模塊的相關(guān)知識(shí),感興趣的朋友一起看看吧
    2018-01-01
  • Mybatis中單雙引號(hào)引發(fā)的慘案及解決

    Mybatis中單雙引號(hào)引發(fā)的慘案及解決

    這篇文章主要介紹了Mybatis中單雙引號(hào)引發(fā)的慘案及解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Java實(shí)現(xiàn)多叉樹(shù)和二叉樹(shù)之間的互轉(zhuǎn)

    Java實(shí)現(xiàn)多叉樹(shù)和二叉樹(shù)之間的互轉(zhuǎn)

    本文主要介紹了Java實(shí)現(xiàn)多叉樹(shù)和二叉樹(shù)之間的互轉(zhuǎn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • 詳解Spring注解 @Configuration

    詳解Spring注解 @Configuration

    @Configuration 是 Spring 中的一個(gè)注解,它用于標(biāo)記一個(gè)類(lèi)為配置類(lèi),通過(guò)配置類(lèi)可以定義和組裝 Spring Bean,并且支持高度靈活的配置方式。本問(wèn)詳細(xì)介紹了Spring注解 @Configuration,感興趣的小伙伴可以參考一下
    2023-04-04

最新評(píng)論