Namespace discuzlogin
{
Using system;
Using system. IO;
Using system. net;
Using system. text;
Using system. Web;
Public class discuzlogin
{
Private httpwebrequest bbsrequest = NULL;
Private httpwebresponse bbsresponse = NULL;
Private cookiecollection gcookiecollention = NULL;
Private string responsehtml = string. empty;
Public cookiecollection login (string name, string PWD, string bbsurl)
{
String S = string. format ("formhash = 270dff9b & Referer =" + bbsurl + "% 2f & loginfield = username & username = {0} & Password = {1} & questionid = 0 & Answer =", httputility. urlencode (name, encoding. default), PWD );
Byte [] bytes = encoding. utf8.getbytes (s );
String requesturistring = string. Format ("{0}/logging. php? Action = login & loginsubmit = Yes & floatlogin = Yes & inajax = 1 ", bbsurl );
Int length = environment. getfolderpath (environment. specialfolder. Cookies). length;
For (INT I = 0; I <length; I ++)
{
Environment. getfolderpath (environment. specialfolder. Cookies). Remove (0 );
}
Try
{
Cookiecontainer Container = new cookiecontainer ();
This. bbsrequest = (httpwebrequest) webrequest. Create (requesturistring );
This. bbsrequest. cookiecontainer = container;
This. bbsrequest. contenttype = "application/X-WWW-form-urlencoded ";
This. bbsrequest. method = "Post ";
This. bbsrequest. contentlength = bytes. length;
Stream requeststream = This. bbsrequest. getrequeststream ();
Requeststream. Write (bytes, 0, bytes. Length );
Requeststream. Close ();
This. bbsresponse = (httpwebresponse) This. bbsrequest. getresponse ();
This. responsehtml = new streamreader (this. bbsresponse. getresponsestream (), encoding. Default). readtoend ();
This. gcookiecollention = This. bbsresponse. Cookies;
Return this. gcookiecollention;
}
Catch (exception)
{
Return this. gcookiecollention;
}
}
}
}
Reference: http://www.cnblogs.com/qihangkeji/archive/2010/10/31/1865795.html