package shop.alien.util.httpsend.apache;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Post请求
 *
 * @author ssk
 * @version 1.0
 * @date 2020/4/22 10:16
 */
public class HttpPostRequestUtil {

    public static void main(String[] args) {
        //请求参数
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("", "");
        String url = "https://www.mxnzp.com/api";
        url = url + "/verifycode/code?len=5&type=0";
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("app_id", "ej6plljgwnwcvl8u");
        headerMap.put("app_secret", "UUUrYjhCL3RLQjFpZlBBbGd3cHFKZz09");
        HttpPostRequestUtil httpPostRequestUtil = new HttpPostRequestUtil();
        String result = httpPostRequestUtil.postRequest(url, jsonObject, headerMap);
        System.out.println(result);
    }

    /**
     * Post请求
     *
     * @param url        请求url
     * @param jsonObject 请求参数
     * @param headerMap  请求头
     * @return 返回结果
     */
    public static String postRequest(String url, JSONObject jsonObject, Map<String, String> headerMap) {
        String result;
        HttpPost post = new HttpPost(url);
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            //请求头
            post.setHeader("Content-Type", "application/json;charset=utf-8");
            //设置Header
            if (!headerMap.isEmpty()) {
                Set<String> set = headerMap.keySet();
                for (String next : set) {
                    post.setHeader(next, headerMap.get(next));
                }
            }
            StringEntity postingString = new StringEntity(jsonObject.toString(), StandardCharsets.UTF_8);
            post.setEntity(postingString);
            HttpResponse response = httpClient.execute(post);
            InputStream in = response.getEntity().getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
            StringBuilder stringBuilder = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                stringBuilder.append(line).append('\n');
            }
            br.close();
            in.close();
            result = stringBuilder.toString();
            if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                result = "服务器异常";
            }
        } catch (Exception e) {
            System.out.println("请求异常");
            throw new RuntimeException(e);
        } finally {
            post.abort();
        }
        return result;
    }
}
