• java一个调用webapi的工具类


    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();
    }


      

  • 相关阅读:
    创业艰难
    2013.5.20 a
    碱基对
    安装免费在线客服livezilla系统
    简单实现后台订单页面 上一单,下一单 的按钮
    zencart新进产品滚动
    ZEN CART 一级分类显示所有二级分类产品
    frame弹出框
    zen cart 打开错误信息显示调试开关
    zencart外贸网站促销方案
  • 原文地址:https://www.cnblogs.com/lpq21314/p/13169708.html
Copyright © 2020-2023  润新知