peterhpchen
6/27/2014 - 6:38 AM

Automate login apple developer

Automate login apple developer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using HtmlAgilityPack;

namespace AppleDeveloper
{
    public class Login
    {
        /// <summary>
        /// 將取得的cookies存入,讓使用者可以使用
        /// </summary>
        public CookieContainer cookies { get; set; }
        /// <summary>
        /// 登入apple developer有兩個步驟
        /// 1.將真的post的uri給找出來
        /// 2.將帳號密碼post取回cookies
        /// 上述兩步驟做完之後會得到存著登入資訊的cookies,就可以利用這cookies去瀏覽其他的網頁
        /// </summary>
        /// <param name="appleId">apple developer ID</param>
        /// <param name="password">apple developer password</param>
        public Login(string appleId, string password)
        {
            string postUri = requestPostUri();
            this.cookies = requestLogin(postUri, appleId, password);
        }
        /// <summary>
        /// 進行登入的動作,登入之後將cookies回傳以便下一次的使用
        /// </summary>
        /// <param name="postUri">post帳號及密碼的uri</param>
        /// <param name="appleId">apple developer ID</param>
        /// <param name="password">apple developer password</param>
        /// <returns>已經記錄登入資料的cookies</returns>
        private CookieContainer requestLogin(string postUri, string appleId, string password) 
        {
            HttpWebRequest request = WebRequest.Create(postUri) as HttpWebRequest;
            
            CookieContainer cContainer = new CookieContainer();
            string data = string.Format("appleId={0}&accountPassword={1}", appleId, password);
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] dataByte = encoding.GetBytes(data);

            request.CookieContainer = new CookieContainer();
            request.Method = WebRequestMethods.Http.Post;
            request.ContentType = "application/x-www-form-urlencoded";

            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(dataByte, 0, dataByte.Length);
            }
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                cContainer = request.CookieContainer;
            }
            return cContainer;
        }
        /// <summary>
        /// 找出真正post的uri
        /// </summary>
        /// <returns>要post的uri</returns>
        private string requestPostUri()
        {
            HttpWebRequest request = WebRequest.Create("https://developer.apple.com/register/completeRegistration.action") as HttpWebRequest;

            string postUri = "https://idmsa.apple.com/IDMSWebAuth/";
            string refererUri = string.Empty;

            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                HtmlNode formNode = getNodeByXPath("//*[@id=\"ds_container\"]/form", response.GetResponseStream());
                postUri = postUri + formNode.Attributes["action"].Value;
            }
            return postUri;
        }
        /// <summary>
        /// 利用HtmlAgilityPack分析html將想找的元素節點取出
        /// </summary>
        /// <param name="xPath">想要找的元素的XPath</param>
        /// <param name="responseStream">想要找的網頁的資料流</param>
        /// <returns>要找的元素的HtmlNode</returns>
        private HtmlNode getNodeByXPath(string xPath, Stream responseStream)
        {
            HtmlDocument doc = new HtmlDocument();
            doc.Load(responseStream);
            HtmlNode node = doc.DocumentNode.SelectSingleNode(xPath);
            return node;
        }
    }
}