笔记用的,有事没事常看看

C#登录织梦获取网站栏目

类代码

using System;      
using System.Collections.Generic;      
using System.Text;      
using System.Web;      
using System.Net;      
using System.IO;      
              
namespace 织梦登录      
{      
    class XiaoBin      
    {      
        CookieContainer cookies = new CookieContainer();      
        /// <summary>      
        /// 登录      
        /// </summary>      
        /// <param name="url">目标页面</param>      
        /// <param name="fromstr">来路地址</param>      
        /// <param name="data">要提交的数据</param>      
        /// <returns></returns>      
        public  string Login(string url, string fromstr, string data)      
        {      
            login1(url, fromstr, data);      
            Encoding enc = Encoding.GetEncoding("gb2312");      
            string RegStr = "系统错误,请重试";//返回的文字信息      
            try
            {      
                HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(url);      
                Request.CookieContainer = cookies;      
                Request.Accept = "text/html, application/xhtml+xml, */*";      
                Request.Method = "post";      
                Request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";      
                Request.ContentType = "application/x-www-form-urlencoded";      
                Request.Referer = fromstr;      
                Request.Headers["Accept-Language"] = "zh-cn";      
                string s = data;      
                byte[] Shuju = enc.GetBytes(s);      
                Request.ContentLength = Shuju.Length;      
                //提交数据      
                Stream outstream = Request.GetRequestStream();      
                outstream.Write(Shuju, 0, Shuju.Length);      
                outstream.Close();      
                //获取响应      
                HttpWebResponse res = (HttpWebResponse)Request.GetResponse();      
                if (res.StatusCode == HttpStatusCode.OK)//状态ok      
                {      
                    //获取响应后的数据      
                    StreamReader reader = new StreamReader(res.GetResponseStream(), enc);      
                    Char[] read = new Char[256];      
                    int count = reader.Read(read, 0, 256);      
                    string str = null;      
                    while (count > 0)      
                    {      
                        str += new String(read, 0, count);      
                        count = reader.Read(read, 0, 256);      
                    }      
                    foreach (Cookie cookie in res.Cookies)      
                        cookies.Add(cookie);      
                    reader.Close();      
                    outstream.Close();      
                    res.Close();      
                    RegStr = str;//返回登录后的Html源码 可以通过内容判断是否登录成功      
                }      
            }      
            catch
            { }      
              
            return RegStr;      
        }      
        private void login1(string url, string fromstr, string data)      
        {      
            Encoding enc = Encoding.GetEncoding("gb2312");      
            string RegStr = "系统错误,请重试";//返回的文字信息      
            try
            {      
                HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(url);      
                Request.CookieContainer = cookies;      
                Request.Accept = "text/html, application/xhtml+xml, */*";      
                Request.Method = "post";      
                Request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";      
                Request.ContentType = "application/x-www-form-urlencoded";      
                Request.Referer = fromstr;      
                Request.Headers["Accept-Language"] = "zh-cn";      
                string s = data;      
                byte[] Shuju = enc.GetBytes(s);      
                Request.ContentLength = Shuju.Length;      
                //提交数据      
                Stream outstream = Request.GetRequestStream();      
                outstream.Write(Shuju, 0, Shuju.Length);      
                outstream.Close();      
                //获取响应      
                HttpWebResponse res = (HttpWebResponse)Request.GetResponse();      
                if (res.StatusCode == HttpStatusCode.OK)//状态ok      
                {      
                    //获取响应后的数据      
                    StreamReader reader = new StreamReader(res.GetResponseStream(), enc);      
                    Char[] read = new Char[256];      
                    int count = reader.Read(read, 0, 256);      
                    string str = null;      
                    while (count > 0)      
                    {      
                        str += new String(read, 0, count);      
                        count = reader.Read(read, 0, 256);      
                    }      
                    foreach (Cookie cookie in res.Cookies)      
                        cookies.Add(cookie);      
                    reader.Close();      
                    outstream.Close();      
                    res.Close();      
                    RegStr = str;      
                }      
            }      
            catch
            { }      
        }      
        public string getlm(string url, string fromstr)      
        {      
            Encoding enc = Encoding.GetEncoding("gb2312");      
            string RegStr = "系统错误,请重试";//返回的文字信息      
            try
            {      
                HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(url);      
                Request.CookieContainer = cookies;      
                Request.Accept = "text/html, application/xhtml+xml, */*";      
                Request.Method = "get";      
                Request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";      
                Request.ContentType = "application/x-www-form-urlencoded";      
                Request.Referer = fromstr;      
                Request.Headers["Accept-Language"] = "zh-cn";      
                //获取响应      
                HttpWebResponse res = (HttpWebResponse)Request.GetResponse();      
                if (res.StatusCode == HttpStatusCode.OK)//状态ok      
                {      
                    //获取响应后的数据      
                    StreamReader reader = new StreamReader(res.GetResponseStream(), enc);      
                    Char[] read = new Char[256];      
                    int count = reader.Read(read, 0, 256);      
                    string str = null;      
                    while (count > 0)      
                    {      
                        str += new String(read, 0, count);      
                        count = reader.Read(read, 0, 256);      
                    }      
                    foreach (Cookie cookie in res.Cookies)      
                        cookies.Add(cookie);      
                    reader.Close();      
                    res.Close();      
                    RegStr = str;      
                }      
            }      
            catch
            { }      
            return RegStr;      
        }      
    }      
}

再看主窗体下的代码。

using System;    
using System.Collections.Generic;    
using System.ComponentModel;    
using System.Data;    
using System.Drawing;    
using System.Text;    
using System.Windows.Forms;    
using System.Text.RegularExpressions;    
namespace 织梦登录    
{    
    public partial class Form1 : Form    
    {    
        public Form1()    
        {    
            InitializeComponent();    
        }    
          
        private void button1_Click(object sender, EventArgs e)    
        {    
            XiaoBin xb = new XiaoBin();    
            xb.Login("http://localhost/dede/login.php",    
                "http://localhost/dede/login.php",    
                "gotopage=&dopost=login&adminstyle=newdedecms&userid=admin&pwd=a123456");    
            //该方法有返回值 字符串    
          
            string s = xb.getlm("http://localhost/dede/article_add.php?channelid=1",    
                "http://localhost/dede/index_menu.php");//该方法有返回值 字符串    
          
            List<string> lms = new List<string>();    
            lms = GetLMs(s);    
            foreach (string i in lms)    
                textBox1.AppendText(i+Environment.NewLine);    
          
        }    
          
        /// <summary>    
        /// 获取栏目数组    
        /// </summary>    
        /// <param name="lmstr"></param>    
        /// <returns></returns>    
        public List<string> GetLMs(string lmstr)    
        {    
            //栏目名称中不能含有─    
            lmstr = lmstr.Remove(lmstr.IndexOf("【<u>选择副栏目</u>】"));    
            List<string> lms = new List<string>();    
            Regex reg = new Regex("<option value=.*?</option>");    
            MatchCollection m = reg.Matches(lmstr);    
            string lm = "";    
            string lid = "";    
            foreach (Match mm in m)    
                if (!mm.Value.ToString().Contains("<option value='0'>请选择栏目...</option>"))    
                {    
                    lm = mm.Value.ToString();    
                    lm = lm.Substring(lm.IndexOf("'") + 1);    
                    lid = lm.Remove(lm.IndexOf("'"));    
                    lm = lm.Substring(lm.IndexOf(">") + 1);    
                    lm = lm.Remove(lm.IndexOf("</option>"));    
                    lm = lm.Replace("─", "");//去掉子栏目标示符号    
                    lm = lid + "|" + lm;    
                    lms.Add(lm);    
                }    
          
            return lms;    
        }    
    }    
}