具有 json 字符串的后台任务中的 UWP Httpclient postasync
UWP Httpclient postasync in background task with json string
我只想从我的 UWP 应用中的后台任务 post 一些 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
的那个?尝试切换到另一个,它可能会有所帮助
最好使用windows.web.http.httpclient for UWP apps since it supports wide range of Languages. See table from the reference
现在是 StringContent。 Windows.Web.HttpClient 作为 HttpStringContent,类似于 System.Net.Http.HttpClient
中的 StringContent
这是一个示例片段,但请务必阅读参考资料。
Uri theUri = new Uri("https://m.xxxx.com/api/v4/session?expand=account,profile)");
HttpStringContent content = new HttpStringContent("{ \"keep_login\": true, \"id\": null, \"username\": \"zumbauser\", \"password\": \"zumbapw\" }", Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");
var client = new HttpClient();
HttpResponseMessage response = await client.PostAsync(theUri, content);
所以要完成您的方法,它将是
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)");
HttpStringContent content = new HttpStringContent("{ \"keep_login\": true, \"id\": null, \"username\": \"zumbauser\", \"password\": \"zumbapw\" }", Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");
HttpResponseMessage aResponse = await aClient.PostAsync(theUri, content);
if (aResponse.IsSuccessStatusCode)
{
Debug.WriteLine("This is outpuuuuuuuuuuuuuut: " + aResponse.ToString());
}
else
{
String failureMsg = "HTTP Status: " + aResponse.StatusCode.ToString() + " – Reason: " + aResponse.ReasonPhrase;
}
_deferral.Complete();
}
注意:我在后台任务中对我的一个应用程序进行了同样的尝试,它工作正常。唯一失败的情况是当我尝试发送大量数据时出现可用内存不足错误。
祝你好运,编码愉快。
我只想从我的 UWP 应用中的后台任务 post 一些 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
的那个?尝试切换到另一个,它可能会有所帮助
最好使用windows.web.http.httpclient for UWP apps since it supports wide range of Languages. See table from the reference
现在是 StringContent。 Windows.Web.HttpClient 作为 HttpStringContent,类似于 System.Net.Http.HttpClient
这是一个示例片段,但请务必阅读参考资料。
Uri theUri = new Uri("https://m.xxxx.com/api/v4/session?expand=account,profile)");
HttpStringContent content = new HttpStringContent("{ \"keep_login\": true, \"id\": null, \"username\": \"zumbauser\", \"password\": \"zumbapw\" }", Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");
var client = new HttpClient();
HttpResponseMessage response = await client.PostAsync(theUri, content);
所以要完成您的方法,它将是
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)");
HttpStringContent content = new HttpStringContent("{ \"keep_login\": true, \"id\": null, \"username\": \"zumbauser\", \"password\": \"zumbapw\" }", Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");
HttpResponseMessage aResponse = await aClient.PostAsync(theUri, content);
if (aResponse.IsSuccessStatusCode)
{
Debug.WriteLine("This is outpuuuuuuuuuuuuuut: " + aResponse.ToString());
}
else
{
String failureMsg = "HTTP Status: " + aResponse.StatusCode.ToString() + " – Reason: " + aResponse.ReasonPhrase;
}
_deferral.Complete();
}
注意:我在后台任务中对我的一个应用程序进行了同样的尝试,它工作正常。唯一失败的情况是当我尝试发送大量数据时出现可用内存不足错误。
祝你好运,编码愉快。