Пример #1
0
        /// <summary>
        /// 发送请求
        /// </summary>
        /// <returns></returns>
        public bool Send(out JObject resJson)
        {
            string url      = Host + PostPath;
            string postBody = RootJson.ToString();
            string sign     = CommonContext.Md5Utf8Encrupt("POST" + url + postBody + AppMasterSecret).ToLower();

            url = url + "?sign=" + sign;

            var request = (HttpWebRequest)WebRequest.Create(url);

            request.Method      = "POST";
            request.UserAgent   = UserAgent;
            request.Timeout     = 2 * 60 * 1000; //超时时间设置为两分钟
            request.ContentType = "application/json";
            //request.Headers.Set("Pragma", "no-cache");

            byte[] postData = Encoding.UTF8.GetBytes(postBody);//参数列表
            string retstring;

            try
            {
                using (var requestStream = request.GetRequestStream())
                {
                    requestStream.Write(postData, 0, postData.Length);
                    using (var response = request.GetResponse())
                    {
                        using (var responseStream = response.GetResponseStream())
                        {
                            using (StreamReader myStreamReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")))
                            {
                                retstring = myStreamReader.ReadToEnd();
                            }
                        }
                    }
                }
            }
            catch (WebException wEx)
            {
                using (var res = (HttpWebResponse)wEx.Response)
                {
                    using (StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.GetEncoding("utf-8")))
                    {
                        retstring = sr.ReadToEnd();
                        LogHelper.WriteLog("发送失败,Status:" + res.StatusCode);
                    }
                }
            }
            try
            {
                JObject jObject = JObject.Parse(retstring);
                string  result  = jObject.Property("ret").Value.ToString();
                resJson = jObject;
                if (result.Equals("SUCCESS", StringComparison.OrdinalIgnoreCase))
                {
                    LogHelper.WriteLog("调用友盟发送成功");
                }
                else
                {
                    LogHelper.WriteLog("调用友盟发送失败:" + retstring);
                    return(false);
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex.ToString());
                resJson = new JObject();
                return(false);
            }
            return(true);
        }