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;
            }
        }
    }

}

 

引用资料:http://www.cnblogs.com/qihangkeji/archive/2010/10/31/1865795.html