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

推荐订阅源

C
CERT Recently Published Vulnerability Notes
G
Google Developers Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
宝玉的分享
宝玉的分享
Microsoft Security Blog
Microsoft Security Blog
Jina AI
Jina AI
L
LangChain Blog
博客园_首页
有赞技术团队
有赞技术团队
The Register - Security
The Register - Security
GbyAI
GbyAI
Blog — PlanetScale
Blog — PlanetScale
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
J
Java Code Geeks
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Security Archives - TechRepublic
Security Archives - TechRepublic
量子位
雷峰网
雷峰网
Security Latest
Security Latest
博客园 - 【当耐特】
V2EX - 技术
V2EX - 技术
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
IT之家
IT之家
爱范儿
爱范儿
S
Schneier on Security
N
News | PayPal Newsroom
H
Help Net Security
Recent Announcements
Recent Announcements
Martin Fowler
Martin Fowler
N
News and Events Feed by Topic
C
Cyber Attacks, Cyber Crime and Cyber Security
U
Unit 42
博客园 - 司徒正美
Forbes - Security
Forbes - Security
P
Proofpoint News Feed
W
WeLiveSecurity
Cisco Talos Blog
Cisco Talos Blog
小众软件
小众软件
The Cloudflare Blog
AWS News Blog
AWS News Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
P
Palo Alto Networks Blog
Google DeepMind News
Google DeepMind News
H
Heimdal Security Blog
V
Vulnerabilities – Threatpost
Microsoft Azure Blog
Microsoft Azure Blog
T
Tailwind CSS Blog
G
GRAHAM CLULEY

博客园 - yulei

几个css技巧 jquery selector 基础 - yulei asp.net ajax实现:Jquery+Json - yulei - 博客园 smtp协议 发送带附件的邮件 控制滚动条 (转)防数据库批量注入JS 转载 JS获取CSS属性值 - yulei - 博客园 HttpCompress相关问题解决方法 - yulei - 博客园 C#发送Email邮件方法总结 数据导出到excel文件给客户端下载的几种方法(转) ndoc2007,生成注释文档,支持泛型,2.0,中文注解,部分汉化 IE和Firefox浏览器CSS网页布局不同点 - yulei - 博客园 通用获得网页的实际大小的javascript函数 仿DVBBS下拉菜单效果 webClient下载进度条 asp.net下URL网址重写成.html格式、RSS、OPML的知识总结 导出excel 使用AD724的RGB→NTSC/PAL制信号转换电路 AV-RF转换器的制作
HttpWebRequest 发送 POST实现自动用户登录 - yulei
yulei · 2010-04-22 · via 博客园 - yulei

怎样通过HttpWebRequest 发送 POST 请求到一个网页服务器?例如编写个程序实现自动用户登录,自动提交表单数据到网站等。
假 如某个页面有个如下的表单(Form):

  1. <form name="form1" action="http:www.breakn.com/login.asp" method="post">  
  2. <input type="text" name="userid" value="">  
  3. <input type="password" name="password" value="">  
  4. </form>  

从表单可看到表单有两个表单域,一个是userid另一个是password,所以以POST形式提交的数据应该 包含有这两项。
其中POST的数据格式为:
表单域名称1=值1&表单域名称2=值2&表单域名称3=值3……
要 注意的是“值”必须是经过HTMLEncode的,即不能包含“<>=&”这些符号。

本例子要提交的数据应该是:
userid=value1&password=value2

用 C#写提交程序:

  1. string strId = "guest";  
  2. string strPassword= "123456";  
  3.   
  4. ASCIIEncoding encoding=new ASCIIEncoding();  
  5. string postData="userid="+strId;  
  6. postData += ("&password="+strPassword);  
  7.   
  8. byte[] data = encoding.GetBytes(postData);  
  9.   
  10. // Prepare web request...  
  11. HttpWebRequest myRequest =  
  12. (HttpWebRequest)WebRequest.Create("http:www.here.com/login.asp");  
  13.   
  14. myRequest.Method = "POST";  
  15. myRequest.ContentType="application/x-www-form-urlencoded";  
  16. myRequest.ContentLength = data.Length;  
  17. Stream newStream=myRequest.GetRequestStream();  
  18.   
  19. // Send the data.  
  20. newStream.Write(data,0,data.Length);  
  21. newStream.Close();  
  22.   
  23. // Get response  
  24. HttpWebResponse myResponse=(HttpWebResponse)myRequest.GetResponse();  
  25. StreamReader reader = new StreamReader(response.GetResponseStream(),Encoding.Default);  
  26. string content = reader.ReadToEnd();  
  27. Console.WriteLine(content);