欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-13 13:25:52
最近有个朋友离开it行业二年的朋友说要实现用程序向某个网站的页面上传数据,他是意思是每天有几十条数据要在网站页面上填写,很烦,最好用程序来写。网站页面是用post传递的,同...

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

using system.web;
using system.net;
using system.text;
using system.io;

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

httpwebrequest  myrequest = (httpwebrequest )webrequest.create("")

//上传的数据,”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("");
axwebbrowser1.refresh2();