Result from response.Content.ReadAsStringAsync() is empty string (Windows.Web.Http). How do I get response?

1.7k views Asked by At

When I run this from a browser manually, it returns a response, that looks like this:

{"value":[{"ID":4}]}

Now, I am POSTing it from a Windows IoT Device, using the following code:

private async Task SendPostRequest(string username, string password, Dictionary<string, string> parameters)
        {
            try
            {
                // using Windows.Web.Http
                HttpFormUrlEncodedContent formattedData = new HttpFormUrlEncodedContent(parameters);
                using (HttpBaseProtocolFilter clientHandler = new HttpBaseProtocolFilter())
                {
                    clientHandler.ServerCredential = GetCredentials(username, password);

                    using (HttpClient httpClient = new HttpClient(clientHandler))
                    {
                        //txtCommand.Text = "PostAsync";
                        HttpResponseMessage response = await httpClient.PostAsync(postUrl, formattedData); 
                        txtResponse.Text = response.ToString();
                        response.EnsureSuccessStatusCode();

                        string responseString = await response.Content.ReadAsStringAsync();
                        txtResponse.Text += " responseString: " + responseString;
                    }
                }

The result in responseString is an empty string.

The result of response.ToString() looks like this:

StatusCode: 200, ReasonPhrase: 'OK', Version: 2, Content: Windows.Web.Http.HttpStreamContent, Headers:
{
 Persistent-Auth: true
 Server: Microsoft-IIS/10.0
 Transfer-Encoding: chunked
 Date: Fri, 27 Aug 2021 15:16:38 GMT
 X-Powered-By: ASP.NET
 dbgate-version: 1.0
}{
 Content-Type: text/plain
}
0

There are 0 answers