1. 程式人生 > 程式設計 >Java commons-httpclient如果實現get及post請求

Java commons-httpclient如果實現get及post請求

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

引入的jar包為:

<!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
<dependency>
  <groupId>commons-httpclient</groupId>
  <artifactId>commons-httpclient</artifactId>
  <version>3.1</version>
</dependency>

具體實現類為:

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() {

  }

  /**
   * 發起POST請求
   *
   * @param url    url
   * @param paramJson 引數的json格式
   */
  public static String sendPost(String url,String paramJson) {
    logger.info("開始發起POST請求,請求地址為{},引數為{}",url,paramJson);

    // 建立httpClient例項物件
    HttpClient httpClient = new HttpClient();
    // 設定httpClient連線主機伺服器超時時間:15000毫秒
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
    // 建立post請求方法例項物件
    PostMethod postMethod = new PostMethod(url);
    // 設定post請求超時時間
    postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,60000);
    postMethod.addRequestHeader("Content-Type","application/json");
    try {
      //json格式的引數解析
      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請求發出失敗,請求的地址為{},引數為{},錯誤資訊為{}",paramJson,e.getMessage(),e);
    }
    return null;
  }

  /**
   * 發起GET請求
   *
   * @param urlParam url請求,包含引數
   */
  public static String sendGet(String urlParam) {
    logger.info("開始發起GET請求,請求地址為{}",urlParam);
    // 建立httpClient例項物件
    HttpClient httpClient = new HttpClient();
    // 設定httpClient連線主機伺服器超時時間:15000毫秒
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
    // 建立GET請求方法例項物件
    GetMethod getMethod = new GetMethod(urlParam);
    // 設定post請求超時時間
    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請求發出失敗,請求的地址為{},錯誤資訊為{}",urlParam,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);
  }

}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。