方法如下:

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;
   //替换预传送的数据。
   string tempData1 = aspValue.Replace("(content1)",HttpUtility.UrlEncode(content1));
   string tempData2 = tempData1.Replace("(content2)",HttpUtility.UrlEncode(content2));
   string tempData3 = tempData2.Replace("(content3)",HttpUtility.UrlEncode(content3));
   byte [] postData = Encoding.UTF8.GetBytes(tempData3.ToString());

HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create("http://"+m_host);

//设定工作属性。
   request1.Method = "POST";
   request1.ContentType = "application/x-www-form-urlencoded";
   request1.ContentLength = postData.Length;
    
   //上传数据。
   Stream writeStream = request1.GetRequestStream();
   writeStream.Write(postData,0,postData.Length);
   writeStream.Close();
   
   //下载回应消息。
   string serverMessage = "";
   try
   {
    response1 = request1.GetResponse();
    //这里的response1是Server在Button点击后跳转到的另一个页面,这个页面有一个值表示是否成功
    //我将取得其值作为函数的返回值
    Stream readStream1 = response1.GetResponseStream();
    int i = 1024;
    byte[] hehe = new byte[i];
    readStream1.Read(hehe,0,i);
    readStream1.Close();

StringBuilder hehe1 = new StringBuilder();
   //由于我知道页面的结构,我可以从页面得到这个值。
    for(int j=658;j<662;j++)
    {
     hehe1.Append((char)hehe[j]);
    }
    serverMessage = hehe1.ToString();

}
   catch(Exception E)
   {
    string tempError = E.Message;
   }
   if(serverMessage == "true")
   {
    isSendMessageSuccess = true;
   }

return isSendMessageSuccess;
  }
 }

转载于:https://www.cnblogs.com/top5/archive/2009/08/11/1543428.html

关于在WinForm里用HttpWebRequest获得某个页面,并填写页面的textbox及点击button的方法...相关推荐

  1. NET中winform与webform互相通讯实例,CS调用BS页面的JS函数---转载

    原创:.NET中winform与webform互相通讯实例,CS调用BS页面的JS函数 时间:2009-12-11 21:09:32 来源:原创 作者:dodo - - 现在C#越来越多朋友使用了,大 ...

  2. 有谁做过在C# winform里,显示3D max文件的呢?请赐教了。

    有谁做过在C# winform里,显示3D max文件的呢?请赐教了. 转载于:https://www.cnblogs.com/igouz/archive/2009/05/05/3DMax.html

  3. EF里查看/修改实体的当前值、原始值和数据库值以及重写SaveChanges方法记录实体状态...

    EF里查看/修改实体的当前值.原始值和数据库值以及重写SaveChanges方法记录实体状态 原文:EF里查看/修改实体的当前值.原始值和数据库值以及重写SaveChanges方法记录实体状态 本文目 ...

  4. php 浏览器打开excel文件夹里,php网页显示excel表格数据-phpexcel 读取excel里的数据并在页面显示出来...

    网页如何调用excel表格中的数据? $retdata=array(); //内容 //二维数 $filename= "文.csv"; //生成头文件 $now = gmdate( ...

  5. 如何删除下一页分节符_页面布局里分节符添加的下一页空白页怎么删 - 卡饭网...

    word2010怎么插入分节符 word2010怎么插入分节符 通过在Word2010文档中插入分节符,可以将Word文档分成多个部分.每个部分可以有不同的页边距.页眉页脚.纸张大小等不同的页面设置. ...

  6. php中跨页面id的获取,excel跨表格提取数据?phpexcel 读取excel里的数据并在页面显示出来...

    php怎么读取excel 文件数据并输出 PHPExcel PHPExcel 是用来操作Office Excel 文一个PHP类库,它基于微软的OpenXML和PHP语言.可以使用它来.写入不同格式的 ...

  7. 从“在winform里打开网页功能”引发的问题探究

    今天有个同事问了一下我,怎么在winform里面打开网页啊?我们都是基于B/S的开发,很少接触winform,所以我当时就懵了,实在不知道怎么回答,所以索性说不知道.但是我又想了想,这个应该是个很简单 ...

  8. WinForm里ListBox实现加入项目,并且排序。

    ListBox要实现排序,因为里面的ListBox.item[i]都是string类型的,所以就先将他们转换成int.parse(),存入到数组中去.再在数组中排序好. int[] Arr = new ...

  9. C# DataGrid 控件在winform里显示行号

    #region 行绘制事件 ,为DataGridView每行写上序号/// <summary>/// 行绘制事件 ,为DataGridView每行写上序号/// </summary& ...

最新文章

  1. 植树节,程序员要爬哪些“树”?
  2. 三层架构用户登录代码c语言,三层架构实现简单的用户登录代码
  3. ASP.NET企业开发框架IsLine FrameWork系列之三--七种武器
  4. 如何在html中编写scss,编写Sass的八个技巧
  5. Openfire:安装指南
  6. jsp 判断时间大小
  7. axis2与cxf区别
  8. 关于vs2008 SP1中CMFCToolBar的一些事儿
  9. xmind2020激活教程_思维导图软件XMind 2020 v10.2.1中文版的官网下载、安装与序列号注册文件激活教程-推荐实用小软件 -亦是美网络...
  10. html超链接下划线改虚线_怎么把下划线变成虚线
  11. 8.0高等数学五-斯托克斯公式
  12. android 免root 模拟器,真正免root的root工具箱详细使用教程
  13. Java base64转化成 inputStream
  14. java3D实现空间立方体_CSS3 3D旋转立方体
  15. 【转载】Windows下Tesseract4.0识别与中文手写字体训练
  16. javafx饼图特效
  17. Java实现 LeetCode 91 解码方法
  18. Python基础1——导入文件数据
  19. 米思齐(Mixly)图形化系列教程(六)-for循环
  20. Android学习-通知的使用详解

热门文章

  1. 【IEEE出版】计算机多主题征稿,ICBASE 2020诚邀您投稿参会!
  2. ECCV 2020 目标检测与跟踪赛事,清华大学主办 GigaVision 2020 欢迎报名~
  3. 用CNN做NLP文本分类竞赛
  4. CVPR 2019 行人检测新思路:高级语义特征检测取得精度新突破
  5. Python中的实用小技巧,可以省下不是事情,喜欢记得收下
  6. 由浅入深|让你彻底理解Python中的yield
  7. 基友扫雷通关跟我炫耀!于是用Python自动扫雷程序十秒通关
  8. ps 2键盘代码 c语言,MicroBlaze中断编程——以PS/2键盘输入为例
  9. 时间序列异常检测 EGADS Surus iForest
  10. Matlab线性/非线性规划优化算法(7)