[robot]送出HttpWebRequest以及接收Response(get,post)

摘要:[robot]送出HttpWebRequest(get,post)

以ie为例,观察fiddler之后的范例

GET:

原始的fiddler的raw数据:

GET http://w2.land.taipei.gov.tw/land4/loina.asp HTTP/1.1

Accept: text/html, application/xhtml+xml, */*

Accept-Language: zh-Hant-TW,zh-Hant;q=0.8,en-US;q=0.5,en;q=0.3

User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko

Accept-Encoding: gzip, deflate

Host: w2.land.taipei.gov.tw

DNT: 1

Proxy-Connection: Keep-Alive

Cookie: ASPSESSIONIDQQDQDBBB=KPCIMLIBOFBMJBGAMPIBKAPL

?

C#:


HttpWebRequest request;
CookieContainer cookies = new CookieContainer();
string url = "http://w2.land.taipei.gov.tw/land4/loina.asp";

string html = "";

request = WebRequest.Create(url) as HttpWebRequest;
//如果需要使用proxy的话....
WebProxy _proxy = new WebProxy("http://myproxy.com.tw:8888", true);
_proxy.Credentials = CredentialCache.DefaultCredentials;
request.Proxy = _proxy;
//end of proxy
request.Method = "GET";
request.Accept = "text/html, application/xhtml+xml, */*";
request.Headers.Set("Accept-Language", "zh-Hant-TW,zh-Hant;q=0.8,en-US;q=0.5,en;q=0.3");
request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko";
//client跟server说,我要使用的加密方式是gzip,server会看设定才决定是否采用
request.Headers.Set("Accept-Encoding", "gzip, deflate");
//如果对方回传的数据有用gzip加密的话,会自动用gzip方式解开, 没加这行的话,可能解不开
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
request.Host = "w2.land.taipei.gov.tw";
request.CookieContainer = cookies;
//以下这是默认值true, 有时候故意设定为false,就会抓不到html啰
request.KeepAlive = true;

using (var response = (HttpWebResponse)request.GetResponse())
{
	using (var responseStream = response.GetResponseStream())
	{
		using (var reader = new StreamReader(responseStream, Encoding.Default))
		{
			html = reader.ReadToEnd();
		}
	}
}          

ps.20161009补充chrome的参考程序:

string result = "";
HttpWebRequest request;
CookieContainer cookies = new CookieContainer();
string url = "www.yoururl.com";
request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
request.Headers.Set("Accept-Encoding", "gzip, deflate, sdch");
request.Headers.Set("Accept-Language", "zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4,zh-CN;q=0.2");
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36";
request.CookieContainer = cookies;
request.Headers.Set("Upgrade-Insecure-Requests", "1");
//以下这是默认值true, 有时候故意设定为false,就会抓不到html啰
request.KeepAlive = true;

using (var response = (HttpWebResponse)request.GetResponse())
{
	using (var responseStream = response.GetResponseStream())
	{
		using (var reader = new StreamReader(responseStream, Encoding.UTF8))
		{
			result = reader.ReadToEnd();
		}
	}
}     

return result;

POST:

fiddler原始raw数据:

POST http://w2.land.taipei.gov.tw/land4/loina.asp HTTP/1.1

Accept: text/html, application/xhtml+xml, */*

Referer: http://w2.land.taipei.gov.tw/land4/loina.asp

Accept-Language: zh-Hant-TW,zh-Hant;q=0.8,en-US;q=0.5,en;q=0.3

User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko

Content-Type: application/x-www-form-urlencoded

Accept-Encoding: gzip, deflate

Proxy-Connection: Keep-Alive

Content-Length: 40

DNT: 1

Host: w2.land.taipei.gov.tw

Pragma: no-cache

Cookie: ASPSESSIONIDQQDQDBBB=KPCIMLIBOFBMJBGAMPIBKAPL

?

destrict=03&section=&land_mom=&land_son=

?

C#:


HttpWebRequest requestPost;
CookieContainer cookiesPost = new CookieContainer();
requestPost = WebRequest.Create(url) as HttpWebRequest;
string html = "";
string postData = "destrict=03§ion=&land_mom=&land_son=";//行政区选择中正区, 有特殊符记得HttpUtility.UrlEncode
requestPost.Method = "POST";
requestPost.Accept = "text/html, application/xhtml+xml, */*";
requestPost.Referer = "http://w2.land.taipei.gov.tw/land4/loina.asp";
requestPost.Headers.Set("Accept-Language", "zh-Hant-TW,zh-Hant;q=0.8,en-US;q=0.5,en;q=0.3");
requestPost.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko";
requestPost.ContentType = "application/x-www-form-urlencoded";
requestPost.Headers.Set("Accept-Encoding", "gzip, deflate");
requestPost.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
requestPost.ContentLength = postData.Length;
requestPost.Host = "w2.land.taipei.gov.tw";
requestPost.Headers.Set("Pragma", "no-cache");
requestPost.CookieContainer = cookiesPost;
//碰到(417) Expectation Failed错误的时候,把下面这行加上去
//System.Net.ServicePointManager.Expect100Continue = false;

using (var stream = requestPost.GetRequestStream())
{
	using (var writer = new StreamWriter(stream))
	{
		writer.Write(postData.ToString());
		writer.Flush();
		writer.Close();
	}
	stream.Close();
}

using (var response = (HttpWebResponse)requestPost.GetResponse())
{
	using (var responseStream = response.GetResponseStream())
	{
		using (var reader = new StreamReader(responseStream, Encoding.Default))
		{
			html = reader.ReadToEnd();
		}
	}
}

ps.记得如果重复使用request变量的话,每次都要重新设定Method,Accept,Referer,Accept-Language....

因为.net在每次送出request之后,会把上述header都reset掉

补充20151116:

一般来说,在网站要抓东西时,常常是连续好几个request + response才能取得目目标数据,每一次每一次的request都要重新

设定相关header条件喔,例如不能偷懒只设定第一次request的request.Accept?=?"text/html,?application/xhtml+xml,?*/*";

这样子是抓不出数据来的...因为.net的默认似乎会将上一次的request设定的header的内容清空

(你看debug模式显示的变量状态,是显示没清空的,但是你如果只设定第一次request的header,事实上就是完全查不出数据喔)

除此之外,cookies也一定每次都要带入喔,因为连续的request的状态的连接,有时候是用ViewState,有时候是用cookies

ps.补充20151118:ViewState, ViewStateGenerator, EventValidation这三个参数会在传统的asp.net web form出现,如果出现的话,三个要一起改

ps.补充20160325:如果要改用其他浏览器(例如:chrome),再利用fiddler观察request以及response的内容与上面文章的差异之后,改成其他浏览器的header即可

PS. 补充20170720:如果是https且必需为TLS1.2较高等级的传输加密的话,需加上以下喔:(需要在电脑安装framework4.5才能正常运行喔,程序项目的版本设定为4.0 or 4.5都可以)

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
ServicePointManager.DefaultConnectionLimit = 9999;

原文:大专栏  [robot]送出HttpWebRequest以及接收Response(get,post)

原文地址:https://www.cnblogs.com/petewell/p/11526686.html

时间: 2024-07-30 08:08:44

[robot]送出HttpWebRequest以及接收Response(get,post)的相关文章

异步4月新书,送出一本你爱的!

点击关注 异步图书,置顶公众号 每天与你分享 IT好书 技术干货 职场知识 参与文末话题讨论,每日赠送异步图书 --异步小编 4月工作日的第一天,小编带来了10本新书,包含关键词Python.神经网络.C语言.iOS开发.数据科学.git.OpenSctck.Joomla.Oracle等. <Python神经网络编程 > [英] 塔里克·拉希德(Tariq Rashid) 著 点击封面购买纸书 当前,深度学习和人工智能的发展和应用给人们留下了深刻的印象.神经网络是深度学习和人工智能的关键元素,

Observable 的 Operators集合

内容为整理博主文章:https://juejin.im/user/58870f04128fe10065efc8d9/article 个人觉得他对Operators的解说较容易理解和全面,顾把它们整理在一起,也方面查找. Operators:Observable 的 Operators 是实例应用上最重要的部份,我们需要了解各种 Operators 的使用方式,才能轻松实现各种需求!Operators 就是一个个被附加到 Observable 型别的函数. Marble diagrams:我们把描

深度分析Linux下双网卡绑定七种模式

现在一般的企业都会使用双网卡接入,这样既能添加网络带宽,同时又能做相应的冗余,可以说是好处多多.而一般企业都会使用linux操作系统下自带的网卡绑定模式,当然现在网卡产商也会出一些针对windows操作系统网卡管理软件来做网卡绑定(windows操作系统没有网卡绑定功能 需要第三方支持).进入正题,linux有七种网卡绑定模式:0. round robin,1.active-backup,2.load balancing (xor), 3.fault-tolerance (broadcast),

Response.Write(&quot;&lt;script&gt;alert(&#39;弹出对话框!&#39;)&lt;/script&gt;&quot;) 后跟Response.Redirect(&quot;page.aspx&quot;);不能弹出对话框,直接跳转页面了 如何解?

Response.Write和Response.Redirect一起用的时候就会这样,write脚本和redirect脚本不能同时使用,这样不会执行脚本,最好使用ClientScript 改进方法: Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language=javascript >alert('弹出对话框!');</script>");

极光推送实战感受

最近公司的项目新增一个消息中心的功能,使用的是极光推送,现在项目上线了,分享一下做这个功能的感受.写的不好,希望大家多多指正 第一步: 集成极光SDK 集成极光文档主要的还是按照极光文档一步步来,导入一些系统框架,添加极光的.a文件和极光头文件,然后在相应的地方添加代码.添加代码极光文档都写的很详细,极光也有案例,可以参考案例 第二步:创建应用,或者appKey 创建应用需要到登陆自己的极光账号(没有注册一个),到控制台创建一个应用,填写应用相应的信息.其中涉及到开发环境和开发环境的p12文件,

iOS 10 消息推送(UserNotifications)秘籍总结(一)

背景 iOS10 新特性一出,各个大神就早已研究新特性能给场景智能化所带来的好处(唉,可惜我只是一个小白).我也被安排适配iOS10的推送工作! Apple 表示这是 iOS 有史以来最大的升级(our biggest release yet),更加智能开放的 Siri .强化应用对 3D Touch 支持. HomeKit .电话拦截及全新设计的通知等等… iOS 10 中将之前繁杂的推送通知统一成UserNotifications.framework 来集中管理和使用通知功能,还增加一些实用

笔记4:HttpWebRequest 与 HttpWebResponse 类

网页的请求与响应均通过HTTP来完成,HTTP是Web最重要的基础核心. HttpWebRequest与HttpWebResponse通过HTTP协议,完成网络"请求/响应"模型架构所需的功能,同时存取网页上各种特定元素. 一.HTTP通信协议 HTTP组成格式主要包含http标题及http主体,这两个部分请求与响应的消息有差别. 客户端向服务器端所发出的HTTP请求消息,其标题包含了一些重要的信息如: (同样服务器响应客户端也会有消息,也有头部信息) Server:Microsoft

iOS开发 iOS10推送必看

iOS10更新之后,推送也是做了一些小小的修改,下面我就给大家仔细说说.希望看完我的这篇文章,对大家有所帮助. 一.简单入门篇---看完就可以简单适配完了 相对简单的推送证书以及环境的问题,我就不在这里讲啦,我在这里说的,是指原有工程的适配. 1.首先我们需要打开下面的开关.所有的推送平台,不管是极光还是什么的,要想收到推送,这个是必须打开的哟~ QQ20160914-4.png 之后,系统会生成一个我们以前没见过的文件,如图: QQ20160918-0.png-5.8kB QQ20160918

Android高效率编码-第三方SDK详解系列(三)——JPush推送牵扯出来的江湖恩怨,XMPP实现推送,自定义客户端推送

Android高效率编码-第三方SDK详解系列(三)--JPush推送牵扯出来的江湖恩怨,XMPP实现推送,自定义客户端推送 很久没有更新第三方SDK这个系列了,所以更新一下这几天工作中使用到的推送,写这个系列真的很要命,你要去把他们的API文档大致的翻阅一遍,而且各种功能都实现一遍,解决各种bug各种坑,不得不说,极光推送真坑,大家使用还是要慎重,我们看一下极光推送的官网 https://www.jpush.cn/common/ 推送比较使用,很多软件有需要,所以在这个点拿出来多讲讲,我们本节