HttpWebRequest获得某个页面,并填写页面的textbox及点击button的方法

step1:use HttpWebRequest class to request,and use HttpWebResponse get the response.
if you read the response, you can get the information including what did in the Page_load function.
step2: if you want to fill a textbox or click a button and etc.. in this page, you can repalce the response with the Message,and use HttpWebRequest class to post it to the server.The server will get what in the textbox or do the Onclick function.
Message: you can use sniffer to catch the postbag and get what in it,usually with a "&" and the webcontrol name or id.It is a format of ASPX post.
step3: use HttpWebResponse get the response.You can read the response to get the information which did in the Onclick function.
以下代码是向一个指定的页面发送请求,填写三个textbox,click一个button,并得到Server端的执行是否成功的class。注:由于页面的代码也是我写的,所以我知道将要Post页面的结构,即我知道每个页面控件的name和页面结构。

  1. publicclassFtpMessage
  2. {
  3. privatestringm_fileName;
  4. privatestringm_host;
  5. privatestringaspValue;
  6. publicFtpMessage(stringfileName,stringhostUrl)
  7. {
  8. //指定的一个信息,将用于填写TextBoxFileName。
  9. m_fileName=fileName;
  10. //指定的URL
  11. m_host=hostUrl;
  12. }
  13. publicboolSendCompleteMessage(stringuser,stringpassword)
  14. {
  15. boolisSendMessageSuccess=false;
  16. HttpWebRequestrequest=(HttpWebRequest)WebRequest.Create("http://"+m_host);
  17. //得到网页。
  18. WebResponseresponse=null;
  19. response=request.GetResponse();
  20. StreamreadStream=response.GetResponseStream();
  21. StreamReadersr=newStreamReader(readStream,Encoding.GetEncoding("utf-8"));
  22. stringcontent="";
  23. intindex=-1;
  24. //寻找数据
  25. while(index<0&&content!=null)
  26. {
  27. content=sr.ReadLine();
  28. //得到标准的ASPX页面的头一共26个字节,以("name=/"__VIEWSTATE/"value=/""开始的
  29. index=content.IndexOf("name=/"__VIEWSTATE/"value=/"",0);
  30. }
  31. index+=26;
  32. intendIndex=content.LastIndexOf("/"");
  33. //形成目标数据。
  34. if(index>26&&endIndex>index)
  35. {
  36. //得到页面数据段
  37. aspValue=content.Substring(index,endIndex-index);
  38. StringBuildertempData=newStringBuilder();
  39. tempData.Append("__VIEWSTATE=");
  40. tempData.Append(HttpUtility.UrlEncode(aspValue));
  41. //填写TextBoxFileName的值,其值见后
  42. tempData.Append("&TextBoxFileName=");
  43. tempData.Append("(content1)");
  44. //填写TextBoxUser的值,其值见后
  45. tempData.Append("&TextBoxUser=");
  46. tempData.Append("(content2)");
  47. //填写TextBoxPassword的值,其值见后
  48. tempData.Append("&TextBoxPassword=");
  49. tempData.Append("(content3)");
  50. //填写ButtonClick的Message
  51. tempData.Append("&ButtonForData=");
  52. tempData.Append(HttpUtility.UrlEncode("Message"));
  53. aspValue=tempData.ToString();
  54. }
  55. stringcontent1=m_fileName;
  56. stringcontent2=user;
  57. stringcontent3=password;
  58. WebResponseresponse1=null;
  59. //替换预传送的数据。
  60. stringtempData1=aspValue.Replace("(content1)",HttpUtility.UrlEncode(content1));
  61. stringtempData2=tempData1.Replace("(content2)",HttpUtility.UrlEncode(content2));
  62. stringtempData3=tempData2.Replace("(content3)",HttpUtility.UrlEncode(content3));
  63. byte[]postData=Encoding.UTF8.GetBytes(tempData3.ToString());
  64. HttpWebRequestrequest1=(HttpWebRequest)WebRequest.Create("http://"+m_host);
  65. //设定工作属性。
  66. request1.Method="POST";
  67. request1.ContentType="application/x-www-form-urlencoded";
  68. request1.ContentLength=postData.Length;
  69. //上传数据。
  70. StreamwriteStream=request1.GetRequestStream();
  71. writeStream.Write(postData,0,postData.Length);
  72. writeStream.Close();
  73. //下载回应消息。
  74. stringserverMessage="";
  75. try
  76. {
  77. response1=request1.GetResponse();
  78. //这里的response1是Server在Button点击后跳转到的另一个页面,这个页面有一个值表示是否成功
  79. //我将取得其值作为函数的返回值
  80. StreamreadStream1=response1.GetResponseStream();
  81. inti=1024;
  82. byte[]hehe=newbyte[i];
  83. readStream1.Read(hehe,0,i);
  84. readStream1.Close();
  85. StringBuilderhehe1=newStringBuilder();
  86. //由于我知道页面的结构,我可以从页面得到这个值。
  87. for(intj=658;j<662;j++)
  88. {
  89. hehe1.Append((char)hehe[j]);
  90. }
  91. serverMessage=hehe1.ToString();
  92. }
  93. catch(ExceptionE)
  94. {
  95. stringtempError=E.Message;
  96. }
  97. if(serverMessage=="true")
  98. {
  99. isSendMessageSuccess=true;
  100. }
  101. returnisSendMessageSuccess;
  102. }
  103. }

你可能感兴趣的:(request)