Reputation: 1060
I am trying to post this JSON data string to my php site:
{
"tag":"login"
"email":"[email protected]"
"password":"P@ssw0rd"
}
this is my C# code that I have:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost/login/");
string link = "http://localhost/login/";
UserCredentials cred = new UserCredentials(email.Text, pass.Password.ToString());
var data = new Dictionary<string, List<UserCredentials>>();
string json = JsonConvert.SerializeObject(data, Formatting.Indented);
HttpResponseMessage re = await client.PostAsync(link, new StringContent(json));
User credentials class:
public class UserCredentials
{
public UserCredentials(string user, string pass)
{
User = user;
Pass = pass;
Tag = "login";
}
internal string User;
internal string Pass;
internal string Tag;
}
in my php script :
<?php
if (isset($_POST['tag']) && $_POST['tag'] != '') {
echo "got tags";
else
echo "didn't get anything";
?>
does anyone know how I am supposed to send the tags over the postasync method? I am getting 'didn't get anything' .. please help
Upvotes: 2
Views: 2262
Reputation: 16450
public class UserCredentials
{
public UserCredentials(string user, string pass)
{
User = user;
Pass = pass;
Tag = "login";
}
public string User;
public string Pass;
public string Tag;
}
UserCredentials cred = new UserCredentials(email.Text, pass.Password.ToString());
string json = JsonConvert.SerializeObject(cred, Formatting.Indented);
System.Diagnostics.Debug.WriteLine(json);
You will see in the Output window something like this:
{
"User": "[email protected]",
"Pass": "ILoveToEatGrapes",
"Tag": "login"
}
StringContent
with FormUrlEncodedContent
If you are using PHP $_POST
, then you must send something like key1=value1&key2=value2
in your HTTP request. This is called x-www-form-urlencoded
and HttpClient contains the right class for this: FormUrlEncodedContent
.
FormUrlEncodedContent
receives a dictionary of key/value pairs.
// Create the list of keys and values.
var data = new Dictionary<string, string>();
data["tag"] = json;
// Send my keys and values.
HttpClient client = new HttpClient();
string link = "http://localhost/login/";
HttpResponseMessage re = await client.PostAsync(link, new FormUrlEncodedContent(data));
This will be sent through the connection:
POST /login/ HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: localhost
Content-Length: 142
Expect: 100-continue
Connection: Keep-Alive
tag=%7B%0D%0A++%22User%22%3A+%22me%40hotmail.com%22%2C%0D%0A++%22Pass%22%3A+%22I
LoveToEatGrapes%22%2C%0D%0A++%22Tag%22%3A+%22login%22%0D%0A%7D
As you see above, data is encoded with a lot of percent signs. PHP automatically decodes the data for you, so you do not need to worry about that.
To convert the credentials from json to an array, you may use json_decode
$cred = json_decode($_POST['tag']);
Upvotes: 1
Reputation: 2848
cred created, but not added to data
will probably result in an empty set
UserCredentials cred = new UserCredentials(email.Text, pass.Password.ToString());
var data = new Dictionary<string, List<UserCredentials>>();
data["key"] = new List<UserCredentials>{cred};
string json = JsonConvert.SerializeObject(data, Formatting.Indented);
Upvotes: 0
Reputation: 3506
Any reason why you're using the dictionary (well, you actually create it but don't fill it) instead of the object rightaway?
UserCredentials cred = new UserCredentials(email.Text, pass.Password.ToString());
string json = JsonConvert.SerializeObject(cred, Formatting.Indented);
Upvotes: 0