天际说

DirectAdminAPI的C#类库

发布时间:2014年8月10日 / 分类:.NET学习 / 6,981 次围观 / 抢沙发!

今天做一个项目的时候需要与DirectAdmin的api通信,但翻遍google百度竟然没有一个C#的类库,实在是汗。。。
所以本着逼急了就会了的原则。。自己写了个,用起来还行,基本的框架有了,扩展其他功能的话应该也很简单。就在这里共享一下吧。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections.Specialized;
using System.Net;
namespace WindowsFormsApplication1
{
    class DirectAdmin
    {
        private string server,  username, password, method,port = "2222";
        /// <summary>
        /// DirectAdmin类构造函数
        /// </summary>
        /// <param name="server">服务器FQDN</param>
        /// <param name="port">DirectAdmin的端口,默认为2222</param>
        /// <param name="username">DirectAdmin管理员/分销用户名</param>
        /// <param name="password">DirectAdmin管理员/分销密码</param>
        /// <param name="method">DirectAdmin访问方式。默认为http,如有ssl请使用https</param>
        /// <returns></returns>
        public DirectAdmin(string server, string port, string username, string password, string method = "http")
        {
            this.server = server;
            this.port = port;
            this.username = username;
            this.password = password;
            this.method = method;
        }
        public static string ConvertToBasic64(string StrValve)
        {
            return StrValve != null ? Convert.ToBase64String(Encoding.UTF8.GetBytes(StrValve)) : null;
        }
        public static string ConvertFromBasic64(string StrValve)
        {
            return StrValve != null ? Encoding.UTF8.GetString(Convert.FromBase64String(StrValve)) : null;
        }
        /// <summary>
        /// 检查用户的登录凭据是否有效
        /// </summary>
        /// <param name="user">用户名</param>
        /// <param name="passwd">密码</param>
        /// <returns>如果有效则返回true,否则返回false</returns>
        public bool LoginCheck(string user, string passwd)
        {
            NameValueCollection parameters = new NameValueCollection();
            NameValueCollection result = new NameValueCollection();
            parameters.Add("user", user);
            parameters.Add("passwd", passwd);
            result = CallApiByPost("CMD_API_VERIFY_PASSWORD", parameters);
            if (result[0] != "1")
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        /// <summary>
        /// 查询用户资源占用
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns>返回一个包含用户资源占用信息的键值对</returns>
        public NameValueCollection ShowUserUsage(string username)
        {
            NameValueCollection parameters = new NameValueCollection();
            NameValueCollection result = new NameValueCollection();
            username = System.Web.HttpUtility.UrlEncode(username).Replace("+", "%20");
            parameters.Add("user", username);
            MessageBox.Show(username);
            result = CallApiByGet("CMD_API_SHOW_USER_USAGE", parameters);
            return result;
        }
        /// <summary>
        /// 列出所有的用户套餐
        /// </summary>
        /// <returns>返回一个包含所有用户套餐的键值对</returns>
        public NameValueCollection CMD_API_PACKAGES_USER()
        {
            NameValueCollection parameters = new NameValueCollection();
            NameValueCollection result = new NameValueCollection();
            username = System.Web.HttpUtility.UrlEncode(username).Replace("+", "%20");
            result = CallApiByPost("CMD_API_PACKAGES_USER", parameters);
            return result;
        }
        /// <summary>
        /// 通过POST方式调用DirectAdmin接口
        /// </summary>
        /// <param name="command">命令</param>
        /// <param name="parameters">参数</param>
        /// <returns>返回包含执行结果的键值对</returns>
        public NameValueCollection CallApiByPost(string command, NameValueCollection parameters)
        {
            WebClient DAclient = new WebClient();
            NameValueCollection result = new NameValueCollection();
            DAclient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//POST method
            DAclient.Headers.Add("Authorization: Basic " + ConvertToBasic64(username + ":" + password));
            MessageBox.Show(method + "://" + server + ":" + port + "/" + command);
            Byte[] responseData = DAclient.UploadValues(method + "://" + server + ":" + port + "/" + command, "POST", parameters);
            // Decode and display the response.
            string response = Encoding.UTF8.GetString(responseData).ToString();
            response = System.Web.HttpUtility.UrlDecode(response);
            string[] prop = response.Split(new char[] { '&' }), tempvalue = new string[2];
            int rows = 0;
            rows = prop.GetLength(0);
            string[,] ResultText = new string[rows, 2];
            for (int i = 0; i < rows; i++)
            {
                tempvalue = prop[i].Split(new char[] { '=' });
                result.Add(tempvalue[0], tempvalue[1]);
            }
            return result;
        }
        /// <summary>
        /// 通过GET方式调用DirectAdmin接口
        /// </summary>
        /// <param name="command">命令</param>
        /// <param name="parameters">参数</param>
        /// <returns>返回包含执行结果的键值对</returns>
        public NameValueCollection CallApiByGet(string command, NameValueCollection parameters)
        {
            WebClient DAclient = new WebClient();
            NameValueCollection result = new NameValueCollection();
            //DAclient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//POST method
            DAclient.Headers.Add("Authorization: Basic " + ConvertToBasic64(username + ":" + password));
            //MessageBox.Show(method + "://" + server + ":" + port + "/" + command);
            string p = "?";
            for (int i = 0; i < parameters.Count;i++ )
            {
                p += parameters.GetKey(i).ToString() + "=" + parameters[i] + "&";
            }
            MessageBox.Show(p);
            string responseData = DAclient.DownloadString(method + "://" + server + ":" + port + "/" + command + p );
            // Decode and display the response.
            responseData = System.Web.HttpUtility.UrlDecode(responseData);
            MessageBox.Show(responseData);
            string[] prop = responseData.Split(new char[] { '&' }), tempvalue = new string[2];
            int rows = 0;
            rows = prop.GetLength(0);
            string[,] ResultText = new string[rows, 2];
            for (int i = 0; i < rows; i++)
            {
                tempvalue = prop[i].Split(new char[] { '=' });
                result.Add(tempvalue[0], tempvalue[1]);
            }
            return result;
        }
    }
}

标签:none