title: c# windows phone and store app post data to php with cookie
body: <p>I'm working with one of user controlled application. This app is connecting with PHP files to login and do any other processes that requested. </p>
  
  <p>in PHP I'm using session for detect if user already login. When they login successfully php sets and send login token id successfully.</p>
  
  <p>I already working with IOS for same project and in IOS login process is working fine. IOS uses cookies automatically but I couldn't use cookies for windows app. Session has named cookie token id and php file checks token id with posted. I stuck in windows but I have no problem with IOS.</p>
  
  <p>In windows store and phone app I can post perfectly and I can get respond message too. I can login perfectly as well. But when I navigate to second page of app it checks we are login or not again. When it check I'm facing with not logged in message. In my IOS app run perfectly. But in windows session token id check fails..</p>
  
  <p>this is my php file that checks session token id in second page:</p>
  
  <pre><code>$tokencheck = $_POST['Tokenid'];
  
  if($_SESSION["token"] == $tokencheck){
  </code></pre>
  
  <p>first page is login page and when login successfull windows phone gets token id and saves it successfully. After login, in a second page saved token id posting to php that I shared. I checked token id and thats true.</p>
  
  <p>I do some research and I found problem is cookies. I can't use cookies right now. I did some more codes but still I stuck and couldn't solved this problem.</p>
  
  <p>Codes sends post perfectly and also gets respond messages perfectly but I couldn't check session token id, this is because login check fails.</p>
  
  <p>First page, first attempt with httpClient</p>
  
  <pre><code>var values = new List&lt;KeyValuePair&lt;string, string&gt;&gt;
  {
      new KeyValuePair&lt;string, string&gt;("Email", txtEmail.Text),
      new KeyValuePair&lt;string, string&gt;("Password", txtPassword.Password)
  };
  
  string url = ".../login.php";      
  CookieContainer Cookiejar = new CookieContainer();
  var handler = new HttpClientHandler
  {
      CookieContainer = Cookiejar,
      UseCookies = true//,
     // UseDefaultCredentials = false
  };
  
  var httpClient = new HttpClient(handler);
  httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
  HttpResponseMessage response = await httpClient.PostAsync(url, new FormUrlEncodedContent(values));
  response.EnsureSuccessStatusCode();
  var responseString = await response.Content.ReadAsStringAsync();
  
  var wwwUri = new Uri(url);
  Cookiejar.SetCookies(wwwUri, Cookiejar.GetCookieHeader(wwwUri));
  </code></pre>
  
  <p>First page second attempt with HttpWebRequest</p>
  
  <pre><code>CookieContainer cookies = new CookieContainer();
  HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(url);
  getRequest.CookieContainer = cookies;
  getRequest.Method = "POST";
  
  
  HttpWebResponse response = (HttpWebResponse)await getRequest.GetResponseAsync();
  
  var sr = new StreamReader(response.GetResponseStream() );
  
  string responseString = sr.ReadToEnd();
  </code></pre>
  
  <p>I also tried some other codes that I found in internet but can't solved yet.</p>
  
  <p>Second page is also :</p>
  
  <pre><code>object lgntoken = Windows.Storage.ApplicationData.Current.LocalSettings.Values["logintokenid"];
  
  var values = new List&lt;KeyValuePair&lt;string, string&gt;&gt;
  {
      new KeyValuePair&lt;string, string&gt;("Tokenid", Convert.ToString(lgntoken))
  };
  
  string url = ".../get_projects_list.php";
  
  CookieContainer Cookiejar = new CookieContainer();
  var wwwUri = new Uri(url);
  // Cookiejar.SetCookies(wwwUri, Cookiejar.GetCookieHeader(wwwUri));
  Cookiejar.GetCookieHeader(wwwUri);
  
  var handler = new HttpClientHandler
  {
      CookieContainer = Cookiejar,
      UseCookies = true
  };
  
  System.Diagnostics.Debug.WriteLine("Login Token: " + Convert.ToString(lgntoken) + "..");
  
  
  var httpClient = new HttpClient(handler);
  HttpResponseMessage response = await httpClient.PostAsync(url, new FormUrlEncodedContent(values));
  response.EnsureSuccessStatusCode();
  var responseString = await response.Content.ReadAsStringAsync();
  
  System.Diagnostics.Debug.WriteLine("Project List Data : " + responseString + "  ++ Login Token: " + Convert.ToString(lgntoken) + "..");
  </code></pre>
  
  <p>I have some experiance in IOS but I'm newbie in windows store apps. I stuck about this issue and already not understood what should I do, how can I set or get cookies in a first and second page. Waiting your helps, thank you. </p>