小米OAuth2.0 C#版SDK源代码学习

因为,最近在做和小米账号胡同的项目,所以分析学习了关于小米C#版SDK的开放源代码,在这里进行总结和汇总,毕竟编程的学习就是不断的学习别人的代码,取长补短,扩展思路。

获取网页内容的方法

获取html网页内容,该方法是为了获取返回网页内容,以获取必要的接口信息。

/// 
/// 获取html网页内容
/// 
/// 网址代码
/// 请求的方法
/// HTTP标头键值对集合
/// 
public static string getResponse(string uri, string method, Dictionary<string,string> headers)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
    request.Method = method;
    if (headers != null)
    {
        foreach (KeyValuePair<string, string> headersKey in headers)
        {
            request.Headers.Add(headersKey.Key, headersKey.Value);
        }
    }
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
    string respHtml;
    try
    {
        respHtml = reader.ReadToEnd();
    }
    catch (Exception e)
    {
        throw e;
    }
    finally
    {
        reader.Close();
    }
    return respHtml;
}

Uri地址构建自定义类,是为了取代常用的拼接字符串方法实现查询字符串地址的拼接,开发者写了一个自定义的类继承了微软框架中的UriBuilder类,增加了查询字符串的拼接和并入方法。

Uri地址构建自定义改进类

    /// 
    /// Uri地址构建自定义改进类
    /// 
    public class UriBuilderImprove : UriBuilder
    {
        //查询字符串键值对集合
        Dictionary<string, string> queryString = null;
        #region Properties
        public Dictionary<string, string> QueryString
        {
            get
            {
                if (queryString == null)
                {
                    queryString = new Dictionary<string, string>();
                }
                return queryString;
            }
        }

        public string PageName
        {
            get
            {
                string path = base.Path;
                return path.Substring(path.LastIndexOf("/") + 1);
            }
            set
            {
                string path = base.Path;
                path = path.Substring(0, path.LastIndexOf("/"));
                base.Path = string.Concat(path, "/", value);
            }
        }
        #endregion

        #region 重载构造函数
        public UriBuilderImprove():base(){}
        public UriBuilderImprove(string uri):base(uri)
        {
            PopulateQueryString();
        }

        public UriBuilderImprove(Uri uri): base(uri)
        {
            PopulateQueryString();
        }
        public UriBuilderImprove(string schemeName, string hostName): base(schemeName, hostName){}
        public UriBuilderImprove(string scheme, string host, int portNumber): base(scheme, host, portNumber){}
        public UriBuilderImprove(string scheme, string host, int port, string pathValue): base(scheme, host, port, pathValue){}
        public UriBuilderImprove(string scheme, string host, int port, string path, string extraValue): base(scheme, host, port, path, extraValue){}
        #endregion
        /// 
        /// 获取据对的Uri地址字符串
        /// 
        /// 
        public new string ToString()
        {
            GetQueryString();
            return base.Uri.AbsoluteUri;
        }
        /// 
        /// 填入查询字符串
        /// 
        private void PopulateQueryString()
        {
            string query = base.Query;
            if (query == string.Empty || query == null)
            {
                return;
            }
            if (queryString == null)
            {
                queryString = new Dictionary<string, string>();
            }
            queryString.Clear();
            query = query.Substring(1); //remove the ?
            string[] pairs = query.Split(new char[] { '&' });
            foreach (string s in pairs)
            {
                string[] pair = s.Split(new char[] { '=' });

                queryString[pair[0]] = (pair.Length > 1) ? pair[1] : string.Empty;
            }
        }
        /// 
        /// 拼接查询字符串
        /// 
        private void GetQueryString()
        {
            int count = queryString.Count;
            if (count == 0)
            {
                base.Query = string.Empty;
                return;
            }
            string[] keys = new string[count];
            string[] values = new string[count];
            string[] pairs = new string[count];
            queryString.Keys.CopyTo(keys, 0);
            queryString.Values.CopyTo(values, 0);
            for (int i = 0; i < count; i++)
            {
                pairs[i] = string.Concat(keys[i], "=", Uri.EscapeDataString(values[i]));
            }
            base.Query = string.Join("&", pairs);
        }
    }

整个小米开发OAuth2.0的SDK核心的代码就这么多,一个拼接接口地址,一个根据拼接成的uri地址返回所需的小米用户数据。剩下的SDK代码主要是涉及一些小米OAuth的专用名词,uri,和一些加密算法,大家都可以在小米开发者文档中找到,这里就不在占用篇幅了。

你可能感兴趣的:(源码分析)