0

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?

5
  • what is value of ConfigurationManager.AppSettings["scripturi"].ToString() Commented Jun 9, 2016 at 12:30
  • check this stackoverflow.com/a/25005434/5001784 Commented Jun 9, 2016 at 12:40
  • But I already have something like that answer as you can see in my PHP code Commented Jun 9, 2016 at 12:44
  • show me true flag in $json = file_get_contents(__DIR__."/json/".$filename); Commented Jun 9, 2016 at 12:45
  • I don't use it as an array, I use it as an object instead. The result will be the same as an array Commented Jun 9, 2016 at 12:46

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.