[Twitter] Error: 417 Expectation Failed.

Realizando una prueba de concepto con un minicliente de Twitter que estoy desarrollando con WPF para una demo, me encontrado con el error del título a la hora de hacer un update de mi status.

Googleando, he podido encontrar el porque de dicho error:

La clase System.HttpWebRequest y WebClient añaden la cabecera HTTP "Expect: 100-Continue" a menos que se le especifique lo contrario y eso no le gusta a Twitter:

<html>
    <head>
        <title>417 Expectation Failed</title>
    </head>
    <body>
        <h1>Expectation Failed</h1>
        <p>The expectation given in the Expect request-header
        field could not be met by this server.</p>
        <p>The client sent<pre>
        Expect: 100-continue
        </pre>
        but we only allow the 100-continue expectation.</p>
    </body>
</html>

Con el siguiente código he conseguido Twittear desde mi aplicación:

using (WebClient client = new WebClient())
{
    client.Credentials = request.Credentials;
    ServicePoint sp = ServicePointManager.FindServicePoint(request.TwitterUri);
    sp.Expect100Continue = false;
    byte[] updateMessageBytes = System.Text.Encoding.UTF8.GetBytes("status=" + request.Message);
    byte[] responseMessageBytes = client.UploadData(request.TwitterUri, updateMessageBytes);
    response.Response = XDocument.Parse(System.Text.Encoding.UTF8.GetString(responseMessageBytes));
}

Donde:

request.TwitterUri contiene http://twitter.com/statuses/update.xml

Espero que os sirva ;)

Salu2

Published 7/5/2009 16:05 por Luis Ruiz Pavón
Archivado en: ,,,
Comparte este post:
http://geeks.ms/blogs/lruiz/archive/2009/05/07/twitter-error-417-expectation-failed.aspx