UWP Httpclient postasync在后台任务中使用json字符串

Sun*_*nic 5 json httpclient background-task uwp windows-10-universal

我只想在我的UWP应用程序中将一些json从后台任务发布到api,然后取回响应来读取它.我的后台任务经常失败

Platform :: DisconnectedException ^在内存位置0x077FEE74.

我尝试了很多方法来使用互联网上的东西,但只是稍微调整失败.没有代码,我可以完美地执行后台任务.

这里的代码:

public async void Run(IBackgroundTaskInstance taskInstance)
        {
            _deferral = taskInstance.GetDeferral();

HttpClient aClient = new HttpClient();

            aClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            aClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/javascript"));



        Uri theUri = new Uri("https://m.xxxx.com/api/v4/session?expand=account,profile)");

StringContent theContent = new StringContent("{ \"keep_login\": true, \"id\": null, \"username\": \"zumbauser\", \"password\": \"zumbapw\" }", Encoding.UTF8, "application/json");


        HttpResponseMessage aResponse = await aClient.PostAsync(theUri, theContent);

        if (aResponse.IsSuccessStatusCode)
        {
            Debug.WriteLine("This is outpuuuuuuuuuuuuuut: " + aResponse.ToString());
        }
        else
        {
            // show the response status code 
            String failureMsg = "HTTP Status: " + aResponse.StatusCode.ToString() + " – Reason: " + aResponse.ReasonPhrase;
        }

_deferral.Complete();
        }
Run Code Online (Sandbox Code Playgroud)

我试图模仿的Json看起来像这样:

{ "keep_login":空, "ID":空, "用户名": "zumbauser", "密码": "zumbapw"}

任何帮助表示赞赏!

AVK*_*AVK 1

对于 UWP 应用程序,最好使用windows.web.http.httpclient,因为它支持多种语言。请参阅参考资料中的表格

\n\n

现在介绍 StringContent。Windows.Web.HttpClient 作为 HttpStringContent 类似于 StringContentSystem.Net.Http.HttpClient

\n\n

例如,这是一个片段,但请确保您阅读了参考资料。

\n\n
Uri theUri = new Uri("https://m.xxxx.com/api/v4/session?expand=account,profile)");\nHttpStringContent content = new HttpStringContent("{ \\"keep_login\\": true, \\"id\\": null, \\"username\\": \\"zumbauser\\", \\"password\\": \\"zumbapw\\" }", Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");\nvar client = new HttpClient();\nHttpResponseMessage response = await client.PostAsync(theUri, content);\n
Run Code Online (Sandbox Code Playgroud)\n\n

所以要完成你的方法,它将是

\n\n
public async void Run(IBackgroundTaskInstance taskInstance)\n{\n    _deferral = taskInstance.GetDeferral();\n    HttpClient aClient = new HttpClient();\n    aClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));\n    aClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/javascript"));\n    Uri theUri = new Uri("https://m.xxxx.com/api/v4/session?expand=account,profile)");\n    HttpStringContent content = new HttpStringContent("{ \\"keep_login\\": true, \\"id\\": null, \\"username\\": \\"zumbauser\\", \\"password\\": \\"zumbapw\\" }", Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");\n    HttpResponseMessage aResponse = await aClient.PostAsync(theUri, content);\n    if (aResponse.IsSuccessStatusCode)\n    {\n        Debug.WriteLine("This is outpuuuuuuuuuuuuuut: " + aResponse.ToString());\n    }\n    else\n    {\n        String failureMsg = "HTTP Status: " + aResponse.StatusCode.ToString() + " \xe2\x80\x93 Reason: " + aResponse.ReasonPhrase;\n    }\n    _deferral.Complete();\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

笔记:我在后台任务中对我的一个应用程序进行了同样的尝试,效果很好。唯一失败的情况是当我尝试发送大量数据时,出现“内存不足”错误。

\n\n

祝你好运,编码愉快。

\n