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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 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数据,如果没有此代码,即时登录成功,转到应用程序其它页面时,也会提示没有登录(因为验证不了登录信息)。如果只是提交表单数据,第二段代码可以省去。
获取到数据后,如何处理就不是这里的重点了。