腾讯返回json数据转换

 腾讯返回json数据转换

 

#region----远程获取微信小程序二维码的流----
        /// <summary>
        /// 远程获取微信小程序二维码的流
        /// </summary>
        /// <param name="urlGet">请求小程序的地址</param>
        /// <param name="paramJson">请求小程序的参数</param>
        /// <returns>返回流</returns>
        public async static Task<Stream> HttpResponseStreamAsync(string urlGet, string paramJson)
        {
            try
            {
                //发起请求
                HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(urlGet);
                wbRequest.Method = "POST";
                wbRequest.ContentType = "application/json;charset=UTF-8";

                byte[] load = Encoding.UTF8.GetBytes(paramJson);
                wbRequest.ContentLength = load.Length;
                using (Stream writer = wbRequest.GetRequestStream())
                {
                    writer.Write(load, 0, load.Length);
                }

                //接收返回包
                string resultJson = string.Empty;
                HttpWebResponse wbResponse = (HttpWebResponse)(await wbRequest.GetResponseAsync());

                MemoryStream stream = new MemoryStream();
                using (Stream responseStream = wbResponse.GetResponseStream())
                {
                    #region----注释代码保留,这里使用了responseStream会影响该字段转换为byte [],原因不详。----
                    //注释代码保留,这里使用了responseStream会影响该字段转换为byte [],原因不详。
                    //如果请求成功会直接返回数据流,
                    //如果请求失败会返回Json{"errcode":61004,"errmsg":"access clientip is not registered hint: [aHCZjA05601518]"}
                    //StreamReader sread = new StreamReader(responseStream);
                    ////{"errcode":61004,"errmsg":"access clientip is not registered hint: [aHCZjA05601518]"}
                    //string result = sread.ReadToEnd();
                    //bool isConvertToStream = false;//是否转换为byte []
                    //string logMsg = "接收微信返回错误消息:";
                    //try
                    //{
                    //    Dictionary<string, string> dicResult = JsonExt.ToObject<Dictionary<string, string>>(result);
                    //    foreach (var item in dicResult)
                    //    {
                    //        logMsg += item.Key + ":" + item.Value;
                    //    }
                    //}
                    //catch (Exception ex)
                    //{
                    //    isConvertToStream = true;
                    //    Log4NetHelper.Log(LogTypeEnum.ServicesLog, LogLevelEnum.Error,
                    //       logMsg, ex);
                    //}
                    #endregion

                    //将数据流转为byte[]
                    List<byte> bytes = new List<byte>();
                    int temp = responseStream.ReadByte();
                    while (temp != -1)
                    {
                        bytes.Add((byte)temp);
                        temp = responseStream.ReadByte();
                    }
                    byte[] mg = bytes.ToArray();

                    stream = new MemoryStream(mg);
                }
                return stream;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

 

posted on 2022-10-28 17:43  Jankie1122  阅读(25)  评论(0编辑  收藏  举报