public class HttpClient {
private static final Logger LOG = LogManager.getLogger(HttpClient.class);
public static CloseableHttpClient httpClient = null;
public static HttpClientContext context = null;
public static CookieStore cookieStore = null;
public static RequestConfig requestConfig = null;
private static void init() {
context = HttpClientContext.create();
cookieStore = new BasicCookieStore();
requestConfig = RequestConfig.custom().setConnectTimeout(120000).setSocketTimeout(60000)
.setConnectionRequestTimeout(60000).build();
httpClient = HttpClientBuilder.create().setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy())
.setRedirectStrategy(new DefaultRedirectStrategy()).setDefaultRequestConfig(requestConfig)
.setDefaultCookieStore(cookieStore).build();
public static CloseableHttpResponse get(String url) throws ClientProtocolException, IOException {
HttpGet httpget = new HttpGet(url);
CloseableHttpResponse response = httpClient.execute(httpget, context);
cookieStore = context.getCookieStore();
List<Cookie> cookies = cookieStore.getCookies();
for (Cookie cookie : cookies) {
LOG.debug("key:" + cookie.getName() + " value:" + cookie.getValue());
public static CloseableHttpResponse post(String url, String parameters)
throws ClientProtocolException, IOException {
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nvps = toNameValuePairList(parameters);
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
CloseableHttpResponse response = httpClient.execute(httpPost, context);
cookieStore = context.getCookieStore();
List<Cookie> cookies = cookieStore.getCookies();
for (Cookie cookie : cookies) {
LOG.debug("key:" + cookie.getName() + " value:" + cookie.getValue());
@SuppressWarnings("unused")
private static List<NameValuePair> toNameValuePairList(String parameters) {
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
String[] paramList = parameters.split("&");
for (String parm : paramList) {
for (int i = 0; i < parm.length(); i++) {
index = parm.indexOf("=");
String key = parm.substring(0, index);
String value = parm.substring(++index, parm.length());
nvps.add(new BasicNameValuePair(key, value));
System.out.println(nvps.toString());
public void addCookie(String name, String value, String domain, String path) {
BasicClientCookie cookie = new BasicClientCookie(name, value);
cookie.setDomain(domain);
cookieStore.addCookie(cookie);
public static void printResponse(HttpResponse httpResponse) throws ParseException, IOException {
HttpEntity entity = httpResponse.getEntity();
System.out.println("status:" + httpResponse.getStatusLine());
System.out.println("headers:");
HeaderIterator iterator = httpResponse.headerIterator();
while (iterator.hasNext()) {
System.out.println("\t" + iterator.next());
"------------------------------------------------------------------------------------------\r\n");
public static void printCookies() {
System.out.println("headers:");
cookieStore = context.getCookieStore();
List<Cookie> cookies = cookieStore.getCookies();
for (Cookie cookie : cookies) {
System.out.println("key:" + cookie.getName() + " value:" + cookie.getValue());
public static boolean checkCookie(String key) {
cookieStore = context.getCookieStore();
List<Cookie> cookies = cookieStore.getCookies();
for (Cookie cookie : cookies) {
if (cookie.getName().equals(key)) {
public static String toString(CloseableHttpResponse httpResponse) throws ParseException, IOException {
HttpEntity entity = httpResponse.getEntity();
return EntityUtils.toString(entity);