c# 微信商户转零钱 金额大于2000 遇到的问题

金额大于2000,提示缺少 Headers 中的 Wechatpay-Serial

这个,我们需要访问一下微信的接口,来获取到这个序列号 直接上代码

cs 复制代码
 public static string GetWechatpay_Serial()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.mch.weixin.qq.com/v3/certificates");
            request.Method = "GET";
            request.ContentType = "application/json;charset=UTF-8";
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36";
            request.Accept = "application/json";
            string Authorization = GetAuthorization("https://api.mch.weixin.qq.com/v3/certificates", "GET", "", mchid, serialNo);
            request.Headers.Add("Authorization", Authorization);
            //request.Headers.Add("User-Agent", "https://zh.wikipedia.org/wiki/User_agent");
            //request.Headers.Add("Accept", "application/json");

            string reslut = "";
            HttpWebResponse wbResponse = (HttpWebResponse)request.GetResponse();
            using (Stream responseStream = wbResponse.GetResponseStream())
            {
                using (StreamReader sReader = new StreamReader(responseStream))
                {
                    reslut = sReader.ReadToEnd();
                }
            }
            return reslut;

        }

这里面的 商户id 和商户的序列号,自己替换哈

获取内容后,返回的内容中,会有一个序列号,

接上篇文章中的 https://blog.csdn.net/Little_Code/article/details/130243211 WxV3PostJson,添加这个代码

cs 复制代码
 request.Headers.Add("Wechatpay-Serial", "刚刚的序列号");

这个时候,会提示,需要某些信息加密

然后就用到了这个代码

cs 复制代码
private string ALGORITHM = "AES/GCM/NoPadding";
        private  int TAG_LENGTH_BIT = 128;
        private  int NONCE_LENGTH_BYTE = 12;
        //APIV3  密钥   
        private  string AES_KEY = "APIV3密钥";//我这边用的V3,如果你是V2可能就是V2的密钥

        public  string AesGcmDecrypt(string associatedData, string nonce, string ciphertext)
        {
            GcmBlockCipher gcmBlockCipher = new GcmBlockCipher(new AesEngine());
            AeadParameters aeadParameters = new AeadParameters(
                new KeyParameter(Encoding.UTF8.GetBytes(AES_KEY)),
                128,
                Encoding.UTF8.GetBytes(nonce),
                Encoding.UTF8.GetBytes(associatedData));
            gcmBlockCipher.Init(false, aeadParameters);
            byte[] data = Convert.FromBase64String(ciphertext);
            byte[] plaintext = new byte[gcmBlockCipher.GetOutputSize(data.Length)];
            int length = gcmBlockCipher.ProcessBytes(data, 0, data.Length, plaintext, 0);
            gcmBlockCipher.DoFinal(plaintext, length);
            return Encoding.UTF8.GetString(plaintext);
        }

这个方法中的 参数,就是GetWechatpay_Serial(),这个方法返回的数据,然后放进去进行解密,解密得到的就是这样一串 ------BEGIN CEFT...这样一串公钥,拿着这个公钥,对用户的名称等信息进行加密

cs 复制代码
public static string RSAEncrypt(string text, byte[] publicKey)
        {
            using (var x509 = new X509Certificate2(publicKey))
            {
                using (var rsa = (RSACryptoServiceProvider)x509.PublicKey.Key)
                {
                    var buff = rsa.Encrypt(Encoding.UTF8.GetBytes(text), true);

                    return Convert.ToBase64String(buff);
                }
            }
        }

最后 使用这个方法:

cs 复制代码
 string jiami = RSAEncrypt(user_name, System.Text.Encoding.UTF8.GetBytes(publicKey.Replace("-----BEGIN CERTIFICATE-----", "").Replace("-----END CERTIFICATE-----", "").Replace("\n", "")));
            //publickKey.Replace("-----BEGIN CERTIFICATE-----", "") .Replace("-----END CERTIFICATE-----", "").Replace("\n", "")
            dic1.Add("user_name", jiami);

这样,就成功发出申请了

相关推荐
江沉晚呤时2 小时前
C# 实现雪花算法(Snowflake Algorithm)详解与应用
c#·.net
酷炫码神4 小时前
C#语法基础
开发语言·c#
酷炫码神6 小时前
C#数据类型
java·服务器·c#
CodeCraft Studio7 小时前
国产化Word处理控件Spire.Doc教程:通过C# 删除 Word 文档中的超链接
开发语言·c#·word
ghost14310 小时前
C#学习第22天:网络编程
开发语言·学习·c#
神仙别闹11 小时前
基于C#实现中央定位服务器的 P2P 网络聊天系统
服务器·网络·c#
阿蒙Amon12 小时前
DevExpress&WinForms-TreeList-数据绑定
c#·devexpress·winforms
bicijinlian14 小时前
.Net HttpClient 使用代理功能
c#·.net·httpclient·.net httpclient·httpclient 代理
敲代码的 蜡笔小新18 小时前
【行为型之中介者模式】游戏开发实战——Unity复杂系统协调与通信架构的核心秘诀
unity·设计模式·c#·中介者模式
程序猿多布19 小时前
使用Visual Studio将C#程序发布为.exe文件
c#·visual studio