正文

关于在WinForm里用HttpWebRequest获得某个页面,并填写页面的t2006-08-10 11:40:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/Csharpsky/17442.html

分享到:

方法如下:

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和页面结构。


public class FtpMessage
 {
  private string m_fileName;
  private string m_host;
  private string aspValue;

  public FtpMessage(string fileName,string hostUrl)
  {
    //指定的一个信息,将用于填写TextBoxFileName。
    m_fileName = fileName;
    //指定的URL
    m_host = hostUrl;
  }

  public bool SendCompleteMessage(string user,string password)
  {
   bool isSendMessageSuccess = false;
   HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://"+m_host/);
   //得到网页。
   WebResponse response = null;
   response = request.GetResponse();
   Stream readStream = response.GetResponseStream();
   StreamReader sr = new StreamReader(readStream,Encoding.GetEncoding("utf-8"));
   string content = "";
   int index = -1;
   //寻找数据
   while (index < 0 && content != null)
   {
    content = sr.ReadLine();
    //得到标准的ASPX页面的头一共26个字节,以("name=\"__VIEWSTATE\" value=\""开始的
    index = content.IndexOf("name=\"__VIEWSTATE\" value=\"",0);
   }
   index += 26;
   int endIndex = content.LastIndexOf("\"");
   //形成目标数据。
   if (index > 26 && endIndex > index)
   {
    //得到页面数据段
    aspValue = content.Substring(index,endIndex-index);
    StringBuilder tempData = new StringBuilder();
    tempData.Append("__VIEWSTATE=");
    tempData.Append(HttpUtility.UrlEncode(aspValue));
    //填写TextBoxFileName的值,其值见后
    tempData.Append("&TextBoxFileName=");
    tempData.Append("(content1)");
    //填写TextBoxUser的值,其值见后
    tempData.Append("&TextBoxUser=");
    tempData.Append("(content2)");
    //填写TextBoxPassword的值,其值见后
    tempData.Append("&TextBoxPassword=");
    tempData.Append("(content3)");
     //填写Button Click 的Message
    tempData.Append("&ButtonForData=");
    tempData.Append(HttpUtility.UrlEncode("Message"));
    aspValue = tempData.ToString();
   }

   string content1 = m_fileName;
   string content2 = user;
   string content3 = password;
   WebResponse response1 = null;
   //替换预传送的数

阅读(2627) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册