王朝网络
分享
 
 
 

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

王朝c#·作者佚名  2006-12-17
宽屏版  字体: |||超大  

方法如下:

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;

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有