UWP Httpclient postasync in background job with json string

I just would like to post some json for api from the background task in my UWP application and return the answer to read it. My background task constantly fails with

Platform :: DisconnectedException ^ in memory location 0x077FEE74.

I tried many ways to make it work with things from the Internet, but only slightly adjusting the failure. Without code, I can do the background job perfectly.

Here is the code:

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();
        }

The json I'm trying to emulate looks something like this:

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

Any help is appreciated!

+4
2

windows.web.http.httpclient UWP, . .

StringContent. Windows.Web.HttpClient HttpStringContent, StringContent 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();
}

. APP , . , , .

.

+1

, , HttpClient ? System.Net.Http Windows.Web.Http? ,

+1

Source: https://habr.com/ru/post/1653223/


All Articles