//-----------------------------------
nonce 参数a(4)
public static String a(int i) {
Random random = new Random();
StringBuffer stringBuffer = new StringBuffer();
int length = "abcdefghijklmnopqrstuvwxyz0123456789".length();
for (int i2 = 0; i2 < i; i2++) {
stringBuffer.append("abcdefghijklmnopqrstuvwxyz0123456789".charAt(random.nextInt(length)));
}
return stringBuffer.toString();
}
//-----------------------------------
//-----------------------------------
b参数是提交的数据json 键值取出来拼接tostring
private String b() {
if (this.g == null || this.g.isEmpty()) {
return "";
}
JsonObject jsonObject = new JsonObject();
for (Entry entry : this.g.entrySet()) {
jsonObject.addProperty(entry.getKey().toString(), entry.getValue().toString());
}
return jsonObject.toString();
}
//-----------------------------------
signature参数
String a2 = j.a(时间戳timespan, nonce参数, devid序列号, 提交参数取出键值拼接而成, true);
下面两个类是signature算法 j.a(...a)
public class j {
public static String a(String str, String str2, String str3, String str4, boolean z) {
String[] a;
int i = 0;
if (z) {
a = a(str, str2, str3);
} else {
a = new String[]{str, str2, str3};
}
StringBuffer stringBuffer = new StringBuffer();
int length = a.length;
while (i < length) {
stringBuffer.append(a[i]);
i++;
}
return d.a(stringBuffer.toString());
}
private static String[] a(String... strArr) {
Arrays.sort(strArr);
return strArr;
}
}
public class d {
public static String a(String str) {
if (l.a(str)) {
return str;
}
try {
return a(MessageDigest.getInstance("MD5").digest(str.getBytes()));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
private static String a(byte[] bArr) {
if (bArr == null || bArr.length == 0) {
return "";
}
StringBuffer stringBuffer = new StringBuffer();
for (byte b : bArr) {
int i = b & 255;
if (i < 16) {
stringBuffer.append("0");
}
stringBuffer.append(Integer.toHexString(i));
}
return stringBuffer.toString();
}
}
//-----------------------------------
httpURLConnection3.setRequestProperty("timespan", str);
httpURLConnection3.setRequestProperty("nonce", a);
httpURLConnection3.setRequestProperty("devid", c);
httpURLConnection3.setRequestProperty("signature", a2);
转载于:https://my.oschina.net/KFS/blog/1815394