惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Securelist
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
WordPress大学
WordPress大学
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Tailwind CSS Blog
V
V2EX
小众软件
小众软件
博客园 - 聂微东
H
Help Net Security
阮一峰的网络日志
阮一峰的网络日志
云风的 BLOG
云风的 BLOG
Blog — PlanetScale
Blog — PlanetScale
M
MIT News - Artificial intelligence
人人都是产品经理
人人都是产品经理
F
Fortinet All Blogs
S
Schneier on Security
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
Vercel News
Vercel News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Google DeepMind News
Google DeepMind News
Google Online Security Blog
Google Online Security Blog
Webroot Blog
Webroot Blog
A
Arctic Wolf
量子位
博客园 - 叶小钗
I
Intezer
C
Check Point Blog
Cloudbric
Cloudbric
IT之家
IT之家
Last Week in AI
Last Week in AI
GbyAI
GbyAI
Attack and Defense Labs
Attack and Defense Labs
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
Jina AI
Jina AI
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cisco Blogs
J
Java Code Geeks
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Engineering at Meta
Engineering at Meta
酷 壳 – CoolShell
酷 壳 – CoolShell
L
Lohrmann on Cybersecurity
有赞技术团队
有赞技术团队
Simon Willison's Weblog
Simon Willison's Weblog
The Register - Security
The Register - Security
T
Threatpost

博客园 - Allen Zhang

oracle数据库的字符集更改 用SQL server导出到oracle,查询时提示“表或视图不存在ORA-00942”错误 由于无法创建应用程序域,因此未能执行请求。错误: 0x80070002 系统找不到指定的文件 导出Excel时发生COM组件失败的解决方案 jquery下一个空格带来的血案 Web应用系统中关闭Excel进程 C++数据类型与C#对应关系 c#调用WINDWOS API时,非常有用(转) 水晶报表的交叉表中增加超级链接 JavaScript和ExtJS的继承 Ext.extend Ext.applyIf (转) SQL SERVER 2000数据库置疑处理 ScriptX控件部分参数说明 - Allen Zhang - 博客园 给ASP.net程序配置代理服务器 使用jQuery的validation后,无法引发asp.net按钮的事件处理程序 C#调用Delphi写的动态链接库 - Allen Zhang - 博客园 近期一些超值文章及工具收集 用STSADM工具全自动备份sharepoint站点 创建MOSS网站下的新闻 SQL Server Reporting Services与MOSS集成资料汇总 修复MOSS2007备份还原后搜索服务出现的问题(续) - Allen Zhang - 博客园
使用代码模拟请求和提交网页数据 - Allen Zhang - 博客园
Allen Zhang · 2008-04-25 · via 博客园 - Allen Zhang

通过程序代码去模拟请求和提交网页表单数据,这里我使用的是一个登录方法。代码如下:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@http://www.tempuri.com/checkLogin);
string parms = string.Format("txtUser={0}&txtPass={1}&submit=", _username, _password);
byte[] data = System.Text.Encoding.GetEncoding("GB2312").GetBytes(parms);

//加入以下两句代码,能保留登录产生的Session数据,转到其它页时不会提示登录不成功
CookieContainer cookie = new CookieContainer();
request.CookieContainer 
= cookie;

//设置提交格式为POST
request.Method = "POST";
request.ContentType 
= "application/x-www-form-urlencoded";
request.ContentLength 
= data.Length;

Stream writer 
= request.GetRequestStream();
writer.Write(data, 
0, data.Length);
writer.Close();

//获取请求回复数据
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader 
= new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312"));
string content = reader.ReadToEnd();

return content;

这里值得注意的是几点:
1、在WebRequest.Create中使用的参数url必须是表单action的地址,即实际的提交地址。
2、提交参数,如上面第2行,一定要把表单中的文本框等收集清楚,那些hidden的input也要写上,并且保持名称统一
3、如果是模拟登录,在登录的过程中一般的应用程序都会向cookie或Session中写入数据,上面的第二段代表就是用于保留Session数据,如果没有此代码,即时登录成功,转到应用程序其它页面时,也会提示没有登录(因为验证不了登录信息)。如果只是提交表单数据,第二段代码可以省去。
获取到数据后,如何处理就不是这里的重点了。