22530

Sending POST request with data to PHP script from c# level

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.

Answer1:

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

Recommend

  • Fatal error: Uncaught Error: Call to undefined function mysql_result() in [duplicate]
  • How to convert html table to pdf using pdfmake
  • PHP retrieve records from database
  • Fatal error: Call to a member function query() on a non-object in [duplicate]
  • MySQL, PHP, JavaScript UTF-8 Problem with swedish letters (Everything Tested - Nothing Works)
  • Warning: mysqli_close() expects parameter 1 to be mysqli
  • PHP/MySQL table problem?
  • remove quotes from json_encode keys
  • Unable to insert form data in MySQL database
  • INSERT multiple entries from Android -> PHP -> MYSQL
  • Wamp and xampp on same machine
  • echo json array in a phpMyAdmin Column VIA PHP
  • Saving contact form 7 data into custom db and not wordpress db
  • Generate a table of orders grouped by user_id which sums quantity of product
  • Android SQLite queries marked as Error by Android Studio
  • App for Mac App Store doesn't run when signed
  • Error when submitting checkbox to MySQL
  • Number of variables doesn't match number of parameters - Yes they do
  • SQL Server - Is there a collation that provides natural order for numbers?
  • How to find data from last week in MySQL
  • SQL Server Like Query not case sensitive
  • C# Networkstream reads nothing
  • Xamarin PCLCrypto SHA256 give different hash
  • How to extract text from a PDF and decode characters?
  • MySQL: Update rows in table by iterating and joining with another one
  • Javascript CORS - No 'Access-Control-Allow-Origin' header is present
  • How do I remove all but some records based on a threshold?
  • How can I include If-None-Match header in HttpRequestMessage
  • nonblocking BIO_do_connect blocked when there is no internet connected
  • Redux Form - Not able to type anything in input
  • Get history of file changes from TFS to implement custom “blame”-behaviour of exceptions
  • Installed module is empty
  • Moving mysql files across servers
  • Jquery UI tool tip close icon
  • Encrypt data by using a public key in c# and decrypt data by using a private key in php
  • How to check if every primary key value is being referenced as foreign key in another table
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Comma separated Values
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • Unable to use reactive element in my shiny app