使用HttpWebRequest向网站模拟上传数据


最近有个朋友离开IT行业二年的朋友说要实现用程序向某个网站的页面上传数据,他是意思是每天有几十条数据要在网站页面上填写,很烦,最好用程序来写。网站页面是用POST传递的,同时没有验证码之类的东东,只有一点限制就是5分种内不能填写二次记录。这一切都好办。

using System.Web;
using System.Net;
using System.Text;
using System.IO;

//创建对某个网站页面的请求

HttpWebRequest myRequest = (HttpWebRequest )WebRequest.Create("http://www.knowsky.com/a.asp")

//上传的数据,”TextBox1“这些东东是网站页面里的控件ID,如果要上传多个值也是用&来分隔

string postData="TextBox1="+this.textBox1.Text+"&TextBox2="+this.textBox2.Text+"
&TextBox3="+this.textBox3.Text+"&TextBox4="+this.textBox4.Text;
ASCIIEncoding encoding=new ASCIIEncoding();
byte[] byte1=encoding.GetBytes(postData);//最终编码后要上传的数据
// Set the content type of the data being posted.
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.Method="post";//post上传方式
// Set the content length of the string being posted.
myRequest.ContentLength=postData.Length;
Stream newStream=myRequest.GetRequestStream();
newStream.Write(byte1,0,byte1.Length);


一切就OK了,如果你想上传后看到网站的内容的话,可以在程序里放一个IE控件,使用

axWebBrowser1.Navigate("http://www.knowsky.com/a.asp");
axWebBrowser1.Refresh2();


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3