服务端访问http接口

        System.out.println(OrderUtils.md5("abc"));
        Req req = new Req();
        req.setBeginTime("2019-05-16");
        req.setEndTime("2019-05-31");
//        req.setOrderCode(new ArrayList<>());
//        req.getOrderCode().add("AO20190516058531");
        req.setSkuId(new ArrayList<>());
        req.getSkuId().add("32285_19_0_0_0_9");
        ObjectMapper mapper = new ObjectMapper();
        String paramJson = mapper.writeValueAsString(req);

        String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis());
        SortedMap<String, String> ps = new TreeMap();
        ps.put("appKey", "ee14731da4d043d4a371c7ba61dc5312");
        ps.put("token", "90ec1a5e85494691bb93c1160aa045d1");
        ps.put("timestamp", timeStamp);
        ps.put("paramJson", paramJson);
        StringBuilder source = new StringBuilder();
        for (Map.Entry<String, String> kv : ps.entrySet()) {
            source.append(kv.getKey()).append('=').append(kv.getValue()).append('&');
        }
        source.deleteCharAt(source.length() - 1);
        String sign = OrderUtils.md5(source.toString());
        ps.put("sign", sign);
        ps.remove("token");

        System.out.println(paramJson);
        String url = "http://api2.mai.yiche.com/api/Clue/GetISPProjectData";
//        HttpClient client = new DefaultHttpClient();
        //Http
//        String result = HttpUtil.doPost("http://www.baodu.com", "");
        String result = OrderUtils.sendHttpClientPost(url, ps, "utf-8");
        System.out.println(result);

 

public class Req {
    private String beginTime;
    private String endTime;
    private List<String> orderCode;
    private List<String> skuId;
}
    public static String md5(String plainText) {
        //定义一个字节数组
        byte[] secretBytes = null;
        try {
            // 生成一个MD5加密计算摘要
            MessageDigest md = MessageDigest.getInstance("MD5");
            //对字符串进行加密
            md.update(plainText.getBytes());
            //获得加密后的数据
            secretBytes = md.digest();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("没有md5这个算法!");
        }
        //将加密后的数据转换为16进制数字
        String md5code = new BigInteger(1, secretBytes).toString(16);
        // 16进制数字
        // 如果生成数字未满32位,需要前面补0
        for (int i = 0; i < 32 - md5code.length(); i++) {
            md5code = "0" + md5code;
        }
        return md5code;
    }

 

    public static String sendHttpClientPost(String path, Map<String, String> map,
                                            String encode) {
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        if (map != null && !map.isEmpty()) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                // 解析Map传递的参数,使用一个键值对对象BasicNameValuePair保存。
                list.add(new BasicNameValuePair(entry.getKey(), entry
                        .getValue()));
            }
        }
        try {
            // 实现将请求 的参数封装封装到HttpEntity中。
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, encode);
            // 使用HttpPost请求方式
            HttpPost httpPost = new HttpPost(path);
            // 设置请求参数到Form中。
            httpPost.setEntity(entity);
            // 实例化一个默认的Http客户端
            DefaultHttpClient client = new DefaultHttpClient();
            // 执行请求,并获得响应数据
            HttpResponse httpResponse = client.execute(httpPost);
            // 判断是否请求成功,为200时表示成功,其他均问有问题。
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                // 通过HttpEntity获得响应流
                InputStream inputStream = httpResponse.getEntity().getContent();
                return changeInputStream(inputStream, encode);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

 

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.3.3</version>
        </dependency>

        <!--截屏服务-->
        <dependency>
            <groupId>com.yiche.psc.screen</groupId>
            <artifactId>screen-thrift</artifactId>
            <version>0.1.3-RELEASE</version>
        </dependency>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值