HttpClientHelper

 
public static String invoke(String jsondata, String url, String appsecret, String account, String version) throws Exception {
        byte[] dataAssembleByte = RSAUtils.encryptByPublicKey(jsondata.getBytes("UTF-8"), appsecret);
        String jsonDataBase64 = Base64.encodeBase64String(dataAssembleByte);
        
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("method", METHOD);
        map.put("version", version);
        map.put("data", jsonDataBase64);
        map.put("account", account);
        map.put("timestamp", DateUtils.convertToString(new Date(), DateUtils.ISO_DATETIME_NO_T_FORMAT));
        
        if (logger.isDebugEnabled()){
            logger.debug("url=[" + url + "], " +
                            "method=[" + map.get("method") + "], " +
                            "version=[" + map.get("version") + "], " +
                            "jsondata=[" + jsondata + "], " +
                            "jsonDataBase64=["+ map.get("data") + "], " +
                            "timestamp=["+ map.get("timestamp") + "], " + 
                            "account=[" + map.get("account") + "]");
        }
        long startTime = System.currentTimeMillis();
        String result = HttpClientHelper.post(url, map);
        long endTime = System.currentTimeMillis();
        logger.info("调用OMS(新)系统接口耗时:" + (endTime - startTime) + "毫秒");
        return result;
    }

 

package com.yundaex.common.bidiboxing.helper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
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.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;


public class HttpClientHelper {
    
    public static final String CHARSET_ENCODE = "UTF-8";
    
    public static String post(String url, String data) throws Exception {
        RequestConfig config = RequestConfig.custom().setConnectTimeout(60000).setSocketTimeout(30000).build();
        CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();

        HttpPost hp = new HttpPost(url);
        HttpEntity responseEntity = null;
        CloseableHttpResponse response = null;
        int status = 0;
        try {
            StringEntity entity = new StringEntity(data, CHARSET_ENCODE);// 解决中文乱码问题
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");
            hp.setEntity(entity);

            response = httpClient.execute(hp);

            status = (response == null ) ? 0 :response.getStatusLine().getStatusCode();
            if (status == 200) {
                responseEntity = response.getEntity();
                String responseString = EntityUtils.toString(responseEntity, CHARSET_ENCODE);
                return responseString;
            } else {
                hp.abort();
                throw new YDInterfaceAccessException("响应编码为:" + status);
            }

        } finally {
            if (null != responseEntity) {
                EntityUtils.consumeQuietly(responseEntity);
            }
            if (null != response) {
                try {
                    response.close();
                } catch (IOException e) {
                }
            }
            try {
                httpClient.close();
            } catch (IOException e) {
            }
        }
    }
    
    public String post(String url, Map<String, Object> data) throws Exception{
        RequestConfig config = RequestConfig.custom().setConnectTimeout(60000).setSocketTimeout(30000).build();
        CloseableHttpClient   httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
        
        HttpPost hp = new HttpPost(url);
        HttpEntity responseEntity = null;
        CloseableHttpResponse response = null;
        int status = 0;
        
        List<NameValuePair> pairs = new ArrayList<NameValuePair>();
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            Object value = entry.getValue();
            if(value != null){
                pairs.add(new BasicNameValuePair(entry.getKey(), value.toString()));
            }
        }
        
        try {
            UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(pairs, CHARSET_ENCODE);
            hp.setEntity(uefEntity);

            response = httpClient.execute(hp);
            
            status = (response == null ) ? 0 :response.getStatusLine().getStatusCode();
            if (status == 200) {
                responseEntity = response.getEntity();
                String responseString = EntityUtils.toString(responseEntity, CHARSET_ENCODE);
                return responseString;
            } else {
                hp.abort();
                throw new YDInterfaceAccessException("响应编码为:" + status);
            }

        } finally {
            if (null != responseEntity) {
                EntityUtils.consumeQuietly(responseEntity);
            }
            if (null != response) {
                try {
                    response.close();
                } catch (IOException e) {
                }
            }
            try {
                httpClient.close();
            } catch (IOException e) {
            }
        }
    }
}
public class HttpClientHelperTest {
public static void main(String[] args) {
    String url = "http://10.19.105.184:7001/wms/systemConfig/query.do";
    String data  = 
           "{"+
 " \"data\":{ "+
 "   \"criterias\":{ "+
 "  \"sysKey\":\"IFM_SWITCH\" "+
 "   } "+
 " }, "+
 " \"username\":\"wms_test\", "+
 " \"format\":\"json\", "+
 " \"dateTime\":\"2021426925\", "+
 " \"validation\":\"bfbb29498ae850d16d4bea5eb3469048\" "+
 " }" 
            ;
    String responseString ="";
    try {
        responseString = HttpClientHelper.post(url, data);
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println(responseString);
}
}

 

转载于:https://www.cnblogs.com/tonggc1668/p/7472457.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值