Httpwebrequest retrieval page

Source: Internet
Author: User

 

To obtain a page using httpwebrequest in winform, enter the textbox of the page and click the button as follows:
Below Code Is to send a request to a specified page, fill in three Textbox, click a button, and get whether the server-side execution is successful class. Note: Since the Page code is also written by me, I know the structure of the page to be post, that is, I know the name and structure of each page control.

Public   Class Ftpmessage
{
Private   String M_filename;
Private   String M_host;
Private   String Aspvalue;
Public Ftpmessage ( String Filename, String Hosturl)
{
// The specified information is used to fill in textboxfilename.
M_filename = Filename;
// Specified URL
M_host = Hosturl;
}
Public   Bool Sendcompletemessage ( String User, String Password)
{
Bool Issendmessagesuccess =   False ;
Httpwebrequest request = (Httpwebrequest) webrequest. Create ( " Http :// " + M_host );
// Get the webpage.
Webresponse response =   Null ;
Response = Request. getresponse ();
Stream readstream = Response. getresponsestream ();
Streamreader SR =   New Streamreader (readstream, encoding. getencoding ( " UTF-8 " ));
String Content =   "" ;
Int Index =   - 1 ;
// Search for Data
While (Index <   0   && Content ! =   Null )
{
Content = Sr. Readline ();
// The header of the standard ASPX page contains a total of 26 bytes, starting with ("name = \" _ viewstate \ "value = \""
Index = Content. indexof ( " Name = \ " _ Viewstate \ " Value = \ "" , 0 );
}
Index + =   26 ;
Int Endindex = Content. lastindexof ( " \ "" );
// Form the target data.
If (Index >   26   && Endindex > Index)
{
// Get the page data segment
Aspvalue = Content. substring (index, endindex - Index );
Stringbuilder tempdata =   New Stringbuilder ();
Tempdata. append ( " _ Viewstate = " );
Tempdata. append (httputility. urlencode (aspvalue ));
// Enter the value of textboxfilename. For the value, see the following
Tempdata. append ( " & Textboxfilename = " );
Tempdata. append ( " (Content1) " );
// Enter the textboxuser value.
Tempdata. append ( " & Textboxuser = " );
Tempdata. append ( " (Content2) " );
// Enter the value of textboxpassword.
Tempdata. append ( " & Textboxpassword = " );
Tempdata. append ( " (Content3) " );
// Enter the message of the button click.
Tempdata. append ( " & Buttonfordata = " );
Tempdata. append (httputility. urlencode ( " Message " ));
Aspvalue = Tempdata. tostring ();
}
String Content1 = M_filename;
String Content2 = User;
String Content3 = Password;
Webresponse response1 =   Null ;
// Replace the pre-transmitted data.
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 );
// Set work attributes.
Request1.method =   " Post " ;
Request1.contenttype =   " Application/X-WWW-form-urlencoded " ;
Request1.contentlength = Postdata. length;
// Upload data.
Stream writestream = Request1.getrequeststream ();
Writestream. Write (postdata, 0 , Postdata. Length );
Writestream. Close ();
// Download the Response Message.
String Servermessage =   "" ;
Try
{
Response1 = Request1.getresponse ();
// Response1 is another page that the server jumps to after clicking the button. A value indicates whether the page is successful.
// I will get its value as the return value of the Function
Stream readstream1 = Response1.getresponsestream ();
Int I =   1024 ;
Byte [] Hehe =   New   Byte [I];
Readstream1.read (Hehe, 0 , I );
Readstream1.close ();
Stringbuilder hehe1 =   New Stringbuilder ();
// Because I know the Page Structure, I can get this value from the page.
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;
}
}

the code in the webpage is not written, that is, An ASPX page. Drag three textbox and then a button (both web and not html ),
the three textbox names are textboxfilename, textboxuser, and textboxpassword.
the button name is buttonfordata. The processing method is written in the buttonclick method. You can directly obtain this. you can write
" string Ss = This. textboxfilename. text; "

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.