转自:http://www.jb51.net/article/28401.htm

在每个系统出写入报告错误代码(找个合理的理由,比如系统免费升级) -> 自家服务器接收并处理错误报告 -> 反馈用户(解决掉BUG就行,不要太声扬) 最近公司拓展市场异常迅猛,数周之类开出去几十套系统,虽然系统名字不一样,但各个内容相似。由于时间紧迫,很多开出去的系统 出现各种神奇的错误,当初虽然有记录错误日志,然而很多客户使用的是自己的服务器和数据库,出了问题我们并不能立即掌握信息, 因此决定做一个捕获所有系统的异常并保存到自家数据库中。 实现思路 在每个系统出写入报告错误代码(找个合理的理由,比如系统免费升级) -> 自家服务器接收并处理错误报告 -> 反馈用户(解决掉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. string postString = "targetSite=" + HttpUtility.UrlEncode(targetSite);
  21. postString += "&stackTrace=" + HttpUtility.UrlEncode(stackTrace);
  22. postString += "&friendlyMsg=" + HttpUtility.UrlEncode(friendlyMsg);
  23. postString += "&errorPage=" + HttpUtility.UrlEncode(errorPage);
  24. postString += "&projectName=" + HttpUtility.UrlEncode(projectName);
  25. postString += "&key=" + "";
  26. HttpWebRequest webRequest = null;
  27. StreamWriter requestWriter = null;
  28. string responseData = "";
  29. webRequest = System.Net.WebRequest.Create(uriString) as HttpWebRequest;
  30. webRequest.Method = "POST";
  31. webRequest.ServicePoint.Expect100Continue = false;
  32. webRequest.Timeout = * ;
  33. webRequest.ContentType = "application/x-www-form-urlencoded";
  34. //POST the data.
  35. requestWriter = new StreamWriter(webRequest.GetRequestStream());
  36. try
  37. {
  38. requestWriter.Write(postString);
  39. }
  40. catch (Exception ex2)
  41. {
  42. return "连接错误";
  43. }
  44. finally
  45. {
  46. requestWriter.Close();
  47. requestWriter = null;
  48. }
  49. responseData = WebResponseGet(webRequest);
  50. webRequest = null;
  51. return responseData;
  52. }
  53. catch
  54. {
  55. return "未知错误";
  56. }
  57. }
  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. try
  11. {
  12. responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
  13. responseData = responseReader.ReadToEnd();
  14. }
  15. catch
  16. {
  17. return "连接错误";
  18. }
  19. finally
  20. {
  21. webRequest.GetResponse().GetResponseStream().Close();
  22. responseReader.Close();
  23. responseReader = null;
  24. }
  25. return responseData;
  26. }

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. }

当获取服务器响应出错时,捕捉错误,最终打印出错误即可。

其它例子,转自:http://www.jb51.net/article/1316.htm

最近有个朋友离开IT行业二年的朋友说要实现用程序向某个网站的页面上传数据,他是意思是每天有几十条数据要在网站页面上填写,很烦,最好用程序来写。网站页面是用POST传递的,同时没有验证码之类的东东,只有一点限制就是5分种内不能填写二次记录。这一切都好办。

  1. using System.Web;
  2. using System.Net;
  3. using System.Text;
  4. using System.IO;
  5.  
  6. //创建对某个网站页面的请求
  7.  
  8. HttpWebRequest myRequest = (HttpWebRequest )WebRequest.Create("http://www.knowsky.com/a.asp")
  9.  
  10. //上传的数据,”TextBox1“这些东东是网站页面里的控件ID,如果要上传多个值也是用&来分隔
  11.  
  12. string postData="TextBox1="+this.textBox1.Text+"&TextBox2="+this.textBox2.Text+"
  13. &TextBox3="+this.textBox3.Text+"&TextBox4="+this.textBox4.Text;
  14. ASCIIEncoding encoding=new ASCIIEncoding();
  15. byte[] byte1=encoding.GetBytes(postData);//最终编码后要上传的数据
  16. // Set the content type of the data being posted.
  17. myRequest.ContentType="application/x-www-form-urlencoded";
  18. myRequest.Method="post";//post上传方式
  19. // Set the content length of the string being posted.
  20. myRequest.ContentLength=postData.Length;
  21. Stream newStream=myRequest.GetRequestStream();
  22. newStream.Write(byte1,,byte1.Length);
  23.  
  24. 详细出处参考:http://www.jb51.net/article/1316.htm

一切就OK了,如果你想上传后看到网站的内容的话,可以在程序里放一个IE控件,使用

  1. axWebBrowser1.Navigate("http://www.knowsky.com/a.asp");
  2. axWebBrowser1.Refresh2();
  3.  
  4. 详细出处参考:http://www.jb51.net/article/1316.htm

HttpWebRequest与HttpWebResponse使用例子(转)的更多相关文章

  1. c# HttpWebRequest与HttpWebResponse 绝技(转载)

    c# HttpWebRequest与HttpWebResponse 绝技    如果你想做一些,抓取,或者是自动获取的功能,那么就跟我一起来学习一下Http请求吧.本文章会对Http请求时的Get和P ...

  2. C#模拟POST提交表单(二)--HttpWebRequest以及HttpWebResponse

    上次介绍了用WebClient的方式提交POST请求,这次,我继续来介绍用其它一种方式 HttpWebRequest以及HttpWebResponse 自认为与上次介绍的WebClient最大的不同之 ...

  3. C# -- HttpWebRequest 和 HttpWebResponse 的使用 C#编写扫雷游戏 使用IIS调试ASP.NET网站程序 WCF入门教程 ASP.Net Core开发(踩坑)指南 ASP.Net Core Razor+AdminLTE 小试牛刀 webservice创建、部署和调用 .net接收post请求并把数据转为字典格式

    C# -- HttpWebRequest 和 HttpWebResponse 的使用 C# -- HttpWebRequest 和 HttpWebResponse 的使用 结合使用HttpWebReq ...

  4. c# HttpWebRequest与HttpWebResponse

    [转]c# HttpWebRequest与HttpWebResponse 绝技 如果你想做一些,抓取,或者是自动获取的功能,那么就跟我一起来学习一下Http请求吧. 本文章会对Http请求时的Get和 ...

  5. HttpWebRequest以及HttpWebResponse

    上次介绍了用WebClient的方式提交POST请求,这次,我继续来介绍用其它一种方式 HttpWebRequest以及HttpWebResponse 自认为与上次介绍的WebClient最大的不同之 ...

  6. C# HttpWebRequest与HttpWebResponse详解

    C# HttpWebRequest与HttpWebResponse详解  http://www.codeproject.com/Articles/6554/How-to-use-HttpWebRequ ...

  7. 使用HttpWebRequest以及HttpWebResponse读取Http远程文件

     主页>杂项技术>.NET(C#)> 使用HttpWebRequest以及HttpWebResponse读取Http远程文件 jackyhwei 发布于 2010-08-15 21: ...

  8. HttpWebRequest和HttpWebResponse用法小结

    http://www.cnblogs.com/willpan/archive/2011/09/26/2176475.html http://www.cnblogs.com/lip0121/p/4539 ...

  9. 利用HttpWebRequest和HttpWebResponse获取Cookie

    之前看过某个同学的一篇有关与使用JSoup解析学校图书馆的文章,仔细一看,发现竟然是同校!!既然对方用的是java,那么我也就来个C#好了,虽然我的入门语言是java. C#没有JSoup这样方便的东 ...

随机推荐

  1. 编译maxscale

    编译maxscale,需要依赖mariadb版本的MySQL.有自己的版本就是任性啊

  2. ARPG游戏技能系统设计

    ARPG游戏的技能的前端表现, 主要指的是人物动作和特效表现, 从普遍意义上讲,大致可以分为三个阶段:起手.飞行.碰撞(爆炸). [特效可见性判断] 1.[attacker & victim] ...

  3. 资源 之 4.3 访问Resource(拾壹)

    4.3.1  ResourceLoader接口 ResourceLoader接口用于返回Resource对象:其实现可以看作是一个生产Resource的工厂类. public interface Re ...

  4. 关于http响应内容压缩的一点小积累。

    1.在tomcat的server.xml配置文件中,添加上背景颜色为绿色的配置,服务器就会自动压缩 <Connector port="80" maxHttpHeaderSiz ...

  5. 【CITE】当类库项目中无法使用Application.StartupPath的时侯 (注:主要是在进行反射读取文件的时候!!)

    http://jcserver.blog.163.com/blog/static/24044859200851582354135/ 通常我们WinForm编程时,要获取程序当 前运行的文件夹路径会用A ...

  6. jq手风琴---点击时列表的左边距逐渐减小

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  7. 简单的Hibernate入门简介

    其实Hibernate本身是个独立的框架,它不需要任何web server或application server的支持.然而,大多数的Hibernate入门介绍都加入了很多非Hibernate的东西, ...

  8. 《javascript高级程序设计》第三章 Language Basics

    3.1 语法syntax 3.1.1 区分大小写case-sensitivity 3.1.2 标识符identifiers 3.1.3 注释comments 3.1.4 严格模式strict mode ...

  9. 你不知道的JavaScript-- 事件流与事件处理

    转载:http://blog.csdn.net/i10630226/article/details/48970971 1. 事件处理 1.1. 绑定事件方式 (1)行内绑定 语法: //最常用的使用方 ...

  10. Java 集合系列 09 HashMap详细介绍(源码解析)和使用示例

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...