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

com.github.hackerwin7.jlib.utils.drivers.url.HttpClient Maven / Gradle / Ivy

There is a newer version: 0.1.1
Show newest version
package com.github.hackerwin7.jlib.utils.drivers.url;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: hackerwin7
 * Date: 2015/12/15
 * Time: 10:32 AM
 * Desc: getOrigin or post to request and response
 */
public class HttpClient {
    /*logger*/
    private static Logger logger = Logger.getLogger(HttpClient.class);
    /*driver*/
    private DefaultHttpClient client = null;
    /*constants*/
    public static final String DEFAULT_CHARSET = "UTF-8";

    /**
     * constructor
     */
    public HttpClient() {
        client = new DefaultHttpClient();
    }

    /**
     * connect
     */
    public void connect() {
        client = new DefaultHttpClient();
    }

    /**
     * http getOrigin
     * @param url
     * @return string
     */
    public String get(String url) throws Exception {
        HttpGet request = new HttpGet(url);
        HttpResponse response = client.execute(request);
        return EntityUtils.toString(response.getEntity(), DEFAULT_CHARSET);
    }

    /**
     * http post
     * @param url
     * @return string
     * @throws Exception
     */
    public String post(String url, Map params) throws Exception {
        HttpPost request = new HttpPost(url);
        List pairs = new ArrayList<>();
        for(Map.Entry entry : params.entrySet()) {
            String key = entry.getKey();
            String val = entry.getValue();
            pairs.add(new BasicNameValuePair(key, val));
        }
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs, DEFAULT_CHARSET);
        request.setEntity(entity);
        HttpResponse response = client.execute(request);
        return EntityUtils.toString(response.getEntity(), DEFAULT_CHARSET);
    }

    /**
     * close the http client
     */
    public void disconnect() {
        if(client != null)
            client.getConnectionManager().shutdown();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy