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和页面结构。
- publicclassFtpMessage
- {
-
privatestringm_fileName;
-
privatestringm_host;
-
privatestringaspValue;
-
publicFtpMessage(stringfileName,stringhostUrl)
- {
-
- m_fileName=fileName;
-
- m_host=hostUrl;
- }
-
publicboolSendCompleteMessage(stringuser,stringpassword)
- {
-
boolisSendMessageSuccess=false;
-
HttpWebRequestrequest=(HttpWebRequest)WebRequest.Create("http://"+m_host);
-
-
WebResponseresponse=null;
- response=request.GetResponse();
- StreamreadStream=response.GetResponseStream();
-
StreamReadersr=newStreamReader(readStream,Encoding.GetEncoding("utf-8"));
-
stringcontent="";
-
intindex=-1;
-
-
while(index<0&&content!=null)
- {
- content=sr.ReadLine();
-
-
index=content.IndexOf("name=/"__VIEWSTATE/"value=/"",0);
- }
- index+=26;
-
intendIndex=content.LastIndexOf("/"");
-
-
if(index>26&&endIndex>index)
- {
-
- aspValue=content.Substring(index,endIndex-index);
-
StringBuildertempData=newStringBuilder();
-
tempData.Append("__VIEWSTATE=");
- tempData.Append(HttpUtility.UrlEncode(aspValue));
-
-
tempData.Append("&TextBoxFileName=");
-
tempData.Append("(content1)");
-
-
tempData.Append("&TextBoxUser=");
-
tempData.Append("(content2)");
-
-
tempData.Append("&TextBoxPassword=");
-
tempData.Append("(content3)");
-
-
tempData.Append("&ButtonForData=");
-
tempData.Append(HttpUtility.UrlEncode("Message"));
- aspValue=tempData.ToString();
- }
-
stringcontent1=m_fileName;
-
stringcontent2=user;
-
stringcontent3=password;
-
WebResponseresponse1=null;
-
-
stringtempData1=aspValue.Replace("(content1)",HttpUtility.UrlEncode(content1));
-
stringtempData2=tempData1.Replace("(content2)",HttpUtility.UrlEncode(content2));
-
stringtempData3=tempData2.Replace("(content3)",HttpUtility.UrlEncode(content3));
-
byte[]postData=Encoding.UTF8.GetBytes(tempData3.ToString());
-
HttpWebRequestrequest1=(HttpWebRequest)WebRequest.Create("http://"+m_host);
-
-
request1.Method="POST";
-
request1.ContentType="application/x-www-form-urlencoded";
- request1.ContentLength=postData.Length;
-
- StreamwriteStream=request1.GetRequestStream();
- writeStream.Write(postData,0,postData.Length);
- writeStream.Close();
-
-
stringserverMessage="";
-
try
- {
- response1=request1.GetResponse();
-
-
- StreamreadStream1=response1.GetResponseStream();
-
inti=1024;
-
byte[]hehe=newbyte[i];
- readStream1.Read(hehe,0,i);
- readStream1.Close();
-
StringBuilderhehe1=newStringBuilder();
-
-
for(intj=658;j<662;j++)
- {
-
hehe1.Append((char)hehe[j]);
- }
- serverMessage=hehe1.ToString();
- }
-
catch(ExceptionE)
- {
-
stringtempError=E.Message;
- }
-
if(serverMessage=="true")
- {
-
isSendMessageSuccess=true;
- }
-
returnisSendMessageSuccess;
- }
- }
分享到:
相关推荐
httpwebrequest抓取页面小工具
C# 代码编写; 通过一个页面按钮调用另一个页面的按钮(不打开页面的前提下); 采用的是Response,HttpWebRequest方法
HttpWebRequest详解HttpWebRequest详解HttpWebRequest详解
httpwebrequest调用webservice返回数据
httpwebrequest 封装类
window phone WebClient和HttpWebRequest 并解决gb2312乱码问题
1.这是一个用HttpWebRequest类构建完整Http多部请求上传文件的示例 2.上传地址是金山文档预览的地址,所以,只要上传的是word、pdf、excel一类的文档,可以通过返回的地址直接在线预览,是一种偷懒的文档在线浏览...
C#中HttpWebRequest使用介绍 GET HTTP HTTPS 请求
一个很实用的例子,注意要用OFFICE 2007
asp.net 服务端代码以 xml utf-8的格式向指定地址post 数据并接收返回值 HttpWebRequest 简单的代码事例,可以自行扩展使用
使用HttpWebRequest访问网页
HttpWebRequest: 命名空间: System.Net,这是.NET创建者最初开发用于使用HTTP请求的标准类。使用HttpWebRequest可以让开发者控制请求/响应流程的各个方面,如 timeouts, cookies, headers, protocols。另一个好处...
模拟请求,基于HttpWebRequest封装好的一个类,自动记下和发送Cookie,你只管调用方法就行了,很实用
使用 HttpWebRequest发送自定义POST请求
本文将讲述如何采用HttpWebRequest方式每次读取固定大小数据片段(如4KB)发送至服务器,为大文件上传提供解决方案,本文还将详细讲述如何将“文件上传”功能做为用户自定义控件,实现模块重用。
.NET中封装静态类通过HttpWebRequest实现post、get、文件传输。
本文将讲述如何采用HttpWebRequest方式每次读取固定大小数据片段(如4KB)发送至服务器,为大文件上传提供解决方案,本文还将详细讲述如何将“文件上传”功能做为用户自定义控件,实现模块重用。
C#使用HttpWebRequest自动更新客户端应用程序