Obtener contenido / mensaje de HttpResponseMessage


Estoy tratando de obtener contenido de HttpResponseMessage. Debería ser: {"message":"Action '' does not exist!","success":false}, pero no lo sé, cómo sacarlo de HttpResponseMessage.

HttpClient httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.GetAsync("http://****?action=");
txtBlock.Text = Convert.ToString(response); //wrong!

En este caso txtBlock tendría valor:

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Vary: Accept-Encoding
  Keep-Alive: timeout=15, max=100
  Connection: Keep-Alive
  Date: Wed, 10 Apr 2013 20:46:37 GMT
  Server: Apache/2.2.16
  Server: (Debian)
  X-Powered-By: PHP/5.3.3-7+squeeze14
  Content-Length: 55
  Content-Type: text/html
}
Author: Clem, 2013-04-11

5 answers

Necesitas llamar a GetResponse().

Stream receiveStream = response.GetResponseStream ();
StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);
txtBlock.Text = readStream.ReadToEnd();
 46
Author: Icemanind,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-04-10 20:58:22

Creo que el enfoque más fácil es simplemente cambiar la última línea a

txtBlock.Text = await response.Content.ReadAsStringAsync(); //right!

De esta manera no necesita introducir ningún lector de flujo y no necesita ningún método de extensión.

 231
Author: rudivonstaden,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-07 10:47:17

Pruebe esto, puede crear un método de extensión como este:

    public static string ContentToString(this HttpContent httpContent)
    {
        var readAsStringAsync = httpContent.ReadAsStringAsync();
        return readAsStringAsync.Result;
    }

Y luego, simplemente llame al método de extensión:

txtBlock.Text = response.Content.ContentToString();

Espero que esto te ayude; -)

 33
Author: MCurbelo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-01-13 15:54:58

Si desea convertirlo a un tipo específico (por ejemplo, dentro de las pruebas) puede usar ReadAsAsync método de extensión:

object yourTypeInstance = await response.Content.ReadAsAsync(typeof(YourType));

O siguiente para el código síncrono:

object yourTypeInstance = response.Content.ReadAsAsync(typeof(YourType)).Result;

Actualización: también hay una opción genérica de ReadAsAsync que devuelve una instancia de tipo específica en lugar de una declarada por objeto:

YourType yourTypeInstance = await response.Content.ReadAsAsync<YourType>();
 6
Author: Taras Mytofir,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-26 12:47:01

Puedes usar el método GetStringAsync:

var uri = new Uri("http://yoururlhere");
var response = await client.GetStringAsync(uri);
 0
Author: Hinrich,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-05 08:04:53