gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 21:08:53 25 4
gpt4 key购买 nike

我只想从我的 UWP 应用程序中的后台任务将一些 json 发布到 api 并获取响应以将其读出。我的后台任务不断失败

Platform::DisconnectedException ^ at memory location 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();
}

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

{"keep_login":null,"id":null,"username":"zumbauser","password":"zumbapw"}



任何帮助表示赞赏!

最佳答案

我知道这听起来可能很傻,但是这 HttpClient你正在用吗?来自 System.Net.Http或者来自 Windows.Web.Http 的那个?尝试切换到另一个,它可能会有所帮助

关于json - UWP Httpclient postasync 在后台任务中使用 json 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39259891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com