欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

HttpWebRequest和HttpWebResponse用法小结

程序员文章站 2024-03-06 18:31:38
最近公司拓展市场异常迅猛,数周之类开出去几十套系统,虽然系统名字不一样,但各个内容相似。由于时间紧迫,很多开出去的系统 出现各种神奇的错误,当初虽然有记录错误日志,然而很多...
最近公司拓展市场异常迅猛,数周之类开出去几十套系统,虽然系统名字不一样,但各个内容相似。由于时间紧迫,很多开出去的系统
出现各种神奇的错误,当初虽然有记录错误日志,然而很多客户使用的是自己的服务器和数据库,出了问题我们并不能立即掌握信息,
因此决定做一个捕获所有系统的异常并保存到自家数据库中。
实现思路
在每个系统出写入报告错误代码(找个合理的理由,比如系统免费升级) -> 自家服务器接收并处理错误报告 -> 反馈用户(解决掉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.远程请求并返回响应
复制代码 代码如下:

/// <summary>
/// 报告系统错误
/// </summary>
/// <param name="ex"></param>
/// <returns></returns>
public static string sys_reporterror(exception ex)
{
try
{
//要提交表单的uri字符串
string uristring = "http://localhost/sys_reporterror.aspx";
httpcontext context = httpcontext.current;
if (context == null) return string.empty;
string targetsite = ex.targetsite.tostring();
string stacktrace = ex.stacktrace;
string friendlymsg = ex.message;
string errorpage = context == null || context.request == null ? "" : context.request.url.tostring();
string projectname = config.sys_title();
//要提交的字符串数据
string poststring = "targetsite=" + httputility.urlencode(targetsite);
poststring += "&stacktrace=" + httputility.urlencode(stacktrace);
poststring += "&friendlymsg=" + httputility.urlencode(friendlymsg);
poststring += "&errorpage=" + httputility.urlencode(errorpage);
poststring += "&projectname=" + httputility.urlencode(projectname);
poststring += "&key=" + "";
httpwebrequest webrequest = null;
streamwriter requestwriter = null;
string responsedata = "";
webrequest = system.net.webrequest.create(uristring) as httpwebrequest;
webrequest.method = "post";
webrequest.servicepoint.expect100continue = false;
webrequest.timeout = 1000 * 60;
webrequest.contenttype = "application/x-www-form-urlencoded";
//post the data.
requestwriter = new streamwriter(webrequest.getrequeststream());
try
{
requestwriter.write(poststring);
}
catch (exception ex2)
{
return "连接错误";
}
finally
{
requestwriter.close();
requestwriter = null;
}
responsedata = webresponseget(webrequest);
webrequest = null;
return responsedata;
}
catch
{
return "未知错误";
}
}

复制代码 代码如下:

/// <summary>
/// process the web response.
/// </summary>
/// <param name="webrequest">the request object.</param>
/// <returns>the response data.</returns>
public static string webresponseget(httpwebrequest webrequest)
{
streamreader responsereader = null;
string responsedata = "";
try
{
responsereader = new streamreader(webrequest.getresponse().getresponsestream());
responsedata = responsereader.readtoend();
}
catch
{
return "连接错误";
}
finally
{
webrequest.getresponse().getresponsestream().close();
responsereader.close();
responsereader = null;
}
return responsedata;
}

2.远程服务器读取流
复制代码 代码如下:

_context = httpcontext.current;
stream stream = _context.request.inputstream; //获取当前传入http输入流
long length = stream.length;
byte[] data = _context.request.binaryread((int)length);//对当前输入流进行指定字节数的二进制读取
string strcontent = encoding.utf8.getstring(data);//解码为utf8编码形式的字符串

代码讲解到此结束,一些相关补充:
1.httpwebrequest对象有一些相关设置属性,如method(发送方式),timeout(请求超时时间),contenttype(http标头的值)等等。
2.若远程接收页面出错,该如何调试,很简单,只需写入下面的代码:
复制代码 代码如下:

httpwebresponse res = null;
webresponse response = null;
try
{
webresponse response = webrequest.getresponse();
}
catch (webexception ex1)
{
res = (httpwebresponse)ex1.response;
}
finally
{
streamreader sr = new streamreader(res.getresponsestream(), encoding.utf8);
string strhtml = sr.readtoend();
httpcontext.current.response.write(strhtml);
}

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