package com.reyun.util.http;

import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;

import java.io.UnsupportedEncodingException;

/**
 * description:
 *
 * @author nolan
 * @date 31/08/2017
 */
public class HttpPost extends AbstractHttpSupport {
    public HttpPost(String url) {
        super(new PostMethod(url));
    }

    public HttpPost request(NameValuePair[] nameValuePairs) {
        ((PostMethod) super.httpMethodBase).setRequestBody(nameValuePairs);
        return this;
    }

    public HttpPost request(String json, String contentType, String charset) {
        RequestEntity entity = null;
        try {
            super.withHeader("Content-Type", "application/json;charset=UTF-8");
            entity = new StringRequestEntity(json, contentType, charset);
            ((PostMethod) super.httpMethodBase).setRequestEntity(entity);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return this;
    }
}