最近公司拓展市场异常迅猛,数周之类开出去几十套系统,虽然系统名字不一样,但各个内容相似。由于时间紧迫,很多开出去的系统
出现各种神奇的错误,当初虽然有记录错误日志,然而很多客户使用的是自己的服务器和数据库,出了问题我们并不能立即掌握信息,
因此决定做一个捕获所有系统的异常并保存到自家数据库中。
实现思路
在每个系统出写入报告错误代码(找个合理的理由,比如系统免费升级) -> 自家服务器接收并处理错误报告 -> 反馈用户(解决掉BUG就行,不要太声扬)
基础回顾
---参考msdn
1.HttpWebRequest类:提供WebRequest类的Http特定的实现。
HttpWebRequest 类对 WebRequest 中定义的属性和方法提供支持,也对使用户能够直接与使用 HTTP 的服务器交互的附加属性和方法提供支持。
不要使用构造函数创建HttpWebRequest实例,请使用System.Net.WebRequest.Create(URI uriString)来创建实例,如果URI是Http://或Https://,
返回的是HttpWebRequest对象。(建立请求特定URI的对象)
当向资源发送数据时,GetRequestStream方法返回用于发送数据的Stream对象。(获取请求数据的流对象)
GetResponse方法向RequestUri属性指定的资源发出同步请求并返回包含该响应的HttpWebResponse。(获取来自internet的响应)
实例讲解
1.远程请求并返回响应
1 ///<summary>
2 /// 报告系统错误
3 ///</summary>
4 ///<param name="ex"></param>
5 ///<returns></returns>
6 public static string Sys_ReportError(Exception ex)
7 {
8 try
9 {
10 //要提交表单的URI字符串
11 string uriString = "http://localhost/Sys_ReportError.aspx";
12 HttpContext context = HttpContext.Current;
13 if (context == null) return string.Empty;
14 string targetSite = ex.TargetSite.ToString();
15 string stackTrace = ex.StackTrace;
16 string friendlyMsg = ex.Message;
17 string errorPage = context == null || context.Request == null ? "" : context.Request.Url.ToString();
18 string projectName = Config.Sys_Title();
19
20 //要提交的字符串数据
21 string postString = "targetSite=" + HttpUtility.UrlEncode(targetSite);
22 postString += "&stackTrace=" + HttpUtility.UrlEncode(stackTrace);
23 postString += "&friendlyMsg=" + HttpUtility.UrlEncode(friendlyMsg);
24 postString += "&errorPage=" + HttpUtility.UrlEncode(errorPage);
25 postString += "&projectName=" + HttpUtility.UrlEncode(projectName);
26 postString += "&key=" + "";
27
28
29 HttpWebRequest webRequest = null;
30 StreamWriter requestWriter = null;
31 string responseData = "";
32
33 webRequest = System.Net.WebRequest.Create(uriString) as HttpWebRequest;
34 webRequest.Method = "POST";
35 webRequest.ServicePoint.Expect100Continue = false;
36 webRequest.Timeout = 1000 * 60;
37
38 webRequest.ContentType = "application/x-www-form-urlencoded";
39
40 //POST the data.
41 requestWriter = new StreamWriter(webRequest.GetRequestStream());
42 try
43 {
44 requestWriter.Write(postString);
45 }
46 catch (Exception ex2)
47 {
48 return "连接错误";
49 }
50 finally
51 {
52 requestWriter.Close();
53 requestWriter = null;
54 }
55
56 responseData = WebResponseGet(webRequest);
57
58 webRequest = null;
59
60 return responseData;
61 }
62 catch
63 {
64 return "未知错误";
65 }
66 }
1 ///<summary>
2 /// Process the web response.
3 ///</summary>
4 ///<param name="webRequest">The request object.</param>
5 ///<returns>The response data.</returns>
6 public static string WebResponseGet(HttpWebRequest webRequest)
7 {
8 StreamReader responseReader = null;
9 string responseData = "";
10
11 try
12 {
13 responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
14 responseData = responseReader.ReadToEnd();
15 }
16 catch
17 {
18 return "连接错误";
19 }
20 finally
21 {
22 webRequest.GetResponse().GetResponseStream().Close();
23 responseReader.Close();
24 responseReader = null;
25 }
26
27 return responseData;
28 }
2.远程服务器读取流
1 _context = HttpContext.Current;
2 Stream stream = _context.Request.InputStream; //获取当前传入Http输入流
3 long length = stream.Length;
4 byte[] data = _context.Request.BinaryRead((int)length);//对当前输入流进行指定字节数的二进制读取
5 string strContent = Encoding.UTF8.GetString(data);//解码为UTF8编码形式的字符串
代码讲解到此结束,一些相关补充:
1.HttpWebRequest对象有一些相关设置属性,如Method(发送方式),TimeOut(请求超时时间),ContentType(Http标头的值)等等。
2.若远程接收页面出错,该如何调试,很简单,只需写入下面的代码:
1 HttpWebResponse res = null;
2 WebResponse response = null;
3 try
4 {
5 WebResponse response = webRequest.GetResponse();
6 }
7 catch (WebException ex1)
8 {
9 res = (HttpWebResponse)ex1.Response;
10 }
11 finally
12 {
13 StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
14 string strhtml = sr.ReadToEnd();
15 HttpContext.Current.Response.Write(strhtml);
16 }
当获取服务器响应出错时,捕捉错误,最终打印出错误即可。
-----------------------------------------------------------------------真诚欢迎大家指摘------------------------------------------------------------------------