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.