C#版Aliyun DNS API

     阿里云解析API,是为域名开发者、注册商、域名代理商等提供的开放和便捷的解析服务接口。API依托于万网云解析服务,可以方便的管理域名和解析记录,让你的解析管理变的随心省时自由舒畅。

一、先附上Aliyun 云解析API地址

二、再附上整理后的C# 源码地址

1、在编写代码过程中,遇到了一个问题。

问题情况如下:

通常情况下,C#可以通过多种方式获取Html响应内容

以下列出常用的两种方式:

a、通过HttpWebRequest

        public static HttpWebResponse GetUrlResponse(string url, HttpVerb method)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = method.ToString();
            req.ContentType = "application/json;charset=UTF-8";//text/plain; charset=utf-8
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            return res;
        }

b、通过WebClient

            using (WebClient client = new WebClient())
            {
                return client.DownloadString(url);
            }

在此处,如果使用上述的两种方法去获取响应的话,在Aliyun云解析各参数正确的情况下,可以正常的获取http响应消息,但是当参数不准确时,在firefox中可以显示如下错误消息;在程序中将直接抛出500或者400错误,无法获取到准确的html正文内容;

为了解决这个问题,在程序中采用socket方式去获取html消息内容,然后在针对返回的响应消息进行解析,http返回状态为200 ok时,解析为响应消息,非200错误码时解析为ErrorMessage

        private static byte[] GetRequestHeaders(Uri uri, HttpVerb method)
        {
            WebHeaderCollection webheaders = new WebHeaderCollection();
            webheaders.Add("Host", uri.Host);
            webheaders.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            webheaders.Add("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
            webheaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0");

            StringBuilder header = new StringBuilder();
            header.AppendFormat("{0} {1} HTTP/1.1\r\n", method, uri.PathAndQuery);
            foreach (string key in webheaders)
            {
                header.AppendFormat("{0}:{1}\r\n", key, webheaders[key]);
            }
            header.Append("\r\n");
            webheaders.Clear();
            return Encoding.UTF8.GetBytes(header.ToString());
        }
        public static string GetUrlHtmlContentBySocket(string url, HttpVerb method)
        {
            using (TcpClient client = new TcpClient())
            {
                Uri uri = new Uri(url);
                client.Connect(uri.Host, uri.Port);
                byte[] buff = GetRequestHeaders(uri, method);
                client.Client.Send(buff);
                using (NetworkStream stream = client.GetStream())
                {
                    byte[] bytes = new byte[1048576];
                    int size = stream.Read(bytes, 0, bytes.Length);
                    string html = Encoding.UTF8.GetString(bytes, 0, size);
                    return html;
                }
            }
        }

以上代码在源码以包含。

2、在使用过程中主要还使用了AliyunRequest类以及派生类、AliyunUtils类、AliyunResponse接口以及继承该接口的Response类等

(1)、AliyunRequest为阿里云公共请求消息

public class AliyunRequest
{
    // Fields
    private string ACCESS_KEY_ID;
    private string ACCESS_KEY_SECRET;
    private ResponseFormat API_FORMAT;
    private string API_VERSION;
    private HttpVerb HTTP_METHOD;
    private string SEARCH_BASE_URL;
    private string SIGNATURE_METHOD;
    private string SIGNATURE_VERSION;

    // Methods
    public AliyunRequest();
    public virtual Dictionary<string, string> GeneralParameters();

    // Properties
    public string Access_Key_Secret { get; set; }
    public string AccessKeyId { get; set; }
    public virtual ActionType Action { get; }
    public string Base_Url { get; set; }
    public ResponseFormat Format { get; set; }
    public HttpVerb Http_Method { get; set; }
    public string SignatureMethod { get; set; }
    public string SignatureVersion { get; set; }
    public string Version { get; set; }
}

 (2)、AliyunUtils工具类

public class AliyunUtils
{
    // Fields
    private static AliyunRequest AliyunRequest;

    // Methods
    static AliyunUtils();
    public AliyunUtils();
    private static AliyunResponse _analyzeJson(ActionType action, string content);
    private static AliyunResponse _analyzeResponse(ActionType type, ResponseFormat format, string content);   
    public static string GeneralURL(AliyunRequest request);
    public static string GeneralURL(Dictionary<string, string> parameters);
    public static ErrorMessage GetErrorMessage(ErrorResponse error);
    public static AliyunHtmlResponse GetHtmlResponse(string html);
    public static AliyunHtmlResponse GetHtmlResponse(string html, ResponseFormat format);
    public static AliyunResponse GetResponse(AliyunRequest request);
    public static AliyunResponse GetResponse(ActionType type, Dictionary<string, string> parameters);
    public static void Init(AliyunRequest AliyunRequest);
} 

 

三、再附上对应的chm文档

 以下为包含的结构:

使用方法如下图所示

 

分别执行,得到的_res和_res2结构应该是相同的。

 

代码中可能存在不足的地方,还请指出,也希望这对大家有所帮助

posted @ 2015-04-20 17:48  Smallbyte  阅读(1547)  评论(1编辑  收藏  举报