dqhbuwrwq692118284 2013-01-31 18:30
浏览 40
已采纳

HttpWebRequest,post方法

I have a small problem with HttpWebRequest in my wp 7.1 application. The application sends some game stats data to the php server by POST -method, but server doesn't fetch it. The $_POST array stays empty. With the aid of the WireShark I can see the data is in the packet sent to the server, but server doesn't receive any.

Here is the code of the method by which the data is sent:

    private void SendStatisticsToServer()
    {
        if (this.stat.Name == String.Empty || Object.ReferenceEquals(this.stat.Name, null))
        {
            this.OnEmptyNameEvent();
        }
        else
        {
            HttpWebRequest request = WebRequest.Create(ADDRESS) as HttpWebRequest;
            request.Method = "POST";
            request.ContentType = "text/plain";
            request.AllowReadStreamBuffering = false;

            request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallBack), request);
        }
    }

    private void GetRequestStreamCallBack(IAsyncResult requestRes)
    {
        StringBuilder builder = new StringBuilder("user=");
        builder.Append(this.stat.Name);
        builder.Append("&won=");
        builder.Append(this.stat.Won == true ? "1" : "0");
        builder.Append("&scores=");
        builder.Append(this.stat.Scores.ToString());

        Byte[] byteArray = Encoding.UTF8.GetBytes(builder.ToString());

        HttpWebRequest request = requestRes.AsyncState as HttpWebRequest;
        Stream stream = request.EndGetRequestStream(requestRes);
        stream.Write(byteArray, 0, byteArray.Length);
        stream.Flush();
        stream.Close();

        request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
    }

    private void GetResponseCallback(IAsyncResult requestRes)
    {
        HttpWebRequest request = requestRes.AsyncState as HttpWebRequest;

        HttpWebResponse response = request.EndGetResponse(requestRes) as HttpWebResponse;
        Stream stream = response.GetResponseStream();
        StreamReader streamRead = new StreamReader(stream);
        String responseString = streamRead.ReadToEnd();
        stream.Close();
        streamRead.Close();

        response.Close();
        this.OnExitButtonPressedEvent(ButtonType.Exit);
    }

(I'm sorry for the code with no comments, but I think it's self-explanatory )

As you can see, it's almost copy-past from the msdn.

And here is the php -server code:

    <?php
     header('Content-type: text/plain'); 
     // HTTP/1.1
     header("Cache-Control: no-store, no-cache, must-revalidate");
     // HTTP/1.0
     header("Pragma: no-cache");
     $logfile = 'postdata.txt';
     $firstData = $_POST["user"];
     $secondData = $_POST["won"];
     $thirdData = $_POST["scores"];
     $file = fopen($logfile, "a+");

     fwrite($file, "Post received - data: firstData=". $firstData . "   
     secondData=" . $secondData . " thirdData=" . $thirdData . "
");
     fclose($file);

     ?>

As you can see, this server doesn't do much - only receiving the data and pushing it into the log -file.

it is definitely not a server's bug, since the old application written in Qt works fine under the same conditions and the $_POST array is not empty.

Could you, please, tell me, where could be a possible error?

The target platform is WP 7.1.

Thank you beforehand for your answers.

  • 写回答

1条回答 默认 最新

  • dongya8378 2013-01-31 22:16
    关注

    I'm going to have to do this as an answer, but I'm not entirely sure this is correct. I believe for a text/plain post, the format of the parameters is:

    user=d
    won=1
    scores=3
    

    If the you do application/x-www-form-urlencoded, then you would go with

    user=d&won=1&scores=3
    

    There's also a possibility PHP doesn't support text/plain.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿