Http ClientによるHTTPプロトコルインターフェーステストでの使用(詳細解)


HTTPプロトコルのインターフェーステストでは、GET要求とPOST要求が一番多く使われています。POSTはFORMパラメータ提出要求とRAW要求があります。以下、HttpClientを結合してこの3つの形態を実現します。
一、GET要求:GET要求時、パラメータはリンクに書いてあります。コードは以下の通りです。

public void get(String url){
  CloseableHttpClient httpClient = null;
  HttpGet httpGet = null;
  try {
    httpClient = HttpClients.createDefault();
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();   
    httpGet = new HttpGet(url);
    httpGet.setConfig(requestConfig);
    CloseableHttpResponse response = httpClient.execute(httpGet);
    HttpEntity httpEntity = response.getEntity();
    System.out.println(EntityUtils.toString(httpEntity,"utf-8"));
  } catch (ClientProtocolException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  }finally{
    try {
      if(httpGet!=null){
        httpGet.releaseConnection();
      }
      if(httpClient!=null){
        httpClient.close();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
パラメータをリンクに書かずに個別にパスしたいなら、このようにしてもいいです。

public void get(String url, Map<String, String> params){
  CloseableHttpClient httpClient = null;
  HttpGet httpGet = null;
  try {
    httpClient = HttpClients.createDefault();
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();
    String ps = "";
    for (String pKey : params.keySet()) {
      if(!"".equals(ps)){
        ps = ps + "&";
      }
      ps = pKey+"="+params.get(pKey);
    }
    if(!"".equals(ps)){
      url = url + "?" + ps;
    }
    httpGet = new HttpGet(url);
    httpGet.setConfig(requestConfig);
    CloseableHttpResponse response = httpClient.execute(httpGet);
    HttpEntity httpEntity = response.getEntity();
    System.out.println(EntityUtils.toString(httpEntity,"utf-8"));
  } catch (ClientProtocolException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  }finally{
    try {
      if(httpGet!=null){
        httpGet.releaseConnection();
      }
      if(httpClient!=null){
        httpClient.close();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
二、POST要求のフォーム提出方式、コードは以下の通りです。

public void post(String url, Map<String, String> params){
  CloseableHttpClient httpClient = null;
  HttpPost httpPost = null;
  try {
    httpClient = HttpClients.createDefault();
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();
    httpPost = new HttpPost(url);
    httpPost.setConfig(requestConfig);
    List<NameValuePair> ps = new ArrayList<NameValuePair>();
    for (String pKey : params.keySet()) {
      ps.add(new BasicNameValuePair(pKey, params.get(pKey)));
    }
    httpPost.setEntity(new UrlEncodedFormEntity(ps));
    CloseableHttpResponse response = httpClient.execute(httpPost);
    HttpEntity httpEntity = response.getEntity();
    System.out.println(EntityUtils.toString(httpEntity,"utf-8"));
  } catch (ClientProtocolException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  }finally{
    try {
      if(httpPost!=null){
        httpPost.releaseConnection();
      }
      if(httpClient!=null){
        httpClient.close();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
三、POST要求のRAWパラメータ伝達:

public void post(String url, String body){
  CloseableHttpClient httpClient = null;
  HttpPost httpPost = null;
  try {
    httpClient = HttpClients.createDefault();
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();
    httpPost = new HttpPost(url);
    httpPost.setConfig(requestConfig);
    httpPost.setEntity(new StringEntity(body));
    CloseableHttpResponse response = httpClient.execute(httpPost);
    HttpEntity httpEntity = response.getEntity();
    System.out.println(EntityUtils.toString(httpEntity,"utf-8"));
  } catch (ClientProtocolException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  }finally{
    try {
      if(httpPost!=null){
        httpPost.releaseConnection();
      }
      if(httpClient!=null){
        httpClient.close();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
以上のHttp Clientに基づいてHTTPプロトコルインターフェーステストでの使用(詳細解)は、小編集が皆さんに共有している内容の全部です。参考にしていただければと思います。どうぞよろしくお願いします。