Reputation: 1039
So I'm trying to set POST data of a remote PHP script. This script uses the POST data as filename and retrieves a JSON file with it. But this sadly does not work. It retrieves the data with no values. Here is how it works:
C#:
using (WebClient client = new WebClient())
{
byte[] saveData = client.UploadData(
"http://" + ConfigurationManager.AppSettings["scripturi"].ToString() + "storeData.php",
"POST",
System.Text.Encoding.ASCII.GetBytes("filename="+ dt.bedrijfsNaam));
}
PHP:
<?php
$host='myip';
$user='username';
$pass='userpass';
$db='mydatabase';
$link= mysqli_connect($host, $user, $pass, $db) or die(msqli_error($link));
$filename = $_POST['filename'] . '.json';
$json = file_get_contents(__DIR__."/json/".$filename);// my thoughts are that something is wrong in this line?
$obj = json_decode($json);
$query_opslaan = "INSERT INTO skMain (BedrijfsName, ContPers, TelNum, email, Land, Plaats, PostCode) VALUES ('". $obj->bedrijfsNaam ."' , '". $obj->ContPers ."', '". $obj->TelNum ."', '". $obj->email ."', '". $obj->Land ."', '". $obj->Plaats ."', '". $obj->PostCode ."')";
mysqli_query($link, $query_opslaan) or die(mysqli_error($query_opslaan));
?>
it should retrieve correct data from the JSON file but instead it retrieves no values it all and the query stores blank data into the database. I think I used the C# script wrong and that's why I also think that the $json variable is not working correctly. But I don't exactly know what I did wrong. Can someone please help me?
Upvotes: 0
Views: 1192
Reputation: 42453
When you lookup the doc for the PHP $_POST
you'll find:
An associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.
This means the content you POST to the server has to be one of those Content-Types and its body need to match the expected format.
In your code you use the UploadData
method. That method doesn't do any magic for you. It simply POSTs the bytes you give it. Your request will look like this on the wire:
POST /questions/ask HTTP/1.1 Host: stackoverflow.com Content-Length: 13 Expect: 100-continue Connection: Keep-Alive filename=test
You see that there is no Content-Type header.
There is however an other method called UploadValues
which takes an NameValueCollection
and transforms its content to the needed x-www-form-urlencoded format for you:
using(var wc= new WebClient())
{
var nv = new System.Collections.Specialized.NameValueCollection();
nv.Add("filename", "test");
nv.Add("user", "bar");
wc.UploadValues("http://stackoverflow.com/questions/ask", nv);
}
When executed the following is send to the server:
POST /questions/ask HTTP/1.1 Content-Type: application/x-www-form-urlencoded Host: stackoverflow.com Content-Length: 22 Expect: 100-continue filename=test&user=bar
This last body content will lead to a populated $_POST
array with filename and user.
When debugging these kind of requests make sure you run Fiddler so you can inspect the HTTP traffic.
Upvotes: 1