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 (30)

  1. 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

  2. Thanks for the post!  I am using Yedda’s wrapper as well, and this fixed my problem.  

  3. Rajib Bahar says:

    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.

  4. For anyone using the CURL library, this will fix the problem…

    curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Expect:’));

  5. hah! just ran into this Tiwtter bug today. thanks for posting the fix.

  6. RockyH says:

    Thanks. I’ve been writing my own twitter aps and this was exactly what I was experiencing. Nice work!!!

  7. Nik says:

    Thanks Shital, we use Yedda at Retaggr.com – was wondering why we weren’t seeing too many tweets!

  8. Thanks for this – sorted out my problem!

  9. Thanks, this fixed it. I’m using Yedda too. Now to figure out what to do with this 🙂

  10. Murilo says:

    Thanks A LOT!! It saved my newly born App!

  11. tonex says:

    I wonder why this doesn’t work on mine.  I’m using VS2008 and 3.5 Framework.  I still get the 417 error.

  12. Vlad Loidap says:

    I’m using Yedda too. Thanks, this fixed it for me too!

  13. regmee says:

    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.

  14. Jeff says:

    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.

  15. Randy says:

    Thank you.  I have been banging my head against the wall with this one.

  16. Sam says:

    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!

  17. whaites says:

    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…

  18. Joe says:

    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

  19. great post. it worked for me too. i appreciate you code thanks.

  20. great post. it worked for me too. i appreciate you code thanks. Sandip

  21. DC says:

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

  22. HeuX Heulab says:

    Thanks! fixed the problem I’m using Yedda’s API as well

  23. HockeyBoyN says:

    This solved my problem too. Thanx

    I am using Yedda’s API.

    Thanx again.

  24. Wonsuk, Choi says:

    Thanks much!!!! You save me!!!

  25. santaClaus says:

    Tonight even with the changes with HttpWebRequest it does not work. I get the same exception (417). Any clue?

  26. santaClaus says:

    Tonight even with the changes with HttpWebRequest it does not work. I get the same exception (417). Any clue?

  27. santaClaus says:

    my mistake. Yes it works fine.

    Sorry for the confusion

  28. Bob Bartel says:

    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?

  29. Andrii says:

    The same error (401)! Any suggestions?

Skip to main content