.net post json

7/22/2016 4:29:08 PM

protected void backSend()
        {
            string path = "";
            string strURL = "http://api.dms.aodianyun.com" + path;
            WisApi api = new WisApi();
            Int64 lexpire = DateTimeToUnixTimestamp(DateTime.Now);
            string str = "";
            
            Dictionary<string, object> obj = new Dictionary<string, object>();
            obj["time"] = lexpire;
            obj["content"] = "124";
            JavaScriptSerializer js = new JavaScriptSerializer();
            string content = DictionaryToJson(obj);

            str = "POST\n" + path + "\n" + lexpire + "\n" + md5(content) + "\n"; ;

            string Signature = api.Sign(str);

            string authorization = "tis " + Signature;
            try
            {
                System.Net.HttpWebRequest request;
                request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
                request.Method = "POST";
                request.ContentType = "application/json; charset=UTF-8";
                request.Headers["Authorization"] = authorization;
                request.Headers["AD-Expire"] = "" + lexpire;
                request.Timeout = 60 * 1000;
                byte[] buffer = Encoding.UTF8.GetBytes(content);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
                using (HttpWebResponse resp = request.GetResponse() as HttpWebResponse)
                {
                    if (resp.StatusCode == HttpStatusCode.OK ||
                        resp.StatusCode == HttpStatusCode.Created ||
                        resp.StatusCode == HttpStatusCode.NoContent)
                    {
                        using (StreamReader reader = new StreamReader(resp.GetResponseStream(), Encoding.UTF8))
                        {
                            IDictionary<string, object> back=JsonToDictionary(reader.ReadToEnd());
                            Response.Write(js.Serialize(back));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }