Пример #1
0
 /// <summary>
 /// 发起Post请求,可上传文件
 /// </summary>
 /// <typeparam name="T">返回数据类型(Json对应的实体)</typeparam>
 /// <param name="url">地址</param>
 /// <param name="cookieContainer">CookieContainer</param>
 /// <param name="postStream">参数数据流</param>
 /// <param name="fileDictionary">字典文件流,需要上传的文件,Key:对应要上传的Name,Value:本地文件名</param>
 /// <param name="encoding">默认null为UTF8</param>
 /// <param name="timeOut">默认null为10秒</param>
 /// <param name="refererUrl">RefererUrl</param>
 /// <param name="maxJsonLength">允许最大JSON长度</param>
 /// <param name="isDebug">是否为debug,默认false</param>
 /// <param name="contentType">ContentType</param>
 /// <param name="headerDictionary">HeaderDictionary</param>
 /// <param name="accept">Accept</param>
 public static T PostGetJson <T>(string url, CookieContainer cookieContainer = null, Stream postStream = null, Dictionary <string, string> fileDictionary = null, string refererUrl = null, Encoding encoding = null, int?timeOut = null, int?maxJsonLength = null, bool isDebug = false, ContentType?contentType = null, Dictionary <string, string> headerDictionary = null, ContentType?accept = null) where T : BaseResponse, new()
 {
     try
     {
         string returnText = RequestUtility.HttpPost(url, cookieContainer, postStream, fileDictionary, refererUrl, encoding, timeOut, contentType, headerDictionary, HttpMethod.Post, accept);
         LogTraceHelper.SendLog(string.Format("Method:POST\r\n\tParames:{0}\r\n\tReturnText:{1}\r\n\tStatusCode:{2}", postStream.GetQueryJsonString(), returnText, 200), url, isDebug: isDebug);//记录日志
         var result = SerializerUtility.GetResult <T>(returnText, maxJsonLength: maxJsonLength);
         return(result);
     }
     catch (WebException ex)
     {
         var httpWebResponse = ex.Response as HttpWebResponse;
         if (httpWebResponse != null)
         {
             var returnText = new StreamReader(httpWebResponse.GetResponseStream()).ReadLine();
             var result     = SerializerUtility.GetResult <T>(null, returnText, httpWebResponse.StatusCode, maxJsonLength);
             LogTraceHelper.SendLog(string.Format("Method:POST\r\n\tParames:{0}\r\n\tReturnText:{1}\r\n\tStatusCode:{2}", postStream.GetQueryJsonString(), returnText, (int)httpWebResponse.StatusCode), url, isDebug: isDebug);//记录日志
             return(result);
         }
         else
         {
             T model = new T();
             ((BaseResponse)model).StatusCode = HttpStatusCode.InternalServerError;
             LogTraceHelper.SendLog(string.Format("Method:DELETE\r\n\tParames:{0}\r\n\tMessage:{1}\r\n\tStackTrace:{2}\r\n\tStatusCode:{3}", postStream.GetQueryJsonString(), ex.Message, ex.StackTrace, 0), url, isDebug: isDebug); //记录日志
             return(model);
         }
     }
     return(null);
 }
Пример #2
0
 /// <summary>
 /// 填充EasemobServer配置节点
 /// </summary>
 private static void FillEasemobServerConfigSection()
 {
     try
     {
         ServerConfigSection serverConfigSection = ConfigurationManager.GetSection("EasemobServer") as ServerConfigSection;
         if (serverConfigSection != null)
         {
             foreach (ServerConfigElement item in serverConfigSection.ServerConfigElementCollection)
             {
                 if (!ServerConfigs.ContainsKey(item.AppName))
                 {
                     ServerConfigs.Add(item.AppName, item);
                 }
             }
         }
         else
         {
             throw new AggregateException("EasemobServer节点读取失败,请检查配置!");
         }
     }
     catch (Exception ex)
     {
         LogTraceHelper.SendLog(string.Format("Message:{0}\r\n\tStackTrace:{1}", ex.Message, ex.StackTrace), "FillEasemobServerConfigSection", isDebug: true);
     }
 }
Пример #3
0
        /// <summary>
        /// 使用Post方法上传数据并下载文件或结果
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="data">json数据</param>
        /// <param name="stream">填充的Stream</param>
        /// <param name="contentType">ContentType</param>
        /// <param name="isDebug">是否为debug,默认false</param>
        /// <param name="headerDictionary">自定义请求头参数</param>
        /// <param name="accept">Accept</param>
        public static void FillDownload(string url, string data, Stream stream, ContentType?contentType = null, bool isDebug = false, Dictionary <string, string> headerDictionary = null, ContentType?accept = null)
        {
            try
            {
                WebClient wc = new WebClient();
                if (contentType != null)
                {
                    wc.Headers[HttpRequestHeader.ContentType] = contentType.Value.ToValue();
                }

                if (accept != null)
                {
                    wc.Headers[HttpRequestHeader.Accept] = accept.Value.ToValue();
                }
                if (headerDictionary != null)//自定义头增加
                {
                    foreach (var header in headerDictionary)
                    {
                        switch (header.Key.ToLower())
                        {
                        case "accept":
                            wc.Headers[HttpRequestHeader.Accept] = header.Value;
                            break;

                        case "contenttype":
                            wc.Headers[HttpRequestHeader.ContentType] = header.Value;
                            break;

                        default:
                            wc.Headers.Add(header.Key, header.Value);
                            break;
                        }
                    }
                }
                var file = wc.UploadData(url, "POST", Encoding.UTF8.GetBytes(string.IsNullOrEmpty(data) ? "" : data));
                foreach (var b in file)
                {
                    stream.WriteByte(b);
                }
                LogTraceHelper.SendLog(string.Format("Method:POST\r\n\tParames:{0}\r\n\tReturnText:{1}\r\n\tStatusCode:{2}", data, "", 200), url, isDebug: isDebug); //记录日志
            }
            catch (WebException ex)
            {
                var httpWebResponse = ex.Response as HttpWebResponse;
                if (httpWebResponse != null)
                {
                    var returnText = new StreamReader(httpWebResponse.GetResponseStream()).ReadLine();
                    LogTraceHelper.SendLog(string.Format("Method:POST\r\n\tParames:{0}\r\n\tReturnText:{1}\r\n\tStatusCode:{2}", data, returnText, (int)httpWebResponse.StatusCode), url, isDebug: isDebug); //记录日志
                }
                else
                {
                    LogTraceHelper.SendLog(string.Format("Method:DELETE\r\n\tParames:{0}\r\n\tMessage:{1}\r\n\tStackTrace:{2}\r\n\tStatusCode:{3}", data, ex.Message, ex.StackTrace, 0), url, isDebug: isDebug); //记录日志
                }
            }
        }
Пример #4
0
 /// <summary>
 /// 填充Custom自定义配置节点
 /// </summary>
 private static void FillCustomerConfig()
 {
     try
     {
         var customServerConfig = new CustomServerConfig();
         if (!customServerConfig.AppName.IsNullOrEmpty())
         {
             if (!ServerConfigs.ContainsKey(customServerConfig.AppName))
             {
                 ServerConfigs.Add(customServerConfig.AppName, customServerConfig);
             }
         }
     }
     catch (Exception ex)
     {
         LogTraceHelper.SendLog(string.Format("Message:{0}\r\n\tStackTrace:{1}", ex.Message, ex.StackTrace), "FillCustomerConfig", isDebug: true);
     }
 }
Пример #5
0
 /// <summary>
 /// 实例化SyncRequest
 /// </summary>
 private static void InstantiateSyncRequest()
 {
     try
     {
         if (ServerConfigs.Count > 0)
         {
             foreach (var serverConfig in ServerConfigs)
             {
                 SyncRequests.Add(serverConfig.Key, new SyncRequest(serverConfig.Value));
             }
         }
         if (SyncRequests.Count > 0)
         {
             DefaultSyncRequest = SyncRequests.FirstOrDefault().Value;
         }
     }
     catch (Exception ex)
     {
         LogTraceHelper.SendLog(string.Format("Message:{0}\r\n\tStackTrace:{1}", ex.Message, ex.StackTrace), "InstantiateSyncRequest", isDebug: true);
     }
 }
Пример #6
0
        /// <summary>
        /// 获取Post结果
        /// </summary>
        /// <typeparam name="T">返回数据类型(Json对应的实体)</typeparam>
        /// <param name="returnText">要序列化的JSON</param>
        /// <param name="errorText">错误信息的JSON,默认为null</param>
        /// <param name="statusCode">错误返回的请求码</param>
        /// <param name="maxJsonLength">允许最大JSON长度</param>
        public static T GetResult <T>(string returnText, string errorText = null, HttpStatusCode?statusCode = null, int?maxJsonLength = null) where T : BaseResponse, new()
        {
            try
            {
                JavaScriptSerializer js = new JavaScriptSerializer();
                if (maxJsonLength != null && maxJsonLength.Value > 0)
                {
                    js.MaxJsonLength = maxJsonLength.Value;
                }
                if (!errorText.IsNullOrEmpty())
                {
                    T             model  = new T();
                    ErrorResponse result = js.Deserialize <ErrorResponse>(errorText);
                    ((BaseResponse)model).ErrorMessage = result;
                    ((BaseResponse)model).StatusCode   = statusCode ?? HttpStatusCode.InternalServerError;
                    return(model);
                }
                else
                {
                    //特殊返回字符处理
                    var replace = returnText.Replace("share-secret", "share_secret"); //处理share-secret

                    T result = js.Deserialize <T>(replace);
                    ((BaseResponse)result).StatusCode = HttpStatusCode.OK;  //默认为200
                    return(result);
                }
            }
            catch (Exception ex)
            {
                LogTraceHelper.SendLog(string.Format("序列化失败!\r\n\tMessage:{0}\r\n\tStackTrace:{1}", ex.Message, ex.StackTrace), "GetResult", isDebug: true);
                T model = new T();
                ((BaseResponse)model).StatusCode   = statusCode ?? HttpStatusCode.BadRequest;
                ((BaseResponse)model).ErrorMessage = new ErrorResponse()
                {
                    exception = ex.Message, error_description = ex.StackTrace
                };
                return(model);
            }
        }