Share via


Twitter Dishing Out 417 - Expectation Failed to .Net Clients

My little Twitter app was broke since past few days with error 417 - Expectation Failed. Infect most .Net apps calling Twitter APIs would be broken right now so I thought to write this up.

This error is seemingly because Twitter servers have started rejecting Expect HTTP header with value "!00-Continue". I'm not sure if this was planned event or enough warnings were issued to developers but it would be guaranteed to drive you nuts.

The error is because of default behavior in HttpWebRequest object that adds an HTTP header called Expect with value "100-Continue" to almost every outgoing POST request. This header basically tells the server that it's going to send all the data in form in the next request instead of current request so that if server has redirects or auth then it doesn't have to resend it all over again. This is a good thing if your web form has lots of data or if you are on low latency network or most servers in the word have either redirects or auth when submitting forms but a bad thing for server performance because now it gets hit twice for each request. I think performance might be the reason Twitter has turned off support for such two partter POST requests which unfortunately happens to be the default for HttpWebRequest.

In any case, it turns out that HttpWebRequest does all these thing under the hood so to get rid of this error you will need to set a static flag in ServicePointManager class like this:

System.Net.ServicePointManager.Expect100Continue = false;

Above statement will cause elimination of HTTP Expect header from your calls to Twitter and it will be happy again.

I'm using Yedda's C# wrapper for Twitter APIs for QckTwit so above line goes in to start of ExecutePostCommand method.

PS: If you are new to Twitter try out free simple lightweight app QckTwit. It just sits in your system tray, asks you about what you are doing at reminder interval you set, updates the Twitter and gets out of your way!

Comments

  • Anonymous
    December 29, 2008
    Nope, but, boy, was it a coincidence! I use Scott Watermasysk's Graffiti Plug-in library to add Twitter notifications when I add new posts to this blog. Well, the weekend before last, I upgraded to Graffiti CMS 1.2 and blogged about it. The plug-in duly

  • Anonymous
    January 02, 2009
    Thanks for the post!  I am using Yedda's wrapper as well, and this fixed my problem.  

  • Anonymous
    January 02, 2009
    Thanks for the solution. You just made my day. I wrote this tool to complement wikipedia. It allows user to save articles for offline viewing as well as sharing it via status message in facebook and twitter. See http://apps.facebook.com/wikiarticlesaver.

  • Anonymous
    January 07, 2009
    For anyone using the CURL library, this will fix the problem... curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));

  • Anonymous
    January 08, 2009
    hah! just ran into this Tiwtter bug today. thanks for posting the fix.

  • Anonymous
    January 14, 2009
    Thanks. I've been writing my own twitter aps and this was exactly what I was experiencing. Nice work!!!

  • Anonymous
    January 16, 2009
    Thanks Shital, we use Yedda at Retaggr.com - was wondering why we weren't seeing too many tweets!

  • Anonymous
    February 22, 2009
    Thanks for this - sorted out my problem!

  • Anonymous
    March 12, 2009
    Thanks, this fixed it. I'm using Yedda too. Now to figure out what to do with this :)

  • Anonymous
    March 16, 2009
    Thanks A LOT!! It saved my newly born App!

  • Anonymous
    March 18, 2009
    I wonder why this doesn't work on mine.  I'm using VS2008 and 3.5 Framework.  I still get the 417 error.

  • Anonymous
    March 24, 2009
    I'm using Yedda too. Thanks, this fixed it for me too!

  • Anonymous
    March 29, 2009
    It doesn't work anymore. I couldn't get it fixed. I guess, something more is broken now. Even the tested application QckTwit (http://qcktwit.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=21094#ReleaseFiles) with the correction doesn't seem to work.

  • Anonymous
    May 14, 2009
    PingBack from http://hungrycoder.xenexbd.com/node/754

  • Anonymous
    May 31, 2009
    Is anyone else starting to get this error again even with the code: System.Net.ServicePointManager.Expect100Continue = false; The 'QckTwit' app is getting this as well. Thanks.

  • Anonymous
    June 06, 2009
    The comment has been removed

  • Anonymous
    June 26, 2009
    Yes this still happens even with Expect100Continue = false. Puzzling, as it used to work. The only solution is to only use GET requests, as no Expect header is sent at all (as only one connection, no post to resend). I use HTTPS for all my oAuth requests, even on twitter, this is why I prefer post, as only the form is encrypted, when using querystrings  (GET requests), no encryption takes place, even on HTTPS. So the transfer of oAuth token via GETS is insecure, but this is the only acceptable way to Twitter!

  • Anonymous
    July 29, 2009
    I was having intermittent issues with the 417 error using the Yedda .net wrapper even after adding the Expect100continue = false. Then I noticed some slight differences in some of the examples on the web with this issue. I modified the Yedda code to use  HttpWebRequest, not WebRequest - HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); AND I set the Expect100continue = false like this: request.ServicePoint.Expect100Continue = false; Finally, I also changed the WebResponse to: HttpWebResponse response = (HttpWebResponse)request.GetResponse() Since these changes, I haven't had the 417 error. Hopefully setting Expect100continue via the HttpWebRequest class has this sorted...

  • Anonymous
    August 05, 2009
    Whaites, Thanks for the tip about using HttpWebRequest. Everything works great on my post attempt now. I have been using Tweetsharp and the post was working but not proxy and basic auth which I need to do behind my (cisco based) proxy server. I can see both my proxy authorization and authorization headers before I read the response object as well. I will figure out how to put that into tweetsharp. What is your opinion about the relative merits of the twitter API: .net vs java, tweet# vs yedda. Thanks Joe

  • Anonymous
    August 31, 2009
    great post. it worked for me too. i appreciate you code thanks.

  • Anonymous
    August 31, 2009
    great post. it worked for me too. i appreciate you code thanks. Sandip

  • Anonymous
    November 27, 2009
    Awesome, this had been bugging me for a long time as the old fix never worked for me, thanks to whaites, your a champion

  • Anonymous
    December 14, 2009
    Thanks! fixed the problem I'm using Yedda's API as well

  • Anonymous
    March 04, 2010
    This solved my problem too. Thanx I am using Yedda's API. Thanx again.

  • Anonymous
    March 09, 2010
    Thanks much!!!! You save me!!!

  • Anonymous
    March 30, 2010
    Tonight even with the changes with HttpWebRequest it does not work. I get the same exception (417). Any clue?

  • Anonymous
    March 30, 2010
    Tonight even with the changes with HttpWebRequest it does not work. I get the same exception (417). Any clue?

  • Anonymous
    March 30, 2010
    my mistake. Yes it works fine. Sorry for the confusion

  • Anonymous
    February 27, 2012
    I just downloaded and adopted this code and I am getting a 401 error (Unauthorized).  I'm positive that my username and password are correct so can anyone give me an idea as to why I might be getting this error?

  • Anonymous
    April 06, 2012
    The same error (401)! Any suggestions?