Ejemplo n.º 1
0
        public static async Task <T> PostGetJson <T>(string url, string json, CookieContainer cookieContainer = null, Encoding encoding = null)
        {
            string resultText = await HttpPostJson(url, json, encoding);

            var result = WeixinJsonHelper.Deserialize <T>(resultText);

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 发起Post请求
        /// </summary>
        /// <typeparam name="T">返回数据类型(Json对应的实体)</typeparam>
        /// <param name="url">请求Url</param>
        /// <param name="cookieContainer">CookieContainer,如果不需要则设为null</param>
        /// <param name="fileStream">文件流</param>
        /// <returns></returns>
        public static async Task <T> PostGetJson <T>(string url, CookieContainer cookieContainer = null, Stream fileStream = null, Encoding encoding = null)
        {
            string resultText = await HttpPost(url, cookieContainer, fileStream, null, null, encoding);

            var result = WeixinJsonHelper.Deserialize <T>(resultText);

            return(result);
        }
Ejemplo n.º 3
0
        public static async Task <T> PostGetJson <T>(string url, CookieContainer cookieContainer = null, Dictionary <string, string> formData = null, Encoding encoding = null)
        {
            string resultText = await HttpPost(url, cookieContainer, formData, encoding);

            var result = WeixinJsonHelper.Deserialize <T>(resultText);

            return(result);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 获取JSON数据。采用Http Get方式。
        /// </summary>
        /// <typeparam name="T">用于封装JSON数据的类</typeparam>
        /// <param name="url">API网址</param>
        /// <param name="encoding">编码。默认为UTF8</param>
        /// <returns></returns>
        public static async Task <T> GetJson <T>(string url, Encoding encoding = null)
        {
            string returnText = await HttpGet(url, encoding);

            Debug.WriteLine("HttpUtility.GetJson<T>");
            Debug.WriteLine("\turl:" + url);
            Debug.WriteLine("\treturn:" + returnText);

            T result = WeixinJsonHelper.Deserialize <T>(returnText);

            return(result);
        }
Ejemplo n.º 5
0
        public string ToJson(string jsapiTicket, string refererUrl)
        {
            var elements = new Dictionary <string, object>();

            elements.Add("jsapi_ticket", jsapiTicket);
            elements.Add("nonceStr", this.nonceStr);            //nonceStr
            elements.Add("timestamp", this.timestamp);
            elements.Add("url", refererUrl);

            signature = WeixinJssdkSignatureHelper.CalculateJsSignature(elements);

            return(WeixinJsonHelper.Serialize(this));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 通过POST请求方式,传入一个表单(object类型),获取一个JSON对象,并将其反向序列化,最终得到一个简单数据实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <param name="objRequest"></param>
        /// <returns></returns>
        public static async Task <T> GetJson <T>(string url, object objRequest)
        {
            var jsonString = WeixinJsonHelper.Serialize(objRequest);

            using (MemoryStream ms = new MemoryStream())
            {
                var bytes = Encoding.UTF8.GetBytes(jsonString);
                ms.Write(bytes, 0, bytes.Length);
                ms.Seek(0, SeekOrigin.Begin);

                return(await HttpUtility.PostGetJson <T>(url, null, ms));
            }
        }
Ejemplo n.º 7
0
        public static async Task <T> Send <T>(string url, object data, MessageSendType sendType = MessageSendType.GET)
        {
            switch (sendType)
            {
            case MessageSendType.GET:
                return(await HttpUtility.GetJson <T>(url));

            case MessageSendType.POST:
                var jsonString = WeixinJsonHelper.Serialize(data);
                using (MemoryStream ms = new MemoryStream())
                {
                    var bytes = Encoding.UTF8.GetBytes(jsonString);
                    ms.Write(bytes, 0, bytes.Length);
                    ms.Seek(0, SeekOrigin.Begin);

                    return(await HttpUtility.PostGetJson <T>(url, null, ms));
                }

            default:
                throw new ArgumentOutOfRangeException("sendType");
            }
        }
Ejemplo n.º 8
0
        public string ToJson(string jsapiTicket, string refererUrl)
        {
            var elements = new Dictionary <string, object>();

            elements.Add("jsapi_ticket", jsapiTicket);
            elements.Add("url", refererUrl);
            elements.Add("nonceStr", this.nonceStr);
            elements.Add("timestamp", this.timestamp);

            //var signFields = new string[]{"nonceStr","timestamp"};
            //var fieldInfos = this.GetType().GetFields()
            //    .Where(x => (signFields.Contains(x.Name)));
            //foreach (var fi in fieldInfos)
            //{
            //    elements.Add(fi.Name, fi.GetValue(this));
            //}

            signature = JsSignatureApi.CalculateJsSignature(elements);

            //JavaScriptSerializer oSerializer = new JavaScriptSerializer();
            //string json = oSerializer.Serialize(this);
            //return json;
            return(WeixinJsonHelper.Serialize(this));
        }