wp8 HttpWebRequest POST不发送帖子值 [英] wp8 HttpWebRequest POST not sending post values along

查看:90
本文介绍了wp8 HttpWebRequest POST不发送帖子值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在发送帖子值时,我有点卡在HttpWebRequest上,我不知道问题出在哪里.

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.

这是我的代码

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请求中接收任何值. 但是我确实从服务器得到响应.

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

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

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

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

我唯一得到的是-

提前感谢您的帮助:)

它没有任何变化地开始工作.

It just started working without anychanges.

目前已结案!

推荐答案

尝试一下.可能会对您有帮助.

Try this. it may help you.

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

这篇关于wp8 HttpWebRequest POST不发送帖子值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆