c#windows phone和store app将数据发布到带有cookie的php

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.

in PHP I'm using session for detect if user already login. When they login successfully php sets and send login token id successfully.

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.

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..

this is my php file that checks session token id in second page:

$tokencheck = $_POST['Tokenid'];

if($_SESSION["token"] == $tokencheck){

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.

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.

Codes sends post perfectly and also gets respond messages perfectly but I couldn't check session token id, this is because login check fails.

First page, first attempt with httpClient

var values = new List<KeyValuePair<string, string>>
{
    new KeyValuePair<string, string>("Email", txtEmail.Text),
    new KeyValuePair<string, string>("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));

First page second attempt with HttpWebRequest

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();

I also tried some other codes that I found in internet but can't solved yet.

Second page is also :

object lgntoken = Windows.Storage.ApplicationData.Current.LocalSettings.Values["logintokenid"];

var values = new List<KeyValuePair<string, string>>
{
    new KeyValuePair<string, string>("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) + "..");

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.

1个回答

SOLVED !

problem is cookies resets in second page. And also need define cookies..

using cookies with httpclient :

public static CookieContainer cookies = new CookieContainer();
 var handler = new HttpClientHandler
            {
                CookieContainer = cookies,
                UseCookies = true,
                UseDefaultCredentials = false
            };



            HttpClient htclient = new HttpClient(handler);

Full code example

First page login screen :

       public static CookieContainer cookies = new CookieContainer();
 private async void btnLogin_Click(object sender, RoutedEventArgs e)
        {
...
var values = new List<KeyValuePair<string, string>>
                {
                    new KeyValuePair<string, string>("Email", txtEmail.Text),
                    new KeyValuePair<string, string>("Password", txtPassword.Password)
                };

        string url = ".../login.php";




        var handler = new HttpClientHandler
        {
            CookieContainer = cookies,
            UseCookies = true,
            UseDefaultCredentials = false
        };



        HttpClient htclient = new HttpClient(handler);

        htclient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
        HttpResponseMessage response = await htclient.PostAsync(url, new FormUrlEncodedContent(values));
        response.EnsureSuccessStatusCode();
        var responseString = await response.Content.ReadAsStringAsync();

Second page should use first page cookies.. After login :

  var values = new List<KeyValuePair<string, string>>
                {
                    new KeyValuePair<string, string>("Tokenid", Convert.ToString(lgntoken))
                };

           string url = ".../get_projects_list.php";




           var handler = new HttpClientHandler
           {
               CookieContainer = MainPage.cookies,
               UseDefaultCredentials = true,
               UseCookies = true

           };

           System.Diagnostics.Debug.WriteLine("Login Token: " + Convert.ToString(lgntoken) + "..");


         //  var httpClient = new HttpClient(handler);

           HttpClient htclient = new HttpClient(handler);
           htclient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
           HttpResponseMessage response = await htclient.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) + "..");

...
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐