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.

6
  • Have you already checked this stackoverflow.com/questions/5401501/… ? Commented Oct 1, 2014 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. Commented Oct 1, 2014 at 13:59
  • According to the post @SergeyMalyutin referenced, try removing the ?. Also, your xxxxxxxx should be like http://yourwebsite.com Commented Oct 1, 2014 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. Commented Oct 1, 2014 at 14:07
  • @darekg11, please try the following solution, let me know, if any issue Commented Oct 1, 2014 at 14:07

1 Answer 1

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

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

1 Comment

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.

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.