2

so I have a php script(its name is wiadomosci.php) with following code:

<?php
if (isset($_GET['wszystkie'])) //when I'm sending GET with wszystkie? paramets it send back all records in JSON -  it works without any problems
{
$con=mysqli_connect("xxxl","xxxx","xxxx","xxxl");



if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
  mysqli_query("SET CHARSET utf8", $con);
  mysqli_query("SET NAMES 'utf8' COLLATE 'utf8_bin'", $con);
  $tablica_wynikow = array();
   $pobrane_dane = mysqli_query($con, "SELECT * FROM wiadomosci");
    while($nt=mysqli_fetch_assoc($pobrane_dane)){ 
    $tablica_wynikow[] = $nt;

    }
header('Content-Type: application/json');
echo json_encode($tablica_wynikow);
}
mysqli_close($con);
}
if (isset($_POST["nowa_wiadomosc"])) //hre is post part
{
    $tresc_wiadomosci = $_POST["nowa_wiadomosc"];
    $con=mysqli_connect("xxx","xxx","xxx","xxxx");



   if (mysqli_connect_errno()) {
      echo "Failed to connect to MySQL: ";
   }
   else
   {
     mysqli_query("SET CHARSET utf8", $con);
     mysqli_query("SET NAMES 'utf8' COLLATE 'utf8_bin'", $con);
     mysqli_query("INSERT INTO wiadomosci (tresc_wiadomosci) values  ($tresc_wiadomosci)");
   }

   mysqli_close($con);  
}
?>

And this is how I try to send POST from c# app:

System.Net.WebClient client = new System.Net.WebClient();
string result = client.UploadString("xxxxxxxx/wiadomosci.php?", "nowa_wiadomosc=TESTTTTT");
Console.WriteLine(result);

But as result I get some random webiste's html code and new record isn't added. If someone could point me in the right direction.

EDIT: ITS FIXED. I started debbuging it more and more and it was wrong query construction in php script, now everything works.

Thank You guys anyway.

darekg11
  • 175
  • 1
  • 3
  • 13
  • Have you already checked this http://stackoverflow.com/questions/5401501/how-to-post-data-to-specific-url-using-webclient-in-c-sharp ? – Sergey Malyutin Oct 01 '14 at 13:58
  • you didn't tell WebClient you're doing a post, so it's probably default to a GET. You can trivially check this in php with `echo $_SERVER['REQUEST_METHOD']` and `var_dump($_GET)`. If PHP is invoked via a GET request, then $_POST will **NOT** be populated. – Marc B Oct 01 '14 at 13:59
  • According to the post @SergeyMalyutin referenced, try removing the ?. Also, your xxxxxxxx should be like `http://yourwebsite.com` – M Akin Oct 01 '14 at 14:05
  • Yes my xxx is proper http link and I also removed ? and it is still the same. Also UploadString uses POST by default. – darekg11 Oct 01 '14 at 14:07
  • @darekg11, please try the following solution, let me know, if any issue – Arindam Nayak Oct 01 '14 at 14:07
  • I have also tried link posted by Sergey Malyutin but it also doesn't work that is why I have concerns about php script. – darekg11 Oct 01 '14 at 14:15

1 Answers1

2

You can try following to send POST request.

using (var wb = new WebClient())
{
    var data = new NameValueCollection();
    data["nowa_wiadomosc"] = "TESTTTTT";


    var response = wb.UploadValues("xxxxxxxx/wiadomosci.php?", "POST", data);
}

Checkout for help -- LINK

Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48
  • Tried, it doesn't change anything. I think there may be problem with my php script actually not c# side but I may be wrong. – darekg11 Oct 01 '14 at 14:12