Created
June 27, 2014 06:38
-
-
Save peterhpchen/0fcc1fdb620d9fdb2cf1 to your computer and use it in GitHub Desktop.
Automate login apple developer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment