asp.net 取得邮政Ems物流数据

浪淘沙10个月前后端144

一、引用dll

BouncyCastle.Crypto.rar


二、SM4加密

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Text;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;

namespace EC
{
    /// <summary>
    /// Sm4Util 的摘要说明
    /// </summary>
    public class Sm4Util
    {
        /// <summary>
        /// 使用 SM4 ECB 模式加密并返回 |$4|Base64 编码字符串
        /// </summary>
        public static string EncryptSm4ToLogitcsInterface(string jsonPayload, string base64Key)
        {
            // 解码 Base64 密钥
            byte[] keyBytes = Convert.FromBase64String(base64Key);

            // 拼接内容:json + key
            string contentToEncrypt = jsonPayload + base64Key;
            byte[] contentBytes = Encoding.UTF8.GetBytes(contentToEncrypt);

            // 初始化 SM4 引擎
            var engine = new SM4Engine(); // ECB mode by default
            engine.Init(true, new KeyParameter(keyBytes)); // true for encryption

            // 填充数据为 16 字节倍数(PKCS#7)
            contentBytes = AddPadding(contentBytes, 16);

            // 加密数据
            byte[] encrypted = new byte[contentBytes.Length];
            int blockSize = engine.GetBlockSize();

            for (int i = 0; i < contentBytes.Length; i += blockSize)
            {
                engine.ProcessBlock(contentBytes, i, encrypted, i);
            }

            // Base64 编码 + 前缀
            string base64 = Convert.ToBase64String(encrypted);
            return "|$4|" + base64;
        }

        private static byte[] AddPadding(byte[] data, int blockSize)
        {
            int padding = blockSize - (data.Length % blockSize);
            byte[] padded = new byte[data.Length + padding];
            Array.Copy(data, padded, data.Length);
            for (int i = data.Length; i < padded.Length; i++)
            {
                padded[i] = (byte)padding;
            }
            return padded;
        }
    }
}


三、发送api请求接口

using System.Collections.Specialized;
using System.Net;
 string url = "https://api.ems.com.cn/amp-prod-api/f/amp/api/open";
        string apiCode = "040001";
        string senderNo = "生产协议客户号";
        string authorization = "生产授权码";
        string timeStamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

        string bw = "{\"waybillNo\": \"1027222554236\",\"direction\":\"0\"}";
        string key = "生产签名钥匙";

        string logitcsInterface = Sm4Util.EncryptSm4ToLogitcsInterface(bw, key);
      
        // 创建 WebClient 实例
        using (var client = new WebClient())
        {
            // 设置请求的 Content-Type 头
            client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";

            // 构建请求参数
            var requestParameters = new NameValueCollection
            {
                {"apiCode", apiCode},
                {"senderNo", senderNo},
                {"authorization", authorization},
                {"timeStamp", timeStamp},
                {"logitcsInterface", logitcsInterface}

            };



            // 发送 POST 请求并获取响应
            byte[] responseBytes = client.UploadValues(url, "POST", requestParameters);
            string str = System.Text.Encoding.UTF8.GetString(responseBytes);

          
        }


相关文章

asp.net core rzaor page 中的ViewComponent 视图用法

asp.net core rzaor page 中的ViewComponent 视图用法

1、/Pages/Components 新建:MyViewComponent.csusing Microsoft.AspNetCore.Mvc; namespace xxts...

oss文件上传

using Aliyun.OSS; #region Oss文件上传  /// <summary>  /// O...

asp.net core razor .cshtml.cs 如何改变写入中文后变成ANSI格式为utf-8

根目录下,添加.editorconfig并写入[*.cshtml.cs] charset = utf-8已生成的文件另存为 utf-8格式...

asp.net core 中文 url 编码

 return Redirect("tj_send?fst=" + fst + "&rq1=" + rq1 + "&rq2=...

asp.net core url编码

 HttpUtility.UrlEncode("https://fdy.eduw.cn/zf/index")...

asp.net core [BindProperty] 用法

在 ASP.NET Core 中,[BindProperty] 是一种用于绑定模型属性的特性,通常与 Razor 页面配合使用。它会自动将请求中的表单数据绑定到页面模型的属性上,简化了手动从 Requ...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。