Exemplo n.º 1
0
        /// <summary>
        /// Upload file with device_tokens or alias to Umeng
        /// </summary>
        /// <param name="contents"></param>
        /// <returns></returns>
        public string UploadContents(string contents)
        {
            if (RootJson.Properties().All(p => p.Name != "appkey") || RootJson.Properties().All(p => p.Name != "timestamp") || RootJson.Properties().All(p => p.Name != "validation_token"))
            {
                throw new Exception("appkey, timestamp and validation_token needs to be set.");
            }
            // Construct the json string
            JObject uploadJson = new JObject
            {
                {"appkey", RootJson.GetValue("appkey")},
                {"timestamp", RootJson.GetValue("timestamp")},
                {"validation_token", RootJson.GetValue("validation_token")},
                {"content", contents}
            };
            // Construct the request
            string url = Host + UploadPath;
            string postBody = uploadJson.ToString();

            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;
            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();
                            //ret = responseStream.ReadBytes();
                        }
                    }
                }
            }

            try
            {

                JObject jObject = JObject.Parse(retString);
                string result = jObject.Property("ret").Value.ToString();
                if (result.Equals("SUCCESS", StringComparison.OrdinalIgnoreCase))
                {
                    string fileId = jObject.GetValue("data").ToObject<JObject>().GetValue("file_id").ToString();
                    SetPredefinedKeyValue("file_id", fileId);
                    return fileId;
                }
                else
                {
                    LogHelper.WriteLog("调用友盟发送失败");
                    LogHelper.WriteLog(retString);
                    throw new Exception("Failed to upload file");
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex.ToString());
                throw ex;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Set key/value in the RootJson, for the keys can be set please see RootKeys, PayloadKeys,
        /// BodyKeys and PolicyKeys.
        /// 设置预定义的key/value
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public sealed override bool SetPredefinedKeyValue(string key, object value)
        {
            if (RootKeys.Contains(key))
            {
                // This key should be in the root level
                RootJson.Add(key, JToken.FromObject(value));
            }
            else if (PayloadKeys.Contains(key))
            {
                // This key should be in the payload level
                JObject payloadJson = null;

                if (RootJson.Properties().Any(p => p.Name == "payload"))
                {
                    payloadJson = RootJson.GetValue("payload").ToObject <JObject>();
                }
                else
                {
                    payloadJson = new JObject();
                    RootJson.Add("payload", payloadJson);
                }
                payloadJson.Add(key, JToken.FromObject(value));
                //需要重新赋值,否则值设置不上
                RootJson.Property("payload").Value = payloadJson;
            }
            else if (BodyKeys.Contains(key))
            {
                // This key should be in the body level
                JObject bodyJson    = null;
                JObject payloadJson = null;
                // 'body' is under 'payload', so build a payload if it doesn't exist
                if (RootJson.Properties().Any(p => p.Name == "payload"))
                {
                    payloadJson = RootJson.Property("payload").Value.ToObject <JObject>();
                }
                else
                {
                    payloadJson = new JObject();
                    RootJson.Add("payload", payloadJson);
                }
                // Get body JSONObject, generate one if not existed
                if (payloadJson.Properties().Any(p => p.Name == "body"))
                {
                    bodyJson = payloadJson.GetValue("body").ToObject <JObject>();
                }
                else
                {
                    bodyJson = new JObject();
                    payloadJson.Add("body", bodyJson);
                }
                bodyJson.Add(key, JToken.FromObject(value));
                //需要重新赋值,否则值设置不上
                payloadJson.Property("body").Value = bodyJson;
                RootJson.Property("payload").Value = payloadJson;
            }
            else if (PolicyKeys.Contains(key))
            {
                // This key should be in the body level
                JObject policyJson = null;
                if (RootJson.Properties().Any(p => p.Name == "policy"))
                {
                    policyJson = RootJson.Property("policy").Value.ToObject <JObject>();
                }
                else
                {
                    policyJson = new JObject();
                    RootJson.Add("policy", policyJson);
                }
                policyJson.Add(key, JToken.FromObject(value));
                //需要重新赋值,否则值设置不上
                RootJson.Property("policy").Value = policyJson;
            }
            else
            {
                if (key == "payload" || key == "body" || key == "policy" || key == "extra")
                {
                    throw new Exception("You don't need to set value for " + key + " , just set values for the sub keys in it.");
                }
                else
                {
                    throw new Exception("Unknown key: " + key);
                }
            }
            return(true);
        }