wp8 HttpWebRequest POST不发送post值

时间:2014-01-07 00:09:04

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

我在发送帖子值时遇到了HttpWebRequest,我不知道问题是什么。

希望任何人都能帮助我。

这是我的代码

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

问题是我的服务器没有从POST请求中收到任何值。 但我确实收到了服务器的回复。

这是我用来检查我的php服务器上的代码

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

我唯一得到的是 -

提前感谢您帮助我:)

它刚刚开始工作而没有任何改变。

现在案件已关闭!

2 个答案:

答案 0 :(得分:0)

我像这样修改了你的代码 希望它绝对适合你

    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;

    }

答案 1 :(得分:0)

试试这个。它可能对你有所帮助。

private async void loggingIn(string url, string postdata)
        {
            if (NetworkInterface.GetIsNetworkAvailable())
            {
                try
                {
                 var request = HttpWebRequest.Create(url) as HttpWebRequest;  
                 request.Method = "POST";
                 request.Accept = "application/json";
                 request.ContentType = "application/json";
                 request.BeginGetRequestStream(loginRequest, request);
                 }
                 catch(Exception ex) 
                {
                }
       }
     private void loginRequest(IAsyncResult asyncResult)
      {
       string postdata = "password=" + password +"&username="+username;
       UTF8Encoding encoding = new UTF8Encoding();
       HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
       Stream _body = request.EndGetRequestStream(asyncResult);
       byte[] formBytes = encoding.GetBytes(postdata);
       _body.Write(formBytes, 0, formBytes.Length);
       _body.Close();
        request.BeginGetResponse(loginResponse, request);
      }

     private void loginResponse(IAsyncResult asyncResult)
      {
         try
          {
            HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
            HttpWebResponse httpResponse = (HttpWebResponse)response;
            using (Stream data = response.GetResponseStream())
            using (var reader = new StreamReader(data))
            {
            string received = await reader.ReadToEndAsync();
            MessageBox.Show(received);
            }
          }
          catch(Exception ex)
         {
         }
     }