Http请求资源方式WebRequest.GetResponse()怎么做到监控

HttpWebRequest.GetResponse 方法
(System.Net)
GetResponse 方法
要查看英语原文,请勾选“英语”复选框。也可将鼠标指针移到文本上,在弹出窗口中显示英语原文。
本文档已存档,并且将不进行维护。
HttpWebRequest.GetResponse 方法
Visual Studio 2010
返回来自 Internet 资源的响应。
命名空间:
System(在 System.dll 中)
abstract GetResponse : unit -& WebResponse
override GetResponse : unit -& WebResponse
类型:。异常条件 调用使用。- 或 -
被设置为一个值,并且
为 false。
为 GET 或 HEAD,并且或者
大于或等于零,或者
为 true。- 或 -
为 true, 为 false, 为 -1, 为 false, 为 POST 或 PUT。- 或 - 具有实体,但不用调用
方法调用 GetResponse 方法。- 或 - 大于零,但应用程序不会写入所有承诺的数据。请求缓存验证程序指示对此请求的响应可从缓存中提供;但是,此请求包含要发送到服务器的数据。
发送数据的请求不得使用缓存。
如果您正在使用错误实现的自定义缓存验证程序,则会发生此异常。
以前被调用过。- 或 -- 或 -处理请求时发生错误。
方法返回包含来自 Internet 资源的响应的
,并且能够转换为访问 HTTP 特定的属性的类。
类设置的属性发生冲突时将引发 。
属性设置为 true,然后发送 HTTP GET 请求,则会引发该异常。
如果应用程序尝试向仅支持 HTTP 1.0 协议而不支持分块请求的服务器发送分块请求,则会引发该异常。
属性就尝试发送数据,或者在 keepalive 连接( 属性为 true)上禁用缓冲时
为 false,则会引发该异常。
警告 方法关闭该流并释放连接。
如果未能做到这一点,可能导致应用程序用完连接。
使用 POST 方法时,必须获取请求流,写入要发送的数据,然后关闭请求流。
注意多次调用 GetResponse 会返回相同的响应对象;该请求不会重新发出。注意应用程序不能对特定请求混合使用同步和异步方法。
方法,则必须使用 GetResponse 方法检索响应。
注意,请使用该异常的
属性确定服务器的响应。注意
注意为安全起见,默认情况下禁用 Cookie。
属性启用 Cookie。
下面的代码示例获取请求的响应。
目前没有可用的代码示例或不支持该语言。
受以下版本支持:4、3.5、3.0、2.0、1.1、1.0受以下版本支持:4、3.5 SP1Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2
.NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见。用C#代码 get请求获取网页响应代码,通过HttpWebRequest的GetResponse()方法报异常,我想获取这相应代码_百度知道
用C#代码 get请求获取网页响应代码,通过HttpWebRequest的GetResponse()方法报异常,我想获取这相应代码
如何实现呢?
是响应代码
答题抽奖
首次认真答题后
即可获得3次抽奖机会,100%中奖。
我不知道你的代码是什么样的出了异常,给你一段代码供参考:
string sRet = &&;
//目标网址事先存在字符串 sPage 中
HttpWebRequest Myrq = (HttpWebRequest)HttpWebRequest.Create(sPage );
Myrq.UserAgent = &Mozilla/4.0 ( MSIE 7.0; Windows NT 5.1; Trident/4.0; EmbeddedWB 14.52 from:
EmbeddedWB 14.52; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.; .NET CLR 3.5.21022)&;
Myrq.Accept = &*/*&;
Myrq.Method = &GET&;
Myrq.Headers.Add(HttpRequestHeader.AcceptLanguage, &zh-cn&);
Myrq.Headers.Add(HttpRequestHeader.AcceptEncoding, &gzip,deflate&);
Myrq.KeepAlive =
//Myrq.Referer =//有些网址要求指定referer
//有些网页需要cookie的
//Myrq.Headers.Add(HttpRequestHeader.Cookie, sCookie);
HttpWebResponse myrp = (HttpWebResponse)Myrq.GetResponse();
long totalBytes = myrp.ContentL
if (!string.IsNullOrEmpty(myrp.CharacterSet))
ec = Encoding.GetEncoding(myrp.CharacterSet);//如果响应的网页头有指定编码就用指定编码,否则采用默认的GB2312编码
ec = Encoding.GetEncoding(&GB2312&);
Stream st = myrp.GetResponseStream();
using (StreamReader sr = new StreamReader(st,ec ))
sRet = sr.ReadToEnd();
st.Close();
//Console.WriteLine(sRet);
catch (WebException ex)
Console.WriteLine(ex.ToString());
采纳率:65%
为您推荐:
其他类似问题
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。订阅你的位置: >
> 【已解决】C#中HttpWebRequest的GetRequestStream执行的效率太低,甚至偶尔死掉
【问题】C#中,提交对应的POST类型http请求之前,会执行:Stream postDataStream = req.GetRequestStream();然后填充对应的post数据,再提交http的请求。但是调试的时候,发现每次执行GetRequestStream都很慢。慢也就算了,结果最近发现,某次,执行req.GetRequestStream();死掉。【解决过程】1.网上找了找,根据  的解释,说是.NET每次会自动搜索代理,所以很慢,此处没有设置代理的话,应该直接复制为空:req.Proxy =然后再去执行GetRequestStream,这样就不会慢了。然后去试了试,好像是这个效果。2.结果第二天同样的代码,去调试,结果GetRequestStream的地方,还是死掉了。原因暂未知。3.后来的情况是,同样的代码,再次运行,就不死了,虽然也还是有时候效率低,反应慢,但是不会死了。具体根本原因,感觉还是没有找到。4. 后来的多次调试,发现对于:req.Proxy =好像也没啥效果。只是碰巧找到了,关于设置proxy为null的原始的官方的解释:”C# 3.0 in a Nutshell, Third Edition: A Desktop Quick Reference“的:WarningIf you don’t have a proxy, you must set the Proxy property to null on all WebClient and WebRequest objects. Otherwise, the Framework may attempt to &auto-detect& your proxy settings, adding up to 30 seconds to your request. If you’re wondering why your web requests execute slowly, this is probably it!但是此处,对于我所遇到的,执行GetRequestStream会死掉的问题,还是没有啥帮助。5.多次调试得到的结果是,如果是对于:Stream postDataStream = req.GetRequestStream();是单步调试的话,好像很少会死掉.而如果直接运行,没有打断点,那么经常容易死掉.6.参考:,去试了:using (Stream postDataStream = req.GetRequestStream())
postDataStream.Write(postBytes, 0, postBytes.Length);
postDataStream.Close();
}还是会死掉。7.网上看到:,看起来此问题,好像已经是官方承认的一个bug。不过即使真的是bug,其解释说是在.NET 4.0 beta2才解决掉,而我此处无法用.NET 4.0,只能用.NET 2.0/3.0/3.5,所以,如果真是bug的话,那就真的悲剧了。。。还是先去看看,有没有其他解决办法再说吧。 8.试了如下代码:try
Stream postDataStream = req.GetRequestStream();
postDataStream.Write(postBytes, 0, postBytes.Length);
postDataStream.Close();
catch (WebException ex)
MessageBox.Show(ex.Message);
}单步调试了:Stream postDataStream = req.GetRequestStream();发现经过很长的时间之后,捕获到了异常WebException ex,出现了“Unable to connect to the remote server”错误,其详细信息如下:ex {&Unable to connect to the remote server&} System.Net.WebException
base {&Unable to connect to the remote server&} System.InvalidOperationException {System.Net.WebException}
base {&Unable to connect to the remote server&} System.SystemException {System.Net.WebException}
base {&Unable to connect to the remote server&} System.Exception {System.Net.WebException}
_className null string
_data null System.Collections.IDictionary
_dynamicMethods null object
_exceptionMethod null System.Reflection.MethodBase
_exceptionMethodString null string
_helpURL null string
_HResult - int
_innerException {&A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 65.55.254.42:443&} System.Exception {System.Net.Sockets.SocketException}
_message &Unable to connect to the remote server& string
_remoteStackIndex 0 int
_remoteStackTraceString null string
_source null string
_stackTrace {sbyte[96]} object {sbyte[]}
_stackTraceString null string
_xcode - int
_xptrs 0 System.IntPtr
Data {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
HelpLink null string
HResult - int
InnerException {&A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 65.55.254.42:443&} System.Exception {System.Net.Sockets.SocketException}
IsTransient false bool
Message &Unable to connect to the remote server& string
Source &System& string
StackTrace &
at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)\r\n
at System.Net.HttpWebRequest.GetRequestStream()\r\n
at InsertSkydriveFiles.skydrive.beforeUploadFile()& string
TargetSite {System.IO.Stream GetRequestStream(System.Net.TransportContext ByRef)} System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}
Static members
InternalStatus ServicePointFatal System.Net.WebExceptionInternalStatus
m_InternalStatus ServicePointFatal System.Net.WebExceptionInternalStatus
m_Response null System.Net.WebResponse
m_Status ConnectFailure System.Net.WebExceptionStatus
Response null System.Net.WebResponse
Status ConnectFailure System.Net.WebExceptionStatus此处觉得很不能理解的事情是,我只是调用GetRequestStream,获得request的stream,又不是来自服务器端的response,为何也要去链接remote server,而不是本地调用一个库函数,获得对应的stream??? 【后记 】后来发现,好像此问题,和GetResponse偶尔死掉,是同一个原因,然后也解决了这个问题,具体过程和办法,请参考:转载请注明: & 与本文相关的文章
13 queries in 0.190 seconds, using 10.30MB memoryWebRequest 如何获取返回的 HTTP 状态码
使用 HttpWebResponse,而不是 WebResponse。
WebRequest request = WebRequest.Create(url);HttpWebResponse response = (HttpWebResponse)request.GetResponse();Console.Write(Convert.ToInt32(response.StatusCode) + " " + response.StatusCode.ToString() + "\r\n");
这里 StatusCode 是 System.Net.HttpStatusCode,System.Net.HttpStatusCode 是一个:所以应该转换为 int 类型,得到的才是 200、404 这样的数字;否则得到的是&OK、NotFound 这样的名称。
HttpWebResponse 除了有 StatusCode 属性外,还有个 StatusDescription。
本站永远终止与捏造“罪名”不支付广告费的某度联盟合作。
作者简介:
vkvi,致力于 .NET Web 开发、移动开发的技术推广,在 .NET、SQL Server、Windows Server 等方面有深入研究和丰富经验,10 年间共计撰写文章 4000 余篇。
主持金融、国土、农业、电商等多个行业项目执行,
推行“技术提升生产力、人心决定成功率”的管理理论。}

我要回帖

更多关于 Http作用 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信