not return correct post method result

2019-07-03 21:58发布

问题:

I'm trying to make an application in windows phone 8 login functionality using php/my sql

i have following php script :

in my windows phone c# click event i wrote following things :

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

            }
        }

i verfity the email and password is correct, in c# code, which i put, but in the end i always get message like this : e.Result = "Incorrect username or password"

回答1:

I downloaded your app sample and tested it. When i examin the request the app is sending using Fiddler, it seams that the app is sending the exactly the info you provided. So your POST method is doing it job.

I tested even to resend the request from inside Fiddler and get the same result, ie. Correct POST info but wrong e.Result (Incorrect username or password).

All this makes me feel that the problem is not in your app or WebClient other than server side. Please look at it.

By the way why using WebClient? you can user HttpWebRequest or HttpClient.



回答2:

you can try and use like this

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

i think this might correct you problem.



回答3:

Have you tried swapping your operator by changing this:

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

To this:

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

I know i've heard of AND on some servers not being compatible... even though they should work either way



回答4:

Oh, snap! It took me quite a while to realize why it's not working.

First, you cannot perform this task using WebClient. The reason is your authentication is built with HTTP redirects. The first script (verifylogin.php) returns successful auth cookie and redirects WebClient to another script (loginstatus.php) which checks the cookie and shows the message. But WebClient simply doesn't pass cookie from first script to another, so the check fails (and makes you think you've done something wrong).

The solution is to use WebRequest instead:

    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