Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
664 views
in Technique[技术] by (71.8m points)

c# - wp8 HttpWebRequest POST not sending post values along

Im kind of stuck with a HttpWebRequest while sending post values and i dont know what the problem is.

Hopefully anyone is able to help me with this.

this is my code

private async void loggingIn(string url, string postdata)
    {
        if (NetworkInterface.GetIsNetworkAvailable())
        {
            try
            {
                var request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
                request.Method = "POST";

                byte[] data = Encoding.UTF8.GetBytes(postdata);
                request.ContentLength = data.Length;
                using (var requestStream = await Task<Stream>.Factory.FromAsync(request.BeginGetRequestStream, request.EndGetRequestStream, request))
                {
                    await requestStream.WriteAsync(data, 0, data.Length);
                }


                WebResponse responseObject = await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, request);
                var responseStream = responseObject.GetResponseStream();
                var sr = new StreamReader(responseStream);
                string received = await sr.ReadToEndAsync();
                MessageBox.Show(received);
            }
            catch
            {

            }
        }
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        string url = "localhost/test.php";
        string password = passwordBoxLogin.Password;
        string username = usernameBoxLogin.Text;

        string postdata = "password=" + password +"&username="+username;

        loggingIn(url,postdata);
    }

The problem is that my server does not receive any values from the POST request. but i do get a response from the server.

this is what i use to check the code on my php server

<?php
   echo $_POST['username'];
   echo"-";
   echo $_POST['password'];
?>

the only thing i get returned is the -

Thanks in advance for helping me out :)

It just started working without anychanges.

For now case closed!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I modified You Code Like this Hope it's definitely Work for You

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        string url = "localhost/test.php";
        string password = passwordBoxLogin.Password;
        string username = usernameBoxLogin.Text;
        string postdata = "password=" + password +"&username="+username;

        bool isNetwork = NetworkInterface.GetIsNetworkAvailable();
        if (!isNetwork)
        {
        }
        else
        {
         try
          {

            WebClient webClient = new WebClient();
            webClient.Headers["Content-Type"] = "application/json"; // Your Application Header Content-Type 
            webClient.Encoding = Encoding.UTF8;
            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(proxy_UploadStringCompleted);
            webClient.UploadStringAsync(new Uri(url ), "POST", postdata ,null);
          }
          catch
          {
          }
        }

    }

    private void proxy_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
    {
        var response = e.Result;

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...