token加密方法
<p><strong>User-Agent请务必模拟正常浏览器!
</strong>
<strong>User-Agent请务必模拟正常浏览器!
</strong>
<strong>User-Agent请务必模拟正常浏览器!
</strong></p>
<p><strong>C#</strong></p>
<pre><code>using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace Test
{
class Program
{
const string appSecret = "APPSECRET"; //这里输入提供的app_secret
const string appKey = "APPKEY"; //这里输入提供的app_key
const string image_url = "图片url"; // 如有必要
public static string CalculateMD5Hash(string input)
{
// step 1, calculate MD5 hash from input
MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hash = md5.ComputeHash(inputBytes);
// step 2, convert byte array to hex string
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("X2"));
}
return sb.ToString().ToLower();
}
public static double ConvertToUnixTimestamp(DateTime date)
{
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
TimeSpan diff = date.ToUniversalTime() - origin;
return Math.Floor(diff.TotalSeconds);
}
static void Main(string[] args)
{
double timeStamp = ConvertToUnixTimestamp(DateTime.Now);
string companyName = ""; // 需要查询的公司抬头名称
string token = CalculateMD5Hash(appKey + '+' + timeStamp + '+' + appSecret);
Dictionary<string, object> dic = new Dictionary<string, object>();
Dictionary<string, string> param = new Dictionary<string, string>(); // 业务参数
param.Add("companyName", companyName);
dic.Add("app_key", appKey);
dic.Add("timestamp", timeStamp.ToString());
dic.Add("token", token);
dic.Add("param", param);
string result = "";
string url = "url"; // 输入对应的接口地址
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36";
#region 添加Post 参数
string builder = JsonConvert.SerializeObject(dic);
byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
req.ContentLength = data.Length;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(data, 0, data.Length);
reqStream.Close();
}
#endregion
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream stream = resp.GetResponseStream();
//获取响应内容
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
result = reader.ReadToEnd();
}
Console.WriteLine(result);
Console.ReadLine();
}
}
}</code></pre>
<p><strong>JAVA</strong></p>
<pre><code>
import org.json.JSONObject;
import java.io.*;
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class test {
public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
String companyName = "companyName"; // 查询的公司抬头
String appKey = "appKey"; // 输入app_key
String appSecret = "appSecret"; // 输入app_secret
String url = "url"; // 输入对应的接口地址
long timestamp = System.currentTimeMillis() / 1000;
// 生成一个MD5加密计算摘要
MessageDigest m = MessageDigest.getInstance("MD5");
m.update((appKey + "+" + timestamp + "+" + appSecret).getBytes("UTF8"));
byte s[] = m.digest();
String token = "";
for (int i = 0; i < s.length; i++) {
token += Integer.toHexString((0x000000FF & s[i]) | 0xFFFFFF00).substring(6);
}
JSONObject param = new JSONObject(); // 业务参数
param.put("companyName",companyName);
JSONObject object = new JSONObject();
object.put("app_key",appKey);
object.put("timestamp",timestamp + "");
object.put("token",token);
object.put("param",param);
URL postUlr = new URL(url);
HttpURLConnection con = (HttpURLConnection) postUlr.openConnection();
// 连接主机超时 毫秒
con.setConnectTimeout(120 * 1000);
// 从主机读取数据超时毫秒
con.setReadTimeout(120 * 1000);
// 设置从HttpURLConnection读入
con.setDoInput(true);
// 设置是否向httpUrlConnection输出, 因为这个是post请求, 参数要放在http正文内, 因此需要设为true, 默认情况下是false
con.setDoOutput(true);
// 设置该URLConnection的allowUserInteraction请求头字段的值
con.setAllowUserInteraction(false);
con.setUseCaches(true);
con.setRequestMethod("POST");
// con.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
con.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36");
// 此处getOutputStream会隐含的进行connect,所以在开发中不调用上述的connect()也可以
BufferedWriter bout = new BufferedWriter(new OutputStreamWriter(con.getOutputStream()));
bout.write(object.toString());
bout.flush();
bout.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//打印结果
System.out.println(response.toString());
}
}
```欢迎使用ShowDoc!</code></pre>