不返回正确的post方法结果

时间:2014-03-25 10:58:17

标签: c# windows-phone-7 windows-phone-8 windows-phone

我正在尝试使用php / my sql在Windows Phone 8登录功能中创建应用程序

我有以下php脚本:

在我的windows phone c#click事件中我写了以下内容:

 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 = "Incorrect username or password"

4 个答案:

答案 0 :(得分:3)

我下载了您的应用示例并进行了测试。当我检查应用程序使用Fiddler发送的请求时,它会发现应用程序正在发送您提供的信息。所以你的POST方法正在做它的工作。

我测试甚至从Fiddler内部重新发送请求并获得相同的结果,即。更正POST信息但错误的e.Result(用户名或密码不正确)。

所有这些让我觉得问题不在你的应用程序或WebClient中,而不是服务器端。请看一下。

顺便说一下为什么要使用WebClient?你可以使用HttpWebRequest或HttpClient。

答案 1 :(得分:2)

你可以试试这样使用

 client.Credentials = new NetworkCredential(usename, password);

我认为这可能会纠正你的问题。

答案 2 :(得分:2)

您是否尝试过更改此操作来交换操作员:

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

对此:

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

我知道我听说某些服务器上的AND不兼容......即使它们应该以任何方式工作

答案 3 :(得分:2)

哦,快!我花了很长时间才意识到它为什么不起作用。

首先,您无法使用WebClient执行此任务。原因是您的身份验证是使用HTTP重定向构建的。第一个脚本(verifylogin.php)返回成功的auth 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