/// <summary> /// 请求方法WebClient /// </summary> /// <param name="model"></param> /// <returns></returns> public static string CallRequestMethodWebClient(ResponseCallModel model) { try { WebClient wc = new WebClient(); string format = string.Empty; if (model.EncodingFormat == EncodingFormat.UTF8) { format = "utf-8"; } else { format = model.EncodingFormat.ToString(); } Encoding encoding = Encoding.GetEncoding(format); //下面是GB2312编码 byte[] sendData = encoding.GetBytes(model.RequestParam); //wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); wc.Headers.Add("Content-Type", "text/plain; charset=utf-8"); wc.Headers.Add("ContentLength", sendData.Length.ToString()); byte[] recData = wc.UploadData(model.URI, model.RequestMethod.ToString(), sendData); return(encoding.GetString(recData)); } catch (Exception ex) { BugLog.Write(ex.ToString()); throw new WebException("WebClient请求出错!!" + ex.ToString()); } }
/// <summary> /// 异步方法写入 系统日志 /// </summary> /// <param name="msg">信息</param> /// <param name="file">AppDomain.CurrentDomain.BaseDirectory + "SysLog"+"\\" + DateTime.Now.ToString("yyyyMMdd") + ".log"; 为null时有默认路径</param> ///当filePath不为null时 指定的是 文件(可以读写的文件) public static void Write(string msg, string file = null) { Task.Factory.StartNew(() => { BugLog.Write2File(msg, file); }); }
/// <summary> /// 请求方法WebResponse/WebRequest /// </summary> /// <param name="model"></param> /// <returns></returns> public static string CallRequestMethod(ResponseCallModel model) { try { Uri uri = null; try { uri = new Uri(model.URI); } catch (Exception ex) { throw new UriFormatException("url 错误" + model.URI + ex.ToString()); } WebRequest req = HttpWebRequest.Create(model.URI); string format = string.Empty; if (model.EncodingFormat == EncodingFormat.UTF8) { format = "utf-8"; } else { format = model.EncodingFormat.ToString(); } Encoding encoding = Encoding.GetEncoding(format); string param = model.RequestParam; param = param.Replace("+", "%2B"); param = param.Replace("&", "%26"); //param = param.Replace("/", "%2F"); //param = param.Replace("?", "%3F"); //param = param.Replace("%", "%25"); //param = param.Replace("#", "%23"); //param = param.Replace("<", "<"); byte[] bs = encoding.GetBytes(param); //Encoding.ASCII.GetBytes req.Method = model.RequestMethod.ToString(); //req.ContentType = "application/x-www-form-urlencoded"; text/plain; charset=utf-8 req.ContentType = "text/plain; charset=utf-8"; req.ContentLength = bs.Length; using (Stream sw = req.GetRequestStream()) { sw.Write(bs, 0, bs.Length); //sw.Close(); } string responseData = String.Empty; using (HttpWebResponse response = (HttpWebResponse)req.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding)) { responseData = reader.ReadToEnd().ToString(); } } return(responseData); } catch (Exception ex) { BugLog.Write(ex.ToString()); throw new WebException("http请求出错!!" + ex.ToString()); } }
/// <summary> /// 异常处理 /// </summary> /// <param name="filterContext"></param> public override void OnException(ExceptionContext filterContext) { Exception Error = filterContext.Exception; //string Message = Error.Message;//错误信息 string Url = HttpContext.Current.Request.RawUrl;//错误发生地址 BugLog.Write(Error.Message + "\n\t" + Url); filterContext.ExceptionHandled = true; filterContext.Result = new RedirectResult("/Report/Error/"); //跳转至错误提示页面 }
private void CheckInput(string parameter, ref bool isTrue) { for (int i = 0; i < blackList.Length; i++) { if ((parameter.IndexOf(blackList[i], StringComparison.OrdinalIgnoreCase) >= 0)) { isTrue = true; BugLog.Write("输入非法 MYSQLInjectAttribute " + parameter + "包含了 " + blackList[i]); //throw new Exception(); } } }