private void page_load(object sender, system.eventargs e)
{
string url="http://localhost/webuserwindowexample/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*\\s*]*<input type=\"hidden\" name=\"__viewstate\" value=\"([^\"]*)\"[\\s*\\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;
}
