【问题标题】:POST request in UWP not sendingUWP中的POST请求未发送
【发布时间】:2017-03-24 05:49:09
【问题描述】:

我为 UWP 编写应用程序

我像这样从后端接收 json:

string url = "http://api.simplegames.com.ua/index.php/?wc_orders=all_orders";
{
    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;
}

我尝试像这样发送 POST 请求

OrdersList = new List<RootObject>(rootObjectData);
using (HttpClient httpClient = new HttpClient())
{
    httpClient.BaseAddress = new Uri(@"http://api.simplegames.com.ua");
    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("utf-8"));

    string endpoint = @"/post_from_local.php";

    try
    {
        HttpContent content = new StringContent(JsonConvert.SerializeObject(OrdersList), Encoding.UTF8, "application/json");
        HttpResponseMessage response = await httpClient.PostAsync(endpoint, content);

        if (response.IsSuccessStatusCode)
        {
            string jsonResponse = await response.Content.ReadAsStringAsync();
            Debug.WriteLine(jsonResponse);
            //do something with json response here
        }
    }
    catch (Exception)
    {
        //Could not connect to server
        //Use more specific exception handling, this is just an example
    }
}

但是,后端开发人员说他看到空行,但没有收到数据。

感谢您的帮助。我的错误在哪里?

【问题讨论】:

    标签: c# .net json visual-studio uwp


    【解决方案1】:

    改变这一行:

    var stream = await httpClient.GetStreamAsync(url);
    

    到:

    var stream = httpClient.GetStreamAsync(url).Result;
    

    【讨论】:

    • 无效。我想我明白我的问题在哪里。我的异步等待问题。在写入 json 变量之前,我尝试通过 post 请求发送 json
    • 没错,这就是为什么我认为这可以解决问题。
    • 好的。我按照你说的重写。但它似乎无法解决问题
    • 等等,我会尝试一件事,然后给你写信
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-22
    • 2021-06-08
    • 2019-05-16
    • 2021-07-27
    • 2018-05-26
    • 1970-01-01
    相关资源
    最近更新 更多