HttpWebRequest,post方法

时间:2013-01-31 18:30:37

标签: php post httpwebrequest windows-phone-7.1

我的wp 7.1应用程序中的HttpWebRequest存在一个小问题。应用程序通过POST -method将一些游戏统计数据发送到php服务器,但是服务器不会获取它。 $ _POST数组保持为空。借助WireShark,我可以看到数据在发送到服务器的数据包中,但服务器没有收到任何数据。

以下是发送数据的方法代码:

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

(我很抱歉没有评论的代码,但我认为这是不言自明的)

正如你所看到的,它几乎是从msdn复制过去的。

这是php -server代码:

    <?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 . "\r\n");
     fclose($file);

     ?>

正如您所看到的,此服务器没有做太多工作 - 只接收数据并将其推送到日志文件中。

它绝对不是服务器的错误,因为在Qt中编写的旧应用程序在相同条件下工作正常并且$ _POST数组不为空。

请你告诉我,哪里可能出现错误?

目标平台是WP 7.1。

事先感谢您的回答。

1 个答案:

答案 0 :(得分:2)

我将不得不这样做作为答案,但我不完全确定这是正确的。我相信文本/普通帖子,参数的格式是:

user=d
won=1
scores=3

如果您执行application / x-www-form-urlencoded,那么您可以使用

user=d&won=1&scores=3

PHP也不支持text / plain。