欢迎光临
我们一直在努力

网上取数据的一个例子-ASP教程,数据库相关

建站超值云服务器,限时71元/月

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;

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 网上取数据的一个例子-ASP教程,数据库相关
分享到: 更多 (0)

相关推荐

  • 暂无文章