0

Code:

private async Task httpAsync()
{

    var data = new StringContent("1254", Encoding.UTF8, "application/x-www-form-urlencoded");

    var url = "https://mywebsite.com/insert.php";
    using var client = new HttpClient();

    var response = await client.PostAsync(url, data);

    string result = response.Content.ReadAsStringAsync().Result;
    textBox2.Text = result;
}

private async void button3_AsyncClick(object sender, EventArgs e)
{
    await httpAsync();
}

My insert.php script:

<?php

 $postdata = $_GET['data'];

 echo $postdata;
?>

The Error I get: Notice: Undefined index: data in /storage/ssd3/643/15098643/public_html/insert.php

I am still new to this can someone assist me with this error. I am trying to echo back the "1254" sent to the server, for testing.

Paul
  • 21
  • 5

1 Answers1

1

The error is about PHP not finding a parameter named 'data' in the request.

First of all, you are mixing up a POST request with a GET request. It looks like your C# script is posting '1254' to your PHP script, but in your PHP script you are looking for GET parameters in the URL. So either:

  • change your C# script to send a GET request or
  • change your PHP script to check the POST request by using $_POST[param name]

Second, your PHP script is looking for a parameter called 'data', but all your C# script is sending is a string with the value '1254'

Have a look at this post to see how you can send a POST request in C# using parameters.

Reinder Wit
  • 6,490
  • 1
  • 25
  • 36