Foxtable(狐表)用户栏目专家坐堂 → 关于第三方接口的写法问题


  共有2776人关注过本帖树形打印复制链接

主题:关于第三方接口的写法问题

帅哥哟,离线,有人找我吗?
ap9709130
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:六尾狐 帖子:1470 积分:11456 威望:0 精华:0 注册:2013/11/24 22:10:00
关于第三方接口的写法问题  发帖心情 Post By:2020/7/2 13:49:00 [显示全部帖子]

 老师

     我正在弄快宝开放平台的接口,C# 的开发文档如下:

 
class Request {
	private
				const String host = "https://kop.kuaidihelp.com";
	private
				const String path = "/api";
	private
				const String requestMethod = "POST";
	static
				void Main(string[] args)
	{
		String querys = "";
		String appId = "50001";
		String method = "account.waybill.get";
		String ts = GetTimeStamp() + "";
		String appKey = "bdf3b5f50865ac813cbdfd6c9b572b79";
		
		// 计算签名
		String signStr = appId + method + ts + appKey;
		String sign = GetMd5(signStr, 32);
		
		String bodys = "app_id=" + appId + "&method=" + method + "&ts=" + ts + "&sign=" + sign;
		
		// data参数是个json格式字符串 建议使用函数或方法生成json字符串
		bodys = bodys + "&data={
    "customer_name":"kuaibao888",
    "customer_password":"1234567890",
    "order_id":"KB101100111011232",
    "trade_name":"智能手机",
    "shipper_type":"sto",
    "pay_type":"1",
    "weight":"1.23",
    "sender":{
        "company":"南山区深圳软件产业基地",
        "name":"张飞鸿",
        "tel":"",
        "mobile":"18688888888",
        "province":"广东省",
        "city":"深圳市",
        "district":"南山区",
        "address":"深圳软件产业基地"
    },
    "recipient":{
        "company":"宝芝林贸易",
        "name":"王三姨",
        "tel":"95127777",
        "mobile":"13666666666",
        "province":"江苏省",
        "city":"苏州市",
        "district":"沧浪区",
        "address":"人民路沧浪亭街31号宝芝林贸易有限公司"
    }
}";
		String url = host + path;
		HttpWebRequest httpRequest = null;
		HttpWebResponse httpResponse = null;
		if (0 < querys.Length)
		{
			url = url + "?" + querys;
		}

		if (host.Contains("https://"))
		{
			ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
			httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
		}
		else
		{
			httpRequest = (HttpWebRequest)WebRequest.Create(url);
		}
		httpRequest.Method = requestMethod;

		//根据API的要求,定义相对应的Content-Type
		httpRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
		if (0 < bodys.Length)
		{
			byte[] data = Encoding.UTF8.GetBytes(bodys);
			using (Stream stream = httpRequest.GetRequestStream())
			{
				stream.Write(data, 0, data.Length);
			}
		}
		try
		{
			httpResponse = (HttpWebResponse)httpRequest.GetResponse();
		}
		catch (WebException ex)
		{
			httpResponse = (HttpWebResponse)ex.Response;
		}

		Console.WriteLine(httpResponse.StatusCode);
		Console.WriteLine(httpResponse.Method);
		Console.WriteLine(httpResponse.Headers);
		Stream st = httpResponse.GetResponseStream();
		StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
		Console.WriteLine(reader.ReadToEnd());
		Console.WriteLine("\n");

	}

	// 计算md5值
				public
				static
				string GetMd5(string md5str, int type)
	{
		if (type == 16)
		{
			MD5 algorithm = MD5.Create();
			byte[] data = algorithm.ComputeHash(Encoding.UTF8.GetBytes(md5str));
			string sh1 = "";
			for (int i = 0; i < data.Length; i++)
			{
				sh1 += data[i].ToString("x2").ToUpperInvariant();
			}
			return sh1.Substring(8, 16).ToLower();
		}
		else
				if (type == 32)
		{
			MD5 algorithm = MD5.Create();
			byte[] data = algorithm.ComputeHash(Encoding.UTF8.GetBytes(md5str));
			string sh1 = "";
			for (int i = 0; i < data.Length; i++)
			{
				sh1 += data[i].ToString("x2").ToUpperInvariant();
			}
			return sh1.ToLower();
		}
		return
				"";
	}

	// 获取当前时间戳
				public
				static
				string GetTimeStamp()
	{
		TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
		return Convert.ToInt64(ts.TotalSeconds).ToString();
	}		

	public
				static
				bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
	{
		return
				true;
	}
}
		
		
		

 回到顶部
帅哥哟,离线,有人找我吗?
ap9709130
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:六尾狐 帖子:1470 积分:11456 威望:0 精华:0 注册:2013/11/24 22:10:00
  发帖心情 Post By:2020/7/2 13:57:00 [显示全部帖子]

 我根据这个 变成 foxtalbe 的代码如下:

  代码请看附件,老是说代码有非法字符,不能上传.
 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:接口代码.txt



得到的结果是:{"code":1005,"msg":"请求时间戳有误,请检查系统时间","data":{},"uid":"1c1bdbd6f4156f140214c9d51e3f2f4900000000"}

请问老师,代码要怎么改才能得到正确的结果.

 回到顶部
帅哥哟,离线,有人找我吗?
ap9709130
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:六尾狐 帖子:1470 积分:11456 威望:0 精华:0 注册:2013/11/24 22:10:00
  发帖心情 Post By:2020/7/2 14:06:00 [显示全部帖子]

 老师

      找到原因了, 时间截要13位的. 多谢!

 回到顶部