How to tips and tricks for Microsoft Visual Studio .net

Showing posts with label (417) Expectation Failed. Show all posts
Showing posts with label (417) Expectation Failed. Show all posts

Thursday, July 22, 2010

(417) Expectation Failed - Twitter and .net aren't nice to each other


I have been writing applications that work with the Twitter API for a few years now. It's pretty simple, but if you don't know where to start, you could run into some trouble.

The first big problem I ran into was the "(417) Expectation Failed" error that Twitter started to return one day out of the blue.

I was using the Yedda dll to do all of my interaction with the Twitter API, and I didn't really know how it worked, so I went through it for a while, getting to know it.
I then got a little frustrated with it, and wrote my own Twitter class to use. I still had the "(417) Expectation Failed" error coming back when I tried to post to the API.

After a little research, I found out that the API no longer likes the HTTP header with the "100-Continue" value.

To fix this, I needed to add the following line of code to my method:

"request.ServicePoint.Expect100Continue = false;"

This fixed my Post code, and I was happy for a little while.

Then my world came crashing down, and the error came back. Again, it was without warning, and I was left scratching my head.

I went back to Google, and after a few hours searching, I found the solution.

This is how I did it:
     public static String ExecutePostCommand(String url, String UserName, String Password, String data)  
     {  
       HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);   
       request.ServicePoint.Expect100Continue = false;  
       try  
       {  
         if (!String.IsNullOrEmpty(UserName) && !String.IsNullOrEmpty(Password))  
         {  
           request.Credentials = new NetworkCredential(UserName, Password);  
           request.ContentType = "application/x-www-form-urlencoded";  
           request.Method = "POST";  
           byte[] bytes = Encoding.UTF8.GetBytes(data);  
           request.ContentLength = bytes.Length;  
           using (Stream requestStream = request.GetRequestStream())  
           {  
             requestStream.Write(bytes, 0, bytes.Length);  
             //HttpWebResponse response = (HttpWebResponse)request.GetResponse()  
             using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())  
             {  
               using (StreamReader reader = new StreamReader(response.GetResponseStream()))  
               {  
                 return reader.ReadToEnd();  
               }  
             }  
           }  
         }  
       }  
       catch (WebException ex)  
       {  
         if (ex.Response is HttpWebResponse)  
         {  
           if ((ex.Response as HttpWebResponse).StatusCode == HttpStatusCode.NotFound)  
           {  
             return null;  
           }  
         }  
         throw ex;  
       }  
       return null;  
     }  

You will see that I am using a HTTPWebRequest in the code. The original Yedda dll used a WebRequest instead.


My code works now, and I'm happy, that's until the next little/big API change that makes me pull my already thinning hair out.