最近有個朋友離開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();
|