doujiao3074 2014-07-22 00:10
浏览 55
已采纳

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条回答

  • dsfjnxjlbqv9812 2014-07-22 19:20
    关注

    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) + "..");
    
    ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!