码迷,mamicode.com
首页 > 编程语言 > 详细

java一个调用webapi的工具类

时间:2020-06-20 19:43:12      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:shu   http   一个   dir   zip   type   simple   ring   byte   

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<NameValuePair> params) {
        return doPost(url, params, 15000, 35000);
    }

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

    public static String doPost(String url, List<NameValuePair> 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<NameValuePair> 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<NameValuePair> 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();
}


  

java一个调用webapi的工具类

标签:shu   http   一个   dir   zip   type   simple   ring   byte   

原文地址:https://www.cnblogs.com/lpq21314/p/13169708.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!