评论

自定义交易组件申请过程记录

由于客户需要 把视频号和小程序打通,在查看了相关文档后感觉小程序的自定义交易组件貌似可以完成相关操作,下面就把开通过程及相关问题记录下来,以供参考

自定义交易组件上传图片接口

protected void Button1_Click(object sender, EventArgs e)
        {
            //https://api.weixin.qq.com/shop/img/upload?access_token= //上传图片
           //string accesstoken = AccessTokenContainer.GetAccessToken(ConfigurationManager.AppSettings["WeChat:Appid"].ToString());
            string accesstoken ="获取到的accesstoken";
            Label1.Text = accesstoken;
            string url = "https://api.weixin.qq.com/shop/img/upload?access_token= " + accesstoken;
            Dictionary postDictionary = new Dictionary();
            postDictionary.Add("resp_type", "1");
            postDictionary.Add("upload_type", "0");
            var filepath = HttpContext.Current.Server.MapPath("/images/"+ TextBox1.Text);
            TextArea1.Text = GongYongClass.PostFile(url, postDictionary, filepath, "media");
            TextArea2.Text = filepath;
        }

public static string PostFile(string getUrl, Dictionary postDictionary, string filePath, string fileFormDataName)
        {
        
            string fileName = Path.GetFileName(filePath);
            //Stream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls;//https形式需要添加
            string sReturnString = string.Empty;
            //fileStream.Position = 0;
            string Enter = "\r\n";
            string strBoundary = "--" + DateTime.Now.Ticks.ToString("x");
            byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "--\r\n");
            //请求头部信息
            StringBuilder sb = new StringBuilder();

            //添加参数信息
            foreach (var item in postDictionary)
            {
               sb.Append("--"+ strBoundary + Enter + "Content-Disposition: form-data; name=\"" + item.Key + "\""+ Enter+ "Content-Type: text/plain" + Enter + Enter+ item.Value+ Enter);
            }
            //添加文件信息
            sb.Append("--"+ strBoundary + Enter+ "Content-Disposition: form-data; name=\"" + fileFormDataName + "\"; filename=\"" + fileName + "\""+ Enter+ "Content-Type:application/octet-stream" + Enter + Enter);
            byte[] sbbyte = Encoding.UTF8.GetBytes(sb.ToString());//将参数信息+文件头信息转换 二进制编码
            byte[] buffer = File.ReadAllBytes(filePath);  //文件二进制编码
            //合并信息到一起
            byte[] postHeaderBytes= new byte[sbbyte.Length + buffer.Length];
            sbbyte.CopyTo(postHeaderBytes, 0);
            buffer.CopyTo(postHeaderBytes, sbbyte.Length); 
            try
            {
                // 根据uri创建HttpWebRequest对象
                HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(new Uri(getUrl));
                httpReq.Method = "POST";
                //对发送的数据不使用缓存
                httpReq.AllowWriteStreamBuffering = false;
                //设置获得响应的超时时间(300秒)
                httpReq.Timeout = 300000;
                //httpReq.ServicePoint.Expect100Continue = false;
                //httpReq.CookieContainer = cookieContainer;
                //httpReq.Accept = header["accept"].ToString();
                //httpReq.Headers.Add("Accept-Encoding","gzip, deflate, br");
                //httpReq.Headers.Add("TE", "Trailers");
                // httpReq.UserAgent = header["userAgent"];
                httpReq.ContentType = "multipart/form-data; boundary=" + strBoundary;
             
                long length =  postHeaderBytes.Length + boundaryBytes.Length;
                httpReq.ContentLength = length;
                Stream postStream = httpReq.GetRequestStream();
                //发送请求消息
                postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
                //添加尾部的时间戳
                postStream.Write(boundaryBytes, 0, boundaryBytes.Length);
                postStream.Close();
                //获取服务器端的响应
                HttpWebResponse webRespon = (HttpWebResponse)httpReq.GetResponse();
                if (webRespon.StatusCode == HttpStatusCode.OK) //如果服务器未响应,那么继续等待相应                 
                {
                    Stream s = webRespon.GetResponseStream();
                    StreamReader sr = new StreamReader(s);
                    //读取服务器端返回的消息
                    sReturnString = sr.ReadLine();
                    s.Close();
                    sr.Close();
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            return sReturnString;
        }


未完待补充


最后一次编辑于  2022-10-25  
点赞 0
收藏
评论
登录 后发表内容