简单弄个1号药城请求的调用,demo如下(转载请标明出处):
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import caichufang.tool.TOOL;
public class YCUtil {
/**
* md5 签名
*
* @param params 传给服务器的参数
*
* @param secret* 分配给您的 APP_SECRET
*/
public static String md5Signature(Map<String, String> params, String secret) {
String result = null;
StringBuffer orgin = getBeforeSign(params, new StringBuffer(secret));
if (orgin == null)
return result;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
result = byte2hex(md.digest(orgin.toString().getBytes("utf-8")));
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 二进制转字符串
*/
private static String byte2hex(byte[] b) {
StringBuffer hs = new StringBuffer();
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs.append("0").append(stmp);
else
hs.append(stmp);
}
return hs.toString();
}
/**
* 添加参数的封装方法
*
* @param params
* @param orgin
* @return
*/
public static StringBuffer getBeforeSign(Map<String, String> params, StringBuffer orgin) {
if (params == null)
return null;
Object[] key = params.keySet().toArray();
Arrays.sort(key);
for (int i = 0; i < key.length; i++) {
orgin.append(key[i]).append(params.get(key[i]));
}
return orgin;
}
/**
* 签名制作
*/
public static String sig(String method,String timestamp) {
Map<String, String> systemRequestMap = new HashMap<String, String>();
systemRequestMap.put("method", method);
systemRequestMap.put("version", "1.0");
systemRequestMap.put("timestamp", timestamp);
systemRequestMap.put("format", "json");
systemRequestMap.put("partnerid", Ev.yaochengpartnerid);
return md5Signature(systemRequestMap, Ev.yaochengsecret);
}
/**
* 请求封装
*/
public static boolean post(String url,JSONArray pam,String method) {
if(pam.size()>0) {
// 200个进行分组
int ss=200;
int i=pam.size();
int num =i/ss;
if(i%ss>0) {
num++;
}
for(int a=0;a<num;a++) {
JSONArray aa = new JSONArray();
for(int b=a*ss;b<(a+1)*ss;b++) {
if(b<i) {
aa.add(pam.get(b));
// System.out.println(b);
}
}
// [{"sellerCode ":"1","produceCode":"1","bigPackage":1,"minOrder":1},
// {"sellerCode ":"2","produceCode":"2","bigPackage":2,"minOrder":2}]
String timestam=new Date().getTime()+"";
String res=TOOL.postc(url,aa.toString(),method,timestam);
// aa.toString()
TOOL.log(aa.toString());
JSONObject o=JSONObject.fromObject(res);
if(o.getInt("code")==1) {
TOOL.log("请求成功"+method+":"+res);
}
else {
TOOL.log("报错:"+res+",参数:"+pam.toString());
return false;
}
}
return true;
}
return false;
}
/**
* 调用示例
*
* @param args
*/
public static void main(String[] args) {
JSONArray arr = new JSONArray();
// [{“sellerCode”:”1”,“produceCode”,1“producePrice”9.9 “priceType”:”1”}]
JSONObject o = new JSONObject();
o.put("sellerCode", Ev.yaochengpartnerid);// 店铺编码
o.put("produceCode", "goodsno");// 商品编码
o.put("producePrice", "price");// 商品价格
o.put("priceType", "3");// 价格类型1、公开价 2、客户组价 3、公开价、客户组价都修改
arr.add(o);
boolean res = YCUtil.post("http://ddx.yaoex.com/yhyc/price/update", arr,"yhyc.price.update");
//****换成自己的对应的参数
}
}