Exemple #1
0
        /// <summary>
        /// Post方式提交数据
        /// </summary>
        /// <param name="url"></param>
        /// <param name="json"></param>
        /// <returns></returns>
        public static string PostData(string url, string json, ref APIExecuteStatistics data, Dictionary <string, string> customHeaders)
        {
            string result = "";

            data.BytesSent = data.BytesReceived = 0;
            Stopwatch watch = new Stopwatch();

            watch.Start();
            try
            {
                string         strURL  = url;
                HttpWebRequest request = GetWebRequest(url, "POST");
                request.ContentType = "application/json;charset=UTF-8";
                if (customHeaders != null && customHeaders.Count > 0)
                {
                    foreach (var item in customHeaders)
                    {
                        request.Headers.Add(item.Key, System.Web.HttpUtility.UrlEncode(item.Value, Encoding.UTF8));
                    }
                }
                byte[] payload = Encoding.UTF8.GetBytes(json);
                //if (SupportGZip)
                //{
                //    payload = HttpExtension.Compress(payload);
                //}
                using (Stream writer = request.GetRequestStream())
                {
                    writer.Write(payload, 0, payload.Length);
                    writer.Close();
                    data.BytesSent += request.GetRequestLength();
                    SentBytes      += data.BytesSent;
                    LastSentBytes  += data.BytesSent;
                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        Encoding encoding = GetEncoding(response);
                        result              = GetResponseAsString(response, encoding, ref data);
                        data.BytesReceived += response.GetResponseLength();
                        ReceivedBytes      += data.BytesReceived;
                        LastReceivedBytes  += data.BytesReceived;
                    }
                }
            }
            catch (Exception e)
            {
                data.Exception = e;
                string       message      = e.Message;
                WebException webException = e as WebException;
                if (webException != null)
                {
                    try
                    {
                        HttpWebResponse response = webException.Response as HttpWebResponse;
                        Encoding        encoding = GetEncoding(response);
                        string          body     = GetResponseAsString(response, encoding, ref data);
                        message            += Environment.NewLine + "服务返回:" + body;
                        data.BytesReceived += response.GetResponseLength();
                        ReceivedBytes      += data.BytesReceived;
                        LastReceivedBytes  += data.BytesReceived;
                    }
                    catch { }
                }
                if (Debugger.IsAttached)
                {
                    message += Environment.NewLine + e.StackTrace.ToString();
                }
                result = GetJson(new ErrorWMSResponse {
                    Code = e.Message, Message = message
                });
            }
            watch.Stop();
            data.NetworkElapseTime = watch.ElapsedMilliseconds;
            return(result);
        }