/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  org.apache.http.Header
 *  org.apache.http.HttpEntity
 *  org.apache.http.client.config.RequestConfig
 *  org.apache.http.client.entity.UrlEncodedFormEntity
 *  org.apache.http.client.methods.CloseableHttpResponse
 *  org.apache.http.client.methods.HttpPost
 *  org.apache.http.client.methods.HttpUriRequest
 *  org.apache.http.impl.client.CloseableHttpClient
 *  org.apache.http.impl.client.HttpClients
 *  org.apache.http.message.BasicNameValuePair
 */
package com.hst.taxService.util;

import com.hst.taxService.util.Compress;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ProtocolException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

public class HttpClientService {
    private static final int CONNECTION_TIMEOUT = 3000;
    private static final int READ_DATA_TIMEOUT = 3000;
    private static final int successCode = 200;

    public String sendSyncSingleHttp(String url, Map<String, String> headers, String param) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        RequestConfig defaultRequestConfig = RequestConfig.custom().build();
        RequestConfig requestConfig = RequestConfig.copy((RequestConfig)defaultRequestConfig).setSocketTimeout(3000).setConnectTimeout(3000).build();
        ArrayList<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
        CloseableHttpResponse response = null;
        String compress = headers.get("compress");
        try {
            HttpPost httpPost = HttpClientService.setConnectionParam(url, headers);
            param = URLEncoder.encode(param, "UTF-8");
            if ("GZIP".equals(compress)) {
                param = Compress.compress(param);
            }
            nvps.add(new BasicNameValuePair("param", param));
            UrlEncodedFormEntity utlencode = new UrlEncodedFormEntity(nvps, "UTF-8");
            httpPost.setEntity((HttpEntity)utlencode);
            httpPost.setConfig(requestConfig);
            response = httpclient.execute((HttpUriRequest)httpPost);
            String string = this.printResult(response);
            return string;
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                if (response != null) {
                    response.close();
                }
                if (httpclient != null) {
                    httpclient.close();
                }
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    private String printResult(CloseableHttpResponse response) {
        String str = "";
        InputStream input = null;
        if (response.getStatusLine().getStatusCode() == 200) {
            try {
                try {
                    input = response.getEntity().getContent();
                    str = URLDecoder.decode(this.inputStreamToString(input), "UTF-8");
                    Header[] heahers = response.getAllHeaders();
                    System.out.println("\u8fd4\u56de\u6d88\u606f\u5934\uff1a");
                    Header[] headerArray = heahers;
                    int n = heahers.length;
                    int n2 = 0;
                    while (n2 < n) {
                        Header header = headerArray[n2];
                        System.out.println(String.valueOf(header.getName()) + ":" + header.getValue());
                        ++n2;
                    }
                }
                catch (Exception e) {
                    e.printStackTrace();
                    try {
                        if (input != null) {
                            input.close();
                        }
                        response.close();
                    }
                    catch (IOException e2) {
                        e2.printStackTrace();
                    }
                }
            }
            finally {
                try {
                    if (input != null) {
                        input.close();
                    }
                    response.close();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else {
            System.out.println(response.getStatusLine().getStatusCode());
        }
        return str;
    }

    private static HttpPost setConnectionParam(String url, Map<String, String> headers) throws IOException, ProtocolException {
        HttpPost hpost = new HttpPost(url);
        Set<String> headerSet = headers.keySet();
        for (String key : headerSet) {
            hpost.setHeader(key, headers.get(key));
        }
        return hpost;
    }

    public String inputStreamToString(InputStream input) throws IOException {
        BufferedReader br = null;
        String data = "";
        StringBuffer sb = new StringBuffer();
        br = new BufferedReader(new InputStreamReader(input));
        while ((data = br.readLine()) != null) {
            sb.append(data);
        }
        return sb.toString();
    }
}
