test.java_调用接口, Test.java(不用任何包, MD5大写32位加密,HttpURLConnection)

import java.io.BufferedInputStream;

import java.io.ByteArrayOutputStream;

import java.io.OutputStream;

import java.net.HttpURLConnection;

import java.net.URL;

import java.net.URLEncoder;

import java.security.MessageDigest;

public class Test {

public static void main(String[] args) throws Exception {

String cpOrderId = String.valueOf(System.currentTimeMillis());

String cpId = "1143";

String goodsCode = "8782842469";

int orderType = 0;

String mobile = "18370017024";

String timestamp = String.valueOf(System.currentTimeMillis() / 1000);

int sourceType = 2;

int channelCode = 1;

String scope = "test";

String sign = md5("channelCode=1&cpId=1143&cpOrderId=" + cpOrderId

+ "&goodsCode=8782842469&mobile=18370017024&orderType=0&sourceType=2&timestamp=" + timestamp

+ "&key=be54c40e601cea5c74414382218ff81d");

StringBuilder body = new StringBuilder().append("{").append("\"cpOrderId\":\"" + cpOrderId + "\",")

.append("\"cpId\":\"" + cpId + "\",").append("\"goodsCode\":\"" + goodsCode + "\",")

.append("\"orderType\":" + orderType + ",").append("\"mobile\":\"" + mobile + "\",")

.      append("\"timestamp\":\"" + timestamp + "\",").append("\"sourceType\":\"" + sourceType + "\",")

.append("\"channelCode\":\"" + channelCode + "\",").append("\"scope\":\"" + scope + "\",")

.append("\"sign\":\"" + sign + "\"").append("}");

String result = post("http://220.194.53.168:7020/api/b2b/sendVerificationCode",

"data=" + URLEncoder.encode(body.toString(), "UTF-8"), "application/x-www-form-urlencoded");

System.out.println(result);

}

public static String md5(String s) throws Exception {

MessageDigest md = MessageDigest.getInstance("MD5");

byte[] bytes = md.digest(s.getBytes("UTF-8"));

final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();

StringBuilder ret = new StringBuilder(bytes.length * 2);

for (int i = 0; i < bytes.length; i++) {

ret.append(HEX_DIGITS[(bytes[i] >> 4) & 0x0f]);

ret.append(HEX_DIGITS[bytes[i] & 0x0f]);

}

return ret.toString();

}

public static String post(String url, String body, String contentType) throws Exception {

HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();

conn.setRequestMethod("POST");

conn.setRequestProperty("Content-Type", contentType);

conn.setDoOutput(true);

OutputStream out = conn.getOutputStream();

out.write(body.getBytes("UTF-8"));

out.flush();

BufferedInputStream in = new BufferedInputStream(conn.getInputStream());

byte[] bs = new byte[10240];

int count;

ByteArrayOutputStream baos = new ByteArrayOutputStream();

while (-1 != (count = in.read(bs))) {

baos.write(bs, 0, count);

}

return new String(baos.toByteArray());

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值