Error communication PHP and C #

1

I'm trying to send a C # data to PHP, but the example I find and all the solutions simply result in the same error.

 using System.Net;
 using System.Collections.Specialized;

        string valor = "1";

        string urlAddress = "http://localhost:80/Untitled-1.php";

        using (WebClient client = new WebClient())
        {

            NameValueCollection postData = new NameValueCollection()
   {
          { "valor", valor}

   };


            client.UploadValues(urlAddress, postData);

        }

PHP code to receive data:

<html>

<?php
      $valor = $_POST["valor"];  

echo $valor;
 ?>

Error in PHP:

  

Notice: Undefined index: value in C: \ xampp \ htdocs \ Untitled-1.php on line 4

It's a very simple code, but it's the basis of a larger program. Anyone have any idea what it can be?

    
asked by anonymous 30.08.2018 / 02:35

1 answer

1

I'm not sure, but I think it's because of missing the Content-Type of the request, like this:

using System.Net;
using System.Collections.Specialized;

string valor = "1";

string urlAddress = "http://localhost:80/Untitled-1.php";

using (WebClient client = new WebClient())
{
    client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";

    NameValueCollection postData = new NameValueCollection()
    {
         { "valor", valor }
    };

    client.UploadValues(urlAddress, postData);
}

This problem has nothing to do with Access-Control-Allow-Origin: * , WebClient is not a site trying to access another, it is a client trying to access a site, so it does not have cross-over . Applying header('Access-Control-Allow-Origin: *'); to PHP does not solve anything and even if it was a CORS-related issue you would not even be able to get the error generated in PHP.

    
31.08.2018 / 20:52