title: Login into iCloud via Json Post request
body: <p>I'm trying to log in into iCloud using a Json Post request in C#. Before trying to implement the code I was studying a little bit the iCloud requests using Chrome Console and using an Ad-on to replicate the requests in order to obtain the same result of the website.</p>
  
  <p>First of All I checked the request directly from iCloud website:</p>
  
  <p><a href="https://i.stack.imgur.com/pZKvw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pZKvw.png" alt="enter image description here"></a></p>
  
  <p>And this is the response:</p>
  
  <pre><code>{
   "serviceErrors" : [ {
   "code" : "-20101",
   "message" : "Il tuo ID Apple o la password non sono corretti."
   } ]
  }
  </code></pre>
  
  <p>Using "Advance REST Client" ad Chrome plugin to replicate the request I ve tried the same Json request to the same Url. But I get Empty response:
  <a href="https://i.stack.imgur.com/MIvMv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MIvMv.png" alt="enter image description here"></a></p>
  
  <p>I Also tried to copy and paste the whole Header (All the settings) and than send the request but the response is the same:
  <a href="https://i.stack.imgur.com/xL5uu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xL5uu.png" alt="enter image description here"></a></p>
  
  <p>Anyone has an Advice? </p>
  
  <p>UPDATE: I tried to implement A Json request through c# program:</p>
  
  <pre><code>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();
          }
  </code></pre>
  
  <p>The problem is that Execution breaks when the</p>
  
  <pre><code>var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
  </code></pre>
  
  <p>is hit and it gives me this error: System.Net.WebException: 'Error Remote Server: (400) Request not valid.'</p>
  
  <p>UPDATE: I solved in this way:</p>
  
  <pre><code>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;
          }
      }
  </code></pre>
  
  <p>Anyways I tested also the Answer and it was good to.. So I check it as valid thanks.</p>