不返回正确的POST方法结果

3

我想制作一个使用php/my sql的Windows Phone 8登录功能的应用程序。

我有以下php脚本:

在我的Windows Phone C#单击事件中,我写了以下内容:

 private void btnLogin_Click(System.Object sender, System.Windows.RoutedEventArgs e)
        {

            Uri uri = new Uri(url, UriKind.Absolute);

            StringBuilder postData = new StringBuilder();
            postData.AppendFormat("{0}={1}", "email", HttpUtility.UrlEncode("Test@test.com"));
            postData.AppendFormat("&{0}={1}", "pwd1", HttpUtility.UrlEncode("password"));

            WebClient client = default(WebClient);
            client = new WebClient();
            client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
            client.Headers[HttpRequestHeader.ContentLength] = postData.Length.ToString();

            client.UploadStringCompleted += client_UploadStringCompleted;
            client.UploadProgressChanged += client_UploadProgressChanged;

            client.UploadStringAsync(uri, "POST", postData.ToString());

            prog = new ProgressIndicator();
            prog.IsIndeterminate = true;
            prog.IsVisible = true;
            prog.Text = "Loading....";
            SystemTray.SetProgressIndicator(this, prog);

        }

        private void client_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
        {
            //Me.txtResult.Text = "Uploading.... " & e.ProgressPercentage & "%"
        }

        private void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            if (e.Cancelled == false & e.Error == null)
            {
                prog.IsVisible = false;

                string[] result = e.Result.ToString().Split('|');
                string strStatus = result[0].ToString();
                string strMemberID = result[1].ToString();
                string strError = result[2].ToString();

                if (strStatus == "0")
                {
                    MessageBox.Show(strError);
                }
                else
                {
                    NavigationService.Navigate(new Uri("/DetailPage.xaml?sMemberID=" + strMemberID, UriKind.Relative));
                }

            }
        }

我验证了电子邮件和密码的正确性,使用我放置的 C# 代码,但最终我总是收到类似于以下消息的信息:e.Result =“用户名或密码不正确”

为什么不尝试使用client.credentials事件? - Sandeep Chauhan
我不知道客户端凭据事件是什么,也不知道为什么需要它? :) - vir
4个回答

3
我下载了你的应用程序样本并进行了测试。当我使用Fiddler检查应用程序发送的请求时,似乎该应用程序确实发送了您提供的完全信息。因此,您的POST方法在发挥作用。
我甚至尝试从Fiddler内部重新发送请求并获得相同的结果,即:正确的POST信息但错误的e.Result(用户名或密码不正确)。
所有这些都让我感觉问题不在于你的应用程序或WebClient,而是服务器端。请查看一下。
顺便问一下,为什么要使用WebClient?你可以使用HttpWebRequest或HttpClient。

我不知道,看这个页面:http://unotez.com/other/app/login.php ,然后输入email:test@test.com和密码:password。 - vir

2

您是否尝试通过更改以下内容来切换您的运算符:

If($mypass = $row['password'] and $myname = $row['email'])
{
$successBit = 1;

转化为:

If($mypass = $row['password'] && $myname = $row['email'])
{
$successBit = 1;

我知道在一些服务器上,AND可能不兼容......即使它们应该可以正常工作。


2
你可以尝试像这样使用:
 client.Credentials = new NetworkCredential(usename, password);

我认为这可能会解决你的问题。

不,这并没有解决我的问题。我在问题中附上了源代码,您能下载并运行一次吗?这不会超过5分钟 :) - vir

2

哎呀,糟糕!我花了很长时间才意识到为什么它不起作用。

首先,您不能使用WebClient执行此任务。原因是您的身份验证是通过HTTP重定向构建的。第一个脚本(verifylogin.php)返回成功的身份验证cookie并将WebClient重定向到另一个脚本(loginstatus.php),该脚本检查cookie并显示消息。但是,WebClient根本不会将第一个脚本中的cookie传递到另一个脚本中,因此检查失败(这让您认为自己做错了什么)。

解决方案是改用WebRequest

    StringBuilder postData = new StringBuilder();
    postData.AppendFormat("{0}={1}", "email", HttpUtility.UrlEncode("test@test.com"));
    postData.AppendFormat("&{0}={1}", "pwd1", HttpUtility.UrlEncode("password"));

    var request = (HttpWebRequest)WebRequest.Create("http://unotez.com/other/app/verifylogin.php");
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.AllowAutoRedirect = true;

    // magic happens here!
    // create a cookie container which will be shared across redirects
    var cookies = new CookieContainer();
    request.CookieContainer = cookies;

    using (var writer = new StreamWriter(request.GetRequestStream()))
        writer.Write(postData.ToString());

    // sync POST request here, but you can use async as well
    var response = (HttpWebResponse)request.GetResponse();

    string data;
    using (var reader = new StreamReader(response.GetResponseStream()))
        data = reader.ReadToEnd();

    Console.WriteLine(data);

    // Success, Welcome: Test

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接