Ejemplo n.º 1
0
        private static void CreateLog(Log log)
        {
            string httpPostURL = string.Concat(ConfigUtils.GetSetting(DefineUtils.Lm_Url), "/api/log");

            JsonSerializer serializer = new JsonSerializer();

            serializer.Converters.Add(new JavaScriptDateTimeConverter());
            serializer.NullValueHandling = NullValueHandling.Ignore;

            string jsonString = JsonConvert.SerializeObject(log, Formatting.Indented);

            HttpWebRequest request = ( HttpWebRequest )WebRequest.Create(httpPostURL);

            request.Method = "POST";

            UTF8Encoding encoding = new UTF8Encoding();

            byte [] byteArray = encoding.GetBytes(jsonString);

            request.ContentLength = byteArray.Length;
            request.ContentType   = "application/json";

            using (Stream dataStream = request.GetRequestStream())
            {
                dataStream.Write(byteArray, 0, byteArray.Length);
            }

            long length = 0;

            try
            {
                using (HttpWebResponse response = ( HttpWebResponse )request.GetResponse())
                {
                    // got response
                    length = response.ContentLength;
                }
            }
            catch (WebException ex)
            {
                WebResponse errorResponse = ex.Response;
                using (Stream responseStream = errorResponse.GetResponseStream())
                {
                    StreamReader reader    = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
                    string       errorText = reader.ReadToEnd();
                }
            }
        }