zoukankan
html css js c++ java
C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
怎样通过HttpWebRequest 发送 POST 请求到一个网页服务器?例如编写个程序实现自动用户登录,自动提交表单数据到网站等。
假如某个页面有个如下的表单(Form):
<
form name
=
"
form1
"
action
=
"
http://www.breakn.com/login.asp
"
method
=
"
post
"
>
<
input type
=
"
text
"
name
=
"
userid
"
value
=
""
>
<
input type
=
"
password
"
name
=
"
password
"
value
=
""
>
</
form
>
从表单可看到表单有两个表单域,一个是userid另一个是password,所以以POST形式提交的数据应该包含有这两项。
其中POST的数据格式为:
表单域名称1
=
值1
&
表单域名称2
=
值2
&
表单域名称3
=
值3……
要注意的是“值”必须是经过HTMLEncode的,即不能包含“
<>=&
”这些符号。
本例子要提交的数据应该是:
userid
=
value1
&
password
=
value2
用C#写提交程序:
string
strId
=
"
guest
"
;
string
strPassword
=
"
123456
"
;
ASCIIEncoding encoding
=
new
ASCIIEncoding();
string
postData
=
"
userid=
"
+
strId;
postData
+=
(
"
&password=
"
+
strPassword);
byte
[] data
=
encoding.GetBytes(postData);
//
Prepare web request
HttpWebRequest myRequest
=
(HttpWebRequest)WebRequest.Create(
"
http://www.here.com/login.asp
"
);
myRequest.Method
=
"
POST
"
;
myRequest.ContentType
=
"
application/x-www-form-urlencoded
"
;
myRequest.ContentLength
=
data.Length;
Stream newStream
=
myRequest.GetRequestStream();
//
Send the data.
newStream.Write(data,
0
,data.Length);
newStream.Close();
//
Get response
HttpWebResponse myResponse
=
(HttpWebResponse)myRequest.GetResponse();
StreamReader reader
=
new
StreamReader(response.GetResponseStream(),Encoding.Default);
string
content
=
reader.ReadToEnd();
Response.Write(content);
查看全文
相关阅读:
【流媒体】Nginx+nginxhttpflvmodule流媒体+鉴权
【Linux】Tess4j识别pdf报错libgs.so无法加载
【Java】tess4j文字识别
【Spring专场】「MVC容器」不看源码就带你认识核心流程以及运作原理
【Spring专场】「AOP容器」不看源码就带你认识核心流程以及运作原理
【Spring专场】「IOC容器」不看源码就带你认识核心流程以及运作原理
【分布式技术专题】「Zookeeper中间件」给大家学习一下Zookeeper的”开发伴侣”—CuratorFramework(基础篇)
🍃【Spring专题】「技术原理」为大家介绍一下Spring中的Ant路径匹配工具组件AntPathMatcher
Wireshark安装入门及抓取网站用户名密码
手把手教你用 wireshark 抓包
原文地址:https://www.cnblogs.com/wubiyu/p/812673.html
最新文章
喜闻我的文章进入“多核技术博客征文” top 30
Java中的System.nano()很慢
Google云计算体验感受
新写了一个Java并发程序设计教程
杂谈单点登陆以及相关技术
重读罗素《西方哲学史》关于浪漫主义部分的介绍
我在Google AppEngine上部署了一个Java应用(OpenID测试)
绝世名将
随想
佛教典故
热门文章
github上的一些注意事项
zz HOWTO do Linux kernel development
zz Closedform solution 闭合解
最活跃FPGA论坛推荐社区
着手写本开源书了
zz嵌入式Linux下Camera编程V4L2
张正友摄像机标定的研究(MATLAB+OpenCV)
zz数字图像的结课报告
需要改变一下观念
【Linux】linux部署tesseract
Copyright © 2011-2022 走看看