All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.clickntap.utils.HttpUtils Maven / Gradle / Ivy

There is a newer version: 1.189
Show newest version
package com.clickntap.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
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.entity.ContentType;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;

public class HttpUtils {

  public static String get(String url, Map headers) throws Exception {
    return HttpUtils.get(url, headers, ConstUtils.UTF_8);
  }

  public static String post(String url, Map headers, Map params) throws Exception {
    return HttpUtils.post(url, headers, params, ConstUtils.UTF_8);
  }

  public static String post(String url, Map headers, InputStream in) throws Exception {
    return HttpUtils.post(url, headers, in, ConstUtils.UTF_8);
  }

  public static String get(String url, Map headers, String encoding) throws Exception {
    CloseableHttpClient client = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet(url);
    headers(request, headers);
    CloseableHttpResponse response = client.execute(request);
    String responseAsString = response(response, encoding);
    response.close();
    client.close();
    return responseAsString;
  }

  public static String post(String url, Map headers, Map params, String encoding) throws Exception {
    CloseableHttpClient client = HttpClientBuilder.create().build();
    HttpPost request = new HttpPost(url);
    if (params != null) {
      ArrayList postParameters = new ArrayList();
      for (String key : params.keySet()) {
        postParameters.add(new BasicNameValuePair(key, params.get(key)));
      }
      HttpEntity entity = new UrlEncodedFormEntity(postParameters);
      request.setEntity(entity);
    }
    headers(request, headers);
    CloseableHttpResponse response = client.execute(request);
    String responseAsString = response(response, encoding);
    response.close();
    client.close();
    return responseAsString;
  }

  private static void headers(HttpRequest request, Map headers) {
    if (headers == null)
      return;
    for (String key : headers.keySet()) {
      request.addHeader(key, headers.get(key));
    }
  }

  public static String post(String url, Map headers, InputStream in, String encoding) throws Exception {
    CloseableHttpClient client = HttpClientBuilder.create().build();
    HttpPost request = new HttpPost(url);
    if (in != null) {
      HttpEntity entity = new InputStreamEntity(in, ContentType.MULTIPART_FORM_DATA);
      request.setEntity(entity);
    }
    headers(request, headers);
    CloseableHttpResponse response = client.execute(request);
    String responseAsString = response(response, encoding);
    response.close();
    client.close();
    return responseAsString;
  }

  private static String response(CloseableHttpResponse response, String encoding) throws Exception {
    String responseAsString = null;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    response.getEntity().writeTo(out);
    responseAsString = out.toString(encoding);
    out.close();
    return responseAsString;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy