I in a C# client, I have the following code:
Uri uri = new Uri(@"http://myserver/test.php");
HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
request.Method = WebRequestMethods.Http.Post;
//request.ContentType = "application/json";
string req = "er3=12";
Console.WriteLine("Req: " + req);
System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding();
byte[] byteData = encoder.GetBytes(req);
request.ContentLength = byteData.Length;
using (Stream postStream = request.GetRequestStream())
{
postStream.Write(byteData, 0, byteData.Length);
}
That’s calling a php test page (soon to be replaced by a rest service):
<?php
echo "post vars:<br>";
foreach ($_POST as $key => $value) {
echo "$key -> $value<br>";
}
echo "end post vars:<br>";
?>
My problem is that when I run the app, in the response I get "post vars:<br>end post vars:<br>", so the variable er3 is not received.
If I run a simple html form, the post variable IS correctly read.
What may be wrong or missing in the C# code ?
Thanks
You’ll need to set the content type of your data so that PHP knows how to parse it.
Like this:
application/x-www-form-urlencodedis the standard MIME type used by web browsers when posting form data.