2017-09-05 190 views
0

我正在嘗試使用C#中的Json Post請求登錄到iCloud。在嘗試實施代碼之前,我正在使用Chrome控制檯研究一些iCloud請求,並使用Ad-on複製請求以獲取網站的相同結果。通過Json Post登錄到iCloud請求

首先我直接從iCloud的網站查請求:

enter image description here

這是響應:

{ 
"serviceErrors" : [ { 
"code" : "-20101", 
"message" : "Il tuo ID Apple o la password non sono corretti." 
} ] 
} 

使用 「高級REST客戶端」 廣告的Chrome插件複製請求我已經嘗試了相同的URL的相同的JSON請求。但是我得到空的響應: enter image description here

我也試圖複製並粘貼整個頭(所有設置),比發送請求,但響應是一樣的: enter image description here

任何人有一個建議嗎?

更新:我想實現通過C#程序JSON請求:

var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://idmsa.apple.com/appleauth/auth/signin"); 
     httpWebRequest.ContentType = "application/json"; 
     httpWebRequest.Method = "POST"; 

     using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) 
     { 
      string json = "{accountName: \"briesanji @gmail.com\", password: \"testPassword\", rememberMe: false, trustTokens: []}"; 

      streamWriter.Write(json); 
      streamWriter.Flush(); 
      streamWriter.Close(); 
     } 

     var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); 
     using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) 
     { 
      var result = streamReader.ReadToEnd(); 
     } 

的問題是,執行休息時

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); 

命中,這讓我這個錯誤:系統。 Net.WebException:'錯誤遠程服務器:(400)請求無效。'

更新:我以這種方式解決:

void POST(string url, string jsonContent) 
    { 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
     request.Method = "POST"; 

     System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); 
     Byte[] byteArray = encoding.GetBytes(jsonContent); 

     request.ContentLength = byteArray.Length; 
     request.ContentType = @"application/json"; 

     using (Stream dataStream = request.GetRequestStream()) 
     { 
      dataStream.Write(byteArray, 0, byteArray.Length); 
     } 
     long length = 0; 
     try 
     { 
      using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 
      { 
       length = response.ContentLength; 
      } 
     } 
     catch (WebException ex) 
     { 
      // Log exception and throw as for GET example above 
     } 
    } 

    string GET(string url) 
    { 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
     try 
     { 
      WebResponse response = request.GetResponse(); 
      using (Stream responseStream = response.GetResponseStream()) 
      { 
       StreamReader reader = new StreamReader(responseStream, Encoding.UTF8); 
       return reader.ReadToEnd(); 
      } 
     } 
     catch (WebException ex) 
     { 
      WebResponse errorResponse = ex.Response; 
      using (Stream responseStream = errorResponse.GetResponseStream()) 
      { 
       StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")); 
       String errorText = reader.ReadToEnd(); 
       // log errorText 
      } 
      throw; 
     } 
    } 

反正我還測試了答案,這是很好的。所以我檢查它作爲有效的感謝。

+0

爲什麼C#如果你想有一個JSON請求從鉻?無論如何,就我所見,您在Chrome轉儲和其他客戶端中使用完全相同的cookie數據和相同的widgetKey。我認爲你必須創建一個沒有widget鍵的原始uri的請求,並從這個響應中獲取widgetKey和cookie數據 - 然後你發送你的登錄數據到這個新的uri ... **未經測試** – Michael

+0

諾諾我想使用C#代碼..但正如你可以閱讀我的帖子的第一行兩個..我只是想測試應該如何設置Json請求..無論如何..在我的第二個ScreenShot我做了你說的和我在AnyCase中我沒有任何結果..我會盡快用C#代碼更新這篇文章。 – Briesanji

+0

在你的第二個屏幕截圖中,你發送了uri中沒有widgetKey的登錄數據。並在你的第三個屏幕截圖中使用了相同的目標URI ... – Michael

回答

1

有了這個,我沒有得到任何錯誤,第二個請求,只是告訴我,有測試帳戶過多失敗的登錄響應內容...

private static void ICloud() 
    { 
     var cc = new CookieContainer(); 

     var first = (HttpWebRequest)WebRequest.Create("https://idmsa.apple.com/appleauth/auth/signin?widgetKey=83545bf919730e51dbfba24e7e8a78d2&locale=de_DE&font=sf"); 
     first.Method = "GET"; 
     first.CookieContainer = cc; 

     var response1 = (HttpWebResponse)first.GetResponse(); 
     using (var streamReader = new StreamReader(response1.GetResponseStream())) 
     { 
      var result = streamReader.ReadToEnd(); 
     } 

     var second = (HttpWebRequest)WebRequest.Create("https://idmsa.apple.com/appleauth/auth/signin"); 
     second.ContentType = "application/json"; 
     second.Method = "POST"; 
     second.Accept = "application/json"; 
     second.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); 
     second.Referrer = "https://idmsa.apple.com/appleauth/auth/signin?widgetKey=83545bf919730e51dbfba24e7e8a78d2&locale=de_DE&font=sf"; 
     second.Headers.Add("X-Requested-With", "XMLHttpRequest"); 
     second.Headers.Add("X-Apple-Widget-Key", "83545bf919730e51dbfba24e7e8a78d2"); 

     using (var streamWriter = new StreamWriter(second.GetRequestStream())) 
     { 
      string json = "{\"accountName\":\"[email protected]\",\"password\":\"test\",\"rememberMe\":false,\"trustTokens\":[]}"; 

      streamWriter.Write(json); 
      streamWriter.Flush(); 
      streamWriter.Close(); 
     } 

     try 
     { 
      var response2 = (HttpWebResponse)second.GetResponse(); 
      using (var streamReader = new StreamReader(response2.GetResponseStream())) 
      { 
       var result = streamReader.ReadToEnd(); 
      } 
     } 
     catch(WebException we) 
     { 
      using (var r = new StreamReader(we.Response.GetResponseStream())) 
      { 
       var result2 = r.ReadToEnd(); 
      } 
     } 
    } 
+0

你也得到一個長的Html文件作爲響應嗎? – Briesanji

+0

@Briesanji是的。我得到了icloud的洞html頁面.com ...的消息是,有太多的登錄嘗試和帳戶需要解鎖... – Michael

+0

Mhhhh ..是我得到的相同的消息,當我嘗試使用我的真實憑證登錄時。答案几乎是正確的,但有些東西我錯過了(我試圖用Icloud站點上的帳戶登錄,並且一切順利) 我也收到了使用我的電子郵件並插入錯誤密碼的錯誤。 。但是我期望在pwd錯誤的情況下做出這種響應:「服務錯誤」:[「code」:「-20101」, 「message」:「Il tuo ID Apple o la password non sono corretti。」 }] }「 – Briesanji