如何在 UWP c# 中发送 POST

How to send a POST in UWP c#

我需要发送一个 POST 方法,但它说 JSON 内容无效,有人知道我在 JSON 格式中做错了什么吗?,非常感谢。

Uri resourceAddress;
        if (!Helpers.TryGetUri(pcHost + pcPort + "/api/code/scan", out resourceAddress))
        {
            rootPage.NotifyUser("Invalid URI.", NotifyType.ErrorMessage);
            return;
        }
        try
        {
            terminalRef = "1";
            //code = "Uc0E17G4nW";
            IHttpContent jsonContent = new HttpJsonContent(JsonValue.Parse("{\"code\":\"" + code +
                                                                            ",\"ref\" : \""+ terminalRef +
                                                                            "\"}"));





            HttpResponseMessage response = await httpClient.PostAsync(resourceAddress, jsonContent).AsTask(cts.Token);
            Debug.WriteLine(".");
            //await Helpers.DisplayTextResultAsync(response, cts.Token);
            rootPage.NotifyUser("Completed", NotifyType.StatusMessage);


        }
        catch (Exception ex)
        {
            rootPage.NotifyUser("Error: " + ex.Message, NotifyType.ErrorMessage);
            String errorMessage = ex.Message.ToString();
        }

注意到您的字符串格式不正确。你能尝试解析下面的字符串吗,

"{\"code\" : \"" + code + "\", \"ref\" : \"" + terminalRef + "\"}"

第一种方法是 Jaliya Udagedara 的建议,您需要检查 json 字符串的 format 以确保它是正确的。

另一种简单的方法是使用 Newtownsoft.Json 转换 c# 对象。

例如:

string jsonstring = JsonConvert.SerializeObject(new {code="code", Ref = "terminalRef" });
IHttpContent jsonContent = new HttpStringContent(jsonstring, Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");
HttpResponseMessage response = await httpClient.PostAsync(resourceAddress, jsonContent).AsTask(cts.Token);

在多次失败后,我发现了如何让它工作(至少对我而言)希望它可以帮助人们在通用 Windows 平台(UWP)中使用 Windows 10 物联网我知道这有多困难可以找到适当的文档。

              Uri resourceAddress;

            if (!Helpers.TryGetUri(Host + Port + "/XXX/YYY/directory", out resourceAddress))
            {
                return;
            }

            IHttpContent jsonContentCoordinates = new HttpJsonContent(JsonValue.Parse("{\"zzz\": \"" + something
                                                                              + "\", \"xxx\": \"" + somethingXXX
                                                                              + "\",\"yyy\": \"" + somethingYYY
                                                                              + "\" }"));
            HttpResponseMessage httpResponseCoordinates = new HttpResponseMessage();
            string httpResponseBodyCoordinates = "";
            try
            {
                httpResponseCoordinates = await httpClient.PostAsync(resourceAddress, jsonContentCoordinates).AsTask(cts.Token);
                httpResponseBodyCoordinates = await httpResponseCoordinates.Content.ReadAsStringAsync();
                httpResponseCoordinates.EnsureSuccessStatusCode();
                FlagInternetNotConnected = false;

            }
            catch (Exception)
            {
             //Catch it if it fails.

            }