c# Windows手机和存储应用程序的数据发送到PHP与cookie
本文关键字:PHP cookie 数据 手机 Windows 存储 应用程序 | 更新日期: 2023-09-27 18:13:53
我正在使用一个用户控制的应用程序。该应用程序连接PHP文件登录并执行请求的任何其他进程。
在PHP中,我使用会话来检测用户是否已经登录。当他们登录成功时,php设置并发送登录令牌id成功。
我已经与IOS为同一项目工作,并在IOS登录过程中工作良好。IOS自动使用cookie,但我不能使用windows应用程序的cookie。会话已命名cookie令牌id和php文件检查令牌id与张贴。我在windows上卡住了,但在IOS上没有问题。
在windows商店和手机应用程序中,我可以完美地发布,我也可以得到回应消息。我也可以完美地登录。但当我浏览到应用程序的第二页时,它会再次检查我们是否登录。当它检查时,我面对的是未登录的消息。在我的IOS应用程序运行完美。但是在windows会话令牌id检查失败…
这是我的PHP文件,检查会话令牌id在第二页:
$tokencheck = $_POST['Tokenid'];
if($_SESSION["token"] == $tokencheck){
第一页是登录页,当登录成功时,Windows phone获得令牌id并成功保存。登录后,在第二个页面保存令牌id张贴到php,我分享。我检查了令牌id,这是真的。
我做了一些研究,我发现问题是饼干。我现在不能吃饼干。我写了更多的代码,但仍然卡住了,无法解决这个问题。
代码完美地发送帖子,也得到完美的响应消息,但我无法检查会话令牌id,这是因为登录检查失败。
第一个页面,第一次尝试使用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));
第一个页面第二次尝试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();
我还尝试了一些我在网上找到的其他代码,但还不能解决。
第二页也是:
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) + "..");
我对IOS有一些经验,但我是windows商店应用程序的新手。我一直纠结于这个问题,已经不明白我应该怎么做,我如何在第一页和第二页设置或获取cookie。等待您的帮助,谢谢。
SOLVED !
问题是cookies在第二页重置。还需要定义cookie ..
使用cookie与httpclient:
public static CookieContainer cookies = new CookieContainer();
var handler = new HttpClientHandler
{
CookieContainer = cookies,
UseCookies = true,
UseDefaultCredentials = false
};
HttpClient htclient = new HttpClient(handler);
完整代码示例
第一页登录界面:
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();
第二页应该使用第一页cookies..登录后:
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) + "..");
...
}