Private void page_load (Object sender, system. eventargs E)
{
String url = "http: // localhost/webuser‑wexample/webform5.aspx ";
System. net. cookiecontainer cook = new system. net. cookiecontainer ();
String gets = gethtmlbyurlcook (URL, ref Cook, "get", null, true );
String strviewstate = system. text. regularexpressions. regEx. replace (gets, "[// s *] * <input type =/" hidden/"name =/" _ viewstate/"value =/" ([^/"] *) /"[// s *] *", "$1", system. text. regularexpressions. regexoptions. ignorecase );
String Param = "_ viewstate =" + system. Web. httputility. urlencode (strviewstate) + "& textbox1 = XXXX & button1 = button & checkbox1 = on ";
String S = A. gethtmlbyurlcook (URL, ref Cook, "Post", Param, true );
Response. Write (s );
}
Public String gethtmlbyurlcook (string URL, Ref system. net. cookiecontainer Cook, string smethod, string Param, bool bautoredirect)
{
Smethod = smethod. toupper ();
Smethod = smethod! = "Post "? "Get": smethod;
String res = "";
Httpwebrequest Re = (httpwebrequest) httpwebrequest. Create (URL );
Re. cookiecontainer = cook; // attach the cook object
Re. method = smethod;
// Re. allowautoredirect = bautoredirect;
// Re. useragent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; myie2;. Net CLR 1.1.4322 )";
//
// Re. clientcertificates = new system. Security. cryptography. x509certificates. x509certificatecollection ();
// Re. clientcertificates = system. Security. cryptography. x509certificates. x509certificate. createfromsignedfile ();
// Re. Timeout = 2000;
If (smethod = "Post") // post data to server
{
Re. contenttype = "application/X-WWW-form-urlencoded ";
Byte [] B = system. Text. encoding. utf8.getbytes (PARAM );
Re. contentlength = B. length;
Try
{
Stream osre = Re. getrequeststream ();
Osre. Write (B, 0, B. Length );
Osre. Close ();
Osre = NULL;
}
Catch (exception)
{
Re = NULL;
Return "-1 ";
}
}
Httpwebresponse rep = NULL;
Stream oresponsestream = NULL;
Streamreader osreader = NULL;
Try
{
Rep = (httpwebresponse) Re. getresponse ();
Oresponsestream = rep. getresponsestream ();
Osreader = new streamreader (oresponsestream, system. Text. encoding. Default );
Res = osreader. readtoend ();
}
Catch (system. net. webexception E)
{
// Res = "-1 ";
Res = E. tostring ();
}
If (rep! = NULL)
{
Rep. Close ();
Rep = NULL;
}
If (oresponsestream! = NULL)
{
Oresponsestream. Close ();
Oresponsestream = NULL;
}
If (osreader! = NULL)
{
Osreader. Close ();
Osreader = NULL;
}
Re = NULL;
Return res;
}