您的位置:首页 > 理论基础 > 计算机网络

使用httpwebrequest Post数据到网站

2014-01-09 18:02 991 查看
怎样通过HttpWebRequest 发送 POST 请求到一个网页服务器?例如编写个程序实现自动用户登录,自动提交表单数据到网站等。

假如某个页面有个如下的表单(Form):

<form name="form1" action="http://www.here.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();

Console.WriteLine(content);

使用Visual Sniffer :

这里介绍个工具: Visual Sniffer , google 一下便可轻松找到下载地址。

可以使用 Visual Sniffer 来捕捉提交的数据信息:

1. 访问你需要站外提交的页面,比如 CSDN 登陆页 http://www.csdn.net/member/UserLogin.aspx

2. 填写好需要的资料,比如用户名和密码,

3. 打开 Visual Sniffer, 点“开始拦截”

4. 在访问的页面中提交。

5. 等提交成功之后,在 Visual Sniffer 中“停止拦截”

6. 在 Visual Sniffer 的左侧栏的加号中依次点开,右边是它拦截到的内容,

找到 内容含有 POST http://www.csdn.net/member/UserLogin.aspx
的节点

以下是我拦截的内容供参考:

POST http://www.csdn.net/member/UserLogin.aspx HTTP/1.0


Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*


Referer: http://www.csdn.net/member/UserLogin.aspx

Accept-Language: zh-cn


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


UA-CPU: x86


Pragma: no-cache


User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; InfoPath.1)


Host: www.csdn.net


Content-Length: 355


Proxy-Connection: Keep-Alive


Cookie: ASPSESSIONIDAAAATBQC=FMEGGCKDBKHAMMCGKPFDMBFG; ASP.NET_SessionId=lusprmnom05lr445tmteaf55; userid=699879




__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=dDwtMTcwMzgxNjQ2Mjs7bDxDU0ROVXNlckxvZ2luOmNiX1NhdmVTdGF0ZTtDU0ROVXNlckxvZ2luOkltYWdlX0xvZ2luOz4%2Btu1q2wmRZoAJTi9L73w1zBleylY%3D&CSDNUserLogin%3Atb_UserName=testusername&CSDNUserLogin%3Atb_Password=testpassword&CSDNUserLogin%3Atb_ExPwd=9232&from=&CSDNUserLogin%3AImage_Login.x=36&CSDNUserLogin%3AImage_Login.y=6


GET http://www.csdn.net/mycustompage.htm?aspxerrorpath=/member/UserLogin.aspx HTTP/1.0


Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*


Referer: http://www.csdn.net/member/UserLogin.aspx

Accept-Language: zh-cn


UA-CPU: x86


Pragma: no-cache


User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; InfoPath.1)


Host: www.csdn.net


Proxy-Connection: Keep-Alive


Cookie: ASPSESSIONIDAAAATBQC=FMEGGCKDBKHAMMCGKPFDMBFG; ASP.NET_SessionId=lusprmnom05lr445tmteaf55; userid=699879

注意:PostData 参数之间是以 " & " 进行 连接的

OK,通过以上简单示例,只要稍微修改下,即可做成多站点自动登陆,或自动网上投票等功能!

such as:

代码如下:


ASCIIEncoding encoding=new ASCIIEncoding();


string postData="TextBox1=33&Button1=Button";


byte[] data = encoding.GetBytes(postData);




// Prepare web request



HttpWebRequest myRequest =


(HttpWebRequest)WebRequest.Create("http://localhost/testform1.aspx");


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();

解释:

postData 为你要提交的数据

比如 CSDN 的登录页面 http://www.csdn.net/member/UserLogin.aspx

输入用户名密码和校验码,并提交之后,浏览器便将下面的数据提交到服务器:


CSDNUserLogin%3Atb_UserName=yourName&CSDNUserLogin%3Atb_Password=yourPassword&CSDNUserLogin%3Atb_ExPwd=2332

其中的 yourName 为你实际登陆时提交的用户名, yourPassword 即为你的密码, 2332 是我刚才登陆时的验证码

参考网址:

http://dev.csdn.net/article/28/28374.shtm

http://www.knowsky.com/18774.html

http://www.netomatix.com/HttpPostData.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: