简体   繁体   English

如何使用WinRT将json发布到网站?

[英]How to post json to a website using WinRT?

I am trying to post this JSON data string to my php site: 我正在尝试将此JSON数据字符串发布到我的php网站:

{
    "tag":"login"
    "email":"email@email.com"
    "password":"P@ssw0rd"
}

this is my C# code that I have: 这是我的C#代码:

        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脚本中:

<?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? 有谁知道我应该如何通过postasync方法发送标签? I am getting 'didn't get anything' .. please help 我得到“什么都没得到” ..请帮助

First, fix your UserCredentials class. 首先,修复您的UserCredentials类。 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 第三,将StringContent替换为FormUrlEncodedContent

If you are using PHP $_POST , then you must send something like key1=value1&key2=value2 in your HTTP request. 如果使用的是PHP $_POST ,则必须在HTTP请求中发送类似key1=value1&key2=value2 This is called x-www-form-urlencoded and HttpClient contains the right class for this: FormUrlEncodedContent . 这称为x-www-form-urlencoded并且HttpClient包含正确的类: FormUrlEncodedContent

FormUrlEncodedContent receives a dictionary of key/value pairs. FormUrlEncodedContent接收键/值对的字典。

// 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 第四,在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. PHP将自动为您解码数据,因此您无需担心。

To convert the credentials from json to an array, you may use json_decode 要将凭据从json转换为数组,可以使用json_decode

$cred = json_decode($_POST['tag']);

cred created, but not added to data will probably result in an empty set 创建但未添加到data凭证可能会导致一个空集

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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