0

I want to insert data from my Form to my server using HTTP Web POST. I have my code below I am unable to get the value of JObject json and send it into my php code.

var caf = entCafNo.Text;
string url = "http://192.168.120.9:7777/TBS/Host=" + Constants.hostname + "&Database=" + Constants.database + "&Request=SendCaf";
string contentType = "application/json";
JObject json = new JObject
{
  { "CAF", caf }
};

HttpClient client = new HttpClient();
var response = await client.PostAsync(url, new StringContent(json.ToString(), Encoding.UTF8, contentType));

PHP Code:

$request = $_GET["Request"];

if($request == "SendCaf"){
    $caf = $_POST["CAF"];

    $sql = "INSERT INTO tblCaf(CAFNo) 
            VALUES('$caf)";
    mysqli_query ($conn, $sql);
}
loot verge
  • 449
  • 1
  • 12
  • 31

1 Answers1

1

You put all of the data in your URL. I think that is a GET, not a POST.

I haven't used GetRequestStream, so I don't have an answer. If you're interested in another way doing a POST, then use PostAsync. The way I'm doing it in my current Xamarin project is something like this

using Newtonsoft.Json;
using System.Net.Http;
//more code here

string url = "http://localhost/helloword/";
string contentType = "application/json";
JObject json = new JObject
{
    { "key1", value1},
    { "key2", value2 }            
};
HttpClient client = new HttpClient();
var response = await client.PostAsync(url, new StringContent(json.ToString(), Encoding.UTF8, contentType));
// this is data received from server. May or may not need this.
var data = await response.Content.ReadAsStringAsync();

Reference: How do I pass an object to HttpClient.PostAsync and serialize as a JSON body?

Fuong
  • 320
  • 3
  • 12
  • Thank you, Do I need loop if I have multiple data needed to be sent to the server? or it will send all the data automatically? – loot verge Aug 18 '18 at 03:24
  • I haven't tried yet, but I think the answer is yes. One thing to think about, for the same amount of data, sending multiple times is slower sending at once. So try to make one POST if possible. – Fuong Aug 18 '18 at 03:33
  • I am sending multiple data. It is ok if its slow as long as it is sending data to my server. Another question what do I put inside "key1" and "value1"? Is it like this `{"customerID", customerID }`? – loot verge Aug 18 '18 at 03:36
  • correct! Then, at the server side, you use the key `customerID` to get the value. For example, make a POST {"customterID", "12345"}. At backend, do something like `String id = get("customerID")` would get the value _12345_ assigned to `id` variable. The example is just to give an idea, not any language specific code. – Fuong Aug 18 '18 at 03:54
  • Oh ok thank you can I message you if I have any more questions? Thank you so much – loot verge Aug 18 '18 at 04:10
  • You're welcome.:) You should post your questions on here. I'm new to Xamarin. – Fuong Aug 18 '18 at 04:21
  • I can't get the JSON Object from xamarin to my php code – loot verge Aug 18 '18 at 06:28
  • can you help me? https://stackoverflow.com/questions/52360256/how-to-receiver-response-from-server-in-xamarin-forms – loot verge Sep 17 '18 at 03:37