cn.aofeng.demo.httpclient.HttpClientBasic.java Source code

Java tutorial

Introduction

Here is the source code for cn.aofeng.demo.httpclient.HttpClientBasic.java

Source

/**
 * 2016-2-23
 */
package cn.aofeng.demo.httpclient;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
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.FileEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

/**
 * HttpClient?
 * 
 * @author <a href="mailto:aofengblog@163.com">?</a>
 */
public class HttpClientBasic {

    private static Logger _logger = Logger.getLogger(HttpClientBasic.class);

    private static String _targetHost = "http://127.0.0.1:8888";

    private static String _charset = "utf-8";

    public void get() throws URISyntaxException, ClientProtocolException, IOException {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet get = new HttpGet(_targetHost + "/get");
        CloseableHttpResponse response = client.execute(get);
        processResponse(response);
    }

    public void post() throws ClientProtocolException, IOException {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("chinese", ""));
        params.add(new BasicNameValuePair("english", ""));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, _charset);

        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(_targetHost + "/post");
        post.addHeader("Cookie", "character=abcdefghijklmnopqrstuvwxyz; sign=abc-123-jkl-098");
        post.setEntity(entity);
        CloseableHttpResponse response = client.execute(post);
        processResponse(response);
    }

    public void sendFile(String filePath) throws UnsupportedOperationException, IOException {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(_targetHost + "/file");
        File file = new File(filePath);
        FileEntity entity = new FileEntity(file,
                ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), _charset));
        post.setEntity(entity);
        CloseableHttpResponse response = client.execute(post);
        processResponse(response);
    }

    private void processResponse(CloseableHttpResponse response) throws UnsupportedOperationException, IOException {
        try {
            // ??
            Header[] headers = response.getAllHeaders();
            for (Header header : headers) {
                _logger.info(header.getName() + ":" + header.getValue());
            }

            // ???
            StatusLine sl = response.getStatusLine();
            _logger.info(String.format("ProtocolVersion:%s, StatusCode:%d, Desc:%s",
                    sl.getProtocolVersion().toString(), sl.getStatusCode(), sl.getReasonPhrase()));

            // ??
            HttpEntity entity = response.getEntity();
            _logger.info(String.format("ContentType:%s, Length:%d, Encoding:%s",
                    null == entity.getContentType() ? "" : entity.getContentType().getValue(),
                    entity.getContentLength(),
                    null == entity.getContentEncoding() ? "" : entity.getContentEncoding().getValue()));
            _logger.info(EntityUtils.toString(entity, _charset));
            //            _logger.info( IOUtils.toString(entity.getContent(), _charset) ); // ??????
        } finally {
            response.close();
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        HttpClientBasic basic = new HttpClientBasic();
        //        basic.get();
        //        basic.post();
        basic.sendFile("/devdata/projects/open_source/mine/JavaTutorial/LICENSE");
    }

}