简体   繁体   中英

How to post json to a website using WinRT?

I am trying to post this JSON data string to my php site:

{
    "tag":"login"
    "email":"email@email.com"
    "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

First, fix your UserCredentials class. Make your memebers public, not internal.

public class UserCredentials
{
    public UserCredentials(string user, string pass)
    {
        User = user;
        Pass = pass;
        Tag = "login";
    }

    public string User;
    public string Pass;
    public string Tag;
}

Second, serialize only your credentials.

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": "me@hotmail.com",
  "Pass": "ILoveToEatGrapes",
  "Tag": "login"
}

Third, replace 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

Fourth, use your data in PHP

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']);

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);

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM