C# 未返回正确的post方法结果

C# 未返回正确的post方法结果,c#,windows-phone-7,windows-phone-8,windows-phone,C#,Windows Phone 7,Windows Phone 8,Windows Phone,我正在尝试使用php/MySQL在WindowsPhone8登录功能中创建一个应用程序 我有以下php脚本: 在我的windows phone c#click事件中,我写了以下内容: private void btnLogin_Click(System.Object sender, System.Windows.RoutedEventArgs e) { Uri uri = new Uri(url, UriKind.Absolute);

我正在尝试使用php/MySQL在WindowsPhone8登录功能中创建一个应用程序

我有以下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=“不正确的用户名或密码”

您可以尝试这样使用

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

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

您是否尝试通过更改以下内容来交换您的操作员:

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

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

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

我下载了你的应用程序示例并对其进行了测试。当我使用Fiddler检查应用程序发送的请求时,它表明应用程序发送的信息与您提供的信息完全相同。所以你的POST方法就是做它的工作

我甚至测试了从Fiddler内部重新发送请求,得到了相同的结果,即正确的帖子信息,但错误的e.结果(错误的用户名或密码)

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


顺便问一下,为什么要使用WebClient?您可以使用HttpWebRequest或HttpClient。

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

首先,您不能使用
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

为什么不试试client.credentials事件?我不知道有关client credentials事件的任何信息,为什么需要它?:)不,它不能解决我的问题,我附加了有问题的源代码,你能下载并运行它一次吗,不会花费更多的5分钟:)我不知道,请查看此页面:,然后键入电子邮件:test@test.com密码:password。