HttpWebRequest 下载网页Html代码 POST方式(站内使用了form方式) System.Net.WebException (417) Expectation failed
2011-05-31 16:49
1626 查看
上一篇说了Get方式可以处理一般的站内搜索,假如使用了表单方式,用Get方式就获取不了。
例如:
网站站内搜索界面:
对就源码:
可以看出使用了form的post,action指向search.html,其中表单域是key;
Post的提交数据格式如下:
所以提交请求应该是:key=key值&proClass=&x=26&y=15灰色部分也可以不用。注意key值需要经过编码,不要包含了&等特殊符号。
实现的代码如下:
在这里如果不使用ServicePointManager.Expect100Continue = false;这段代码时,以上方法执行至GetResponse()就会产生如下错误信息:
System.Net.WebException: 远程服务器返回错误: (417) Expectation failed。
其中在msdn上有如下说明:
如果 POST[/b] 请求需要 100-Continue 响应,则为 true[/b];否则为 false[/b]。默认值为 true[/b]。详情请进入Expect100Continue查阅!
在上也有如下说明:
Requirements for HTTP/1.1 clients: - If a client will wait for a 100 (Continue) response before sending the request body, it MUST send an Expect request-header field with the "100-continue" expectation.
- A client MUST NOT send an Expect request-header field with the "100-continue" expectation if it does not intend to send a request body.
详情请进入HTTP/1.1查阅。
例如:
网站站内搜索界面:
对就源码:
可以看出使用了form的post,action指向search.html,其中表单域是key;
Post的提交数据格式如下:
所以提交请求应该是:key=key值&proClass=&x=26&y=15灰色部分也可以不用。注意key值需要经过编码,不要包含了&等特殊符号。
实现的代码如下:
string searchKey = "搜索内容"; ASCIIEncoding encoding = new ASCIIEncoding(); string postData = "key=" + System.Uri.EscapeUriString(searchKey); postData += "&proClass=&x=0&y=0"; ServicePointManager.Expect100Continue = false; byte[] data = encoding.GetBytes(postData); HttpWebRequest hwrequest = (HttpWebRequest)WebRequest.Create("http://www.website.net/search.html"); hwrequest.Method = "POST"; hwrequest.ContentType = "application/x-www-form-urlencoded"; hwrequest.ContentLength = data.Length; Stream stream = hwrequest.GetRequestStream(); stream.Write(data, 0, data.Length); stream.Close(); HttpWebResponse hwresponse = (HttpWebResponse)hwrequest.GetResponse(); StreamReader sr = new StreamReader(hwresponse.GetResponseStream(), Encoding.UTF8); string content = sr.ReadToEnd();
在这里如果不使用ServicePointManager.Expect100Continue = false;这段代码时,以上方法执行至GetResponse()就会产生如下错误信息:
System.Net.WebException: 远程服务器返回错误: (417) Expectation failed。
其中在msdn上有如下说明:
如果 POST[/b] 请求需要 100-Continue 响应,则为 true[/b];否则为 false[/b]。默认值为 true[/b]。详情请进入Expect100Continue查阅!
在上也有如下说明:
Requirements for HTTP/1.1 clients: - If a client will wait for a 100 (Continue) response before sending the request body, it MUST send an Expect request-header field with the "100-continue" expectation.
- A client MUST NOT send an Expect request-header field with the "100-continue" expectation if it does not intend to send a request body.
详情请进入HTTP/1.1查阅。
相关文章推荐
- wp7使用POST请求Asp.net网页时,HttpWebRequest的BeginGetResponse方法抛出NotSupportException异常的解决方法
- HttpWebRequest 下载网页Html代码 下载文件(Remote和FTP)Get方式
- asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- 【转】asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- WP7中HttpWebRequest的使用方法之POST方式
- ASP.NET 4.0验证请求 System.Web.HttpRequestValidationException: A potentially dangerous Request.F
- C# 模拟post数据提交时 出现如下错误: System.Net.WebException: 远程服务器返回错误: (417) Expectation Failed 的解决办法
- js_html_input中autocomplete="off"在chrom中失效的解决办法 使用JS模拟锚点跳转 js如何获取url参数 C#模拟httpwebrequest请求_向服务器模拟cookie发送 实习期学到的技术(一) LinqPad的变量比较功能 ASP.NET EF 使用LinqPad 快速学习Linq
- 不能以方法的方式使用不可调用的“system.web.httprequest.querystring”
- 使用HttpWebRequest的POST取得网页内容
- 使用HttpWebRequest的POST取得网页内容(异步操作)
- C# post数据时 出现如下错误: System.Net.WebException: 远程服务器返回错误: (417) Expectation Failed 的解决办法
- ASP.NET使用HttpWebRequest读取远程网页源代码
- 使用HttpWebRequest的POST取得网页内容
- System.Web.HttpRequestValidationException: 从客户端(&md.)中检测到有潜在危险的 Request.Form 值
- System.Web.HttpRequestValidationException: 从客户端(dbFlag="<soap:Envelope xmlns...")中检测到有潜在危险的 Request.Form 值。
- C#中网页文件下载——HttpWebRequest与HttpWebResponse的使用方法
- 用System.Web.HttpRequest模拟一个form的post
- NET(C#):使用HttpWebRequest头中的Range下载文件片段
- 【WitchHat.cn心得】Silverlight中,即使使用HttpWebRequest也抛System.Security.SecurityException