자바 웹 api 를 호출 하 는 도구 클래스

8225 단어
package com.hy.fddsvr.utils;
 
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import com.fadada.sdk.util.http.SSLClient;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.util.EntityUtils;

public class MyHttpsUtil {
    public static final String charset = "UTF-8";

    public MyHttpsUtil() {
    }

    public static String doPost(String url, List params) {
        return doPost(url, params, 15000, 35000);
    }

    public static String doCaPost(String url, List params) {
        return doPost(url, params, 5000, 180000);
    }

    public static String doPost(String url, List params, int connect_time, int timeout) {
        HttpClient httpClient = null;
        HttpPost httpPost = null;

        String var8;
        try {
            httpClient = new SSLClient();
            httpPost = new HttpPost(url);
            httpClient.getParams().setParameter("http.connection.timeout", connect_time);
            httpClient.getParams().setParameter("http.socket.timeout", timeout);
            if (null != params && params.size() > 0) {
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
                httpPost.setEntity(entity);
            }

            //ProxyHttpClient.getProxyHttpClient(httpClient);
            HttpResponse response = httpClient.execute(httpPost);
            if (response == null) {
                throw new RuntimeException("HttpResponse is null.");
            }

            if (response.getStatusLine().getStatusCode() != 200) {
                throw new RuntimeException("connect fail. http_status:" + response.getStatusLine().getStatusCode());
            }

            HttpEntity entity = response.getEntity();
            if (null == entity) {
                throw new RuntimeException("HttpEntity is null.");
            }

            var8 = EntityUtils.toString(entity, "UTF-8");
        } catch (Exception var17) {
            var17.printStackTrace();
            throw new RuntimeException(var17);
        } finally {
            try {
                httpClient.getConnectionManager().shutdown();
            } catch (Exception var16) {
                var16.printStackTrace();
            }

        }

        return var8;
    }

    public static String doPost(String url, HttpEntity entity) {
        HttpClient httpClient = null;
        HttpPost httpPost = null;

        String var6;
        try {
            httpClient = new SSLClient();
            httpPost = new HttpPost(url);
            httpPost.setEntity(entity);
            ProxyHttpClient.getProxyHttpClient(httpClient);
            HttpResponse response = httpClient.execute(httpPost);
            if (response == null) {
                throw new RuntimeException("http_response is null.");
            }

            if (response.getStatusLine().getStatusCode() != 200) {
                throw new RuntimeException("connect fail. http_status:" + response.getStatusLine().getStatusCode());
            }

            HttpEntity respEtity = response.getEntity();
            if (null == entity) {
                throw new RuntimeException("response http_entity is null.");
            }

            var6 = EntityUtils.toString(respEtity, "UTF-8");
        } catch (Exception var15) {
            var15.printStackTrace();
            throw new RuntimeException(var15);
        } finally {
            try {
                httpClient.getConnectionManager().shutdown();
            } catch (Exception var14) {
                var14.printStackTrace();
            }

        }

        return var6;
    }

    public static String doPostDownload(String path, String url, List params) {
        HttpClient httpClient = null;
        HttpPost httpPost = null;

        try {
            httpClient = new SSLClient();
            httpPost = new HttpPost(url);
            if (null != params && params.size() > 0) {
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
                httpPost.setEntity(entity);
            }

            ProxyHttpClient.getProxyHttpClient(httpClient);
            HttpResponse response = httpClient.execute(httpPost);
            if (response == null) {
                throw new RuntimeException("HttpResponse is null.");
            }

            if (response.getStatusLine().getStatusCode() != 200) {
                throw new RuntimeException("connect fail. http_status:" + response.getStatusLine().getStatusCode());
            }

            HttpEntity entity = response.getEntity();
            if (null == entity) {
                throw new RuntimeException("HttpEntity is null.");
            }

            if (!"application/zip".equals(response.getEntity().getContentType().getValue()) && !"application/pdf".equals(response.getEntity().getContentType().getValue())) {
                String var37 = EntityUtils.toString(entity, "UTF-8");
                return var37;
            }

            byte[] result = EntityUtils.toByteArray(response.getEntity());
            BufferedOutputStream bw = null;

            try {
                File f = new File(path);
                if (f.exists()) {
                    f.delete();
                }

                if (!f.getParentFile().exists()) {
                    f.getParentFile().mkdirs();
                }

                bw = new BufferedOutputStream(new FileOutputStream(path));
                bw.write(result);
            } catch (Exception var32) {
                throw new RuntimeException(var32);
            } finally {
                try {
                    if (bw != null) {
                        bw.close();
                    }
                } catch (Exception var31) {
                    var31.printStackTrace();
                }

            }
        } catch (Exception var34) {
            var34.printStackTrace();
            throw new RuntimeException(var34);
        } finally {
            try {
                httpClient.getConnectionManager().shutdown();
            } catch (Exception var30) {
                var30.printStackTrace();
            }

        }

        return null;
    }

    public static String getTimeStamp() {
        Timestamp ts = new Timestamp(System.currentTimeMillis());
        DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(ts);
    }
}





import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;


。。。。。。

//      
List urlParameters = new ArrayList<>();



try {
//
urlParameters.add(new BasicNameValuePair("doc_title", ADocTitle));
urlParameters.add(new BasicNameValuePair("template_id", ATemplateId));
urlParameters.add(new BasicNameValuePair("contract_id", AContractId));
urlParameters.add(new BasicNameValuePair("font_size", AFontSize));
urlParameters.add(new BasicNameValuePair("font_type", AFontType));
urlParameters.add(new BasicNameValuePair("parameter_map", AParameterMap));
urlParameters.add(new BasicNameValuePair("msg_digest", LMsgDigest));

res = MyHttpsUtil.doPost(url,urlParameters);
}catch (Exception ex){
ex.printStackTrace();
}



좋은 웹페이지 즐겨찾기