【问题标题】:POST Request UWPPOST 请求 UWP
【发布时间】:2017-03-22 05:16:32
【问题描述】:

我正在编写 UWP 应用程序。

我需要向服务器发送带有 json 的 POST 请求

这是我下载 JSON 并写入值的代码:

public async void AllOrders_down()
    {


        string url = "http://api.simplegames.com.ua/index.php/?wc_orders=all_orders";

        var json = await FetchAsync(url);


        List<RootObject> rootObjectData = JsonConvert.DeserializeObject<List<RootObject>>(json);

        OrdersList = new List<RootObject>(rootObjectData);


    }
    public async Task<string> FetchAsync(string url)
    {
        string jsonString;

        using (var httpClient = new System.Net.Http.HttpClient())
        {
            var stream = await httpClient.GetStreamAsync(url);
            StreamReader reader = new StreamReader(stream);
            jsonString = reader.ReadToEnd();
        }

        return jsonString;
    }

我需要如何将带有这个 json 的 POST 请求发送到服务器?

谢谢你的帮助。

【问题讨论】:

    标签: c# json visual-studio uwp


    【解决方案1】:

    这是我在 UWP 应用程序中使用的示例 Post 请求。

    using (HttpClient httpClient = new HttpClient())
    {
        httpClient.BaseAddress = new Uri(@"http://test.com/");
        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("utf-8"));
    
        string endpoint = @"/api/testendpoint";
    
        try
        {
            HttpContent content = new StringContent(JsonConvert.SerializeObject(yourPocoHere), Encoding.UTF8, "application/json");
            HttpResponseMessage response = await httpClient.PostAsync(endpoint, content);
    
            if (response.IsSuccessStatusCode)
            {
                string jsonResponse = await response.Content.ReadAsStringAsync();
                //do something with json response here
            }
        }
        catch (Exception)
        {
            //Could not connect to server
            //Use more specific exception handling, this is just an example
        }
    }
    

    【讨论】:

    • 我试试你的代码。后端开发人员说他看到空行,但没有收到数据。
    • 有趣。您收到了来自您尝试访问的端点的 200 响应?
    • 是的。我想我知道问题出在哪里。我下载 json 并将其写入var json async。当我设置断点时,我看到 json value = null。
    • 当我使用你的代码空写入content。我的行 - HttpContent content = new StringContent(JsonConvert.SerializeObject(json), Encoding.UTF8, "application/json");
    • 如果已经是Json,则不需要使用JsonConvert.SerializeObject。只需执行 new StringContent(json, Encoding.UTF8, "application/json")
    【解决方案2】:

    您应该使用httpClient.PostAsync()

    【讨论】:

    • 好的,但是我需要如何编写代码,从 var json = await FetchAsync(url); 获取 json 并通过 POST 请求发送?
    • 这样的? using (var client = new HttpClient()) { var content = new StringContent(json, Encoding.UTF8, "application/json"); var result = client.PostAsync(url, content).Result; }
    • 最好调用var result = await client.PostAsync(url, content); 获取async/await 方法。
    • 嗯。我写这样的代码,但是什么都没有贴出这是我的代码string url2 = "http://api.simplegames.com.ua/post_from_local.php"; using (var client = new HttpClient()) { var content = new StringContent(json, Encoding.UTF8, "application/json"); var result = await client.PostAsync(url2, content); }
    • result 填充了什么?它应该有某种结果代码。
    猜你喜欢
    • 2017-03-24
    • 2021-03-03
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    • 2014-07-24
    • 2018-08-03
    • 1970-01-01
    • 2016-05-20
    相关资源
    最近更新 更多