This project has moved and is read-only. For the latest updates, please go here.

System.Net.WebException: The remote server returned an error: (404) Not Found

Jun 28, 2013 at 4:09 PM
hi i am writing unfollow code but getting 404 error

my code like this
 string userId = "";
            userId = Request.QueryString["q"];
            if (userId == "") return;


            var auth = new WebAuthorizer
                Credentials = new SessionStateCredentials()

            if (!auth.IsAuthorized)

            var twitterCtx = new TwitterContext(auth);

                var user = twitterCtx.DestroyFriendship(userId, string.Empty);
                Response.Write(userId + " unfollowed ");
            catch (Exception ex)
printout is like this

System.Net.WebException: The remote server returned an error: (404) Not Found. at LinqToTwitter.TwitterExecute.PostToTwitter[T](String url, IDictionary2 postData, Func2 getResult)
Sorry, that page does not exist
Jun 29, 2013 at 4:29 AM

I think what you'll find is that the authenticated user is not following the user identified by the userId passed to DestroyFriendship. Twitter is a REST API, that uses HTTP as a transport. Twitter returns a 404 Not Found when the condition you're operating on doesn't exist. e.g. no friendship. If a friendship doesn't exist, then it can't be destroyed. .NET translates 4xx and 5xx return codes into WebException and throws that WebException. As you know, an exception means that the method was not able to perform the operation it set out to perform. That is true in this case because you told Twitter that the authenticated user wanted to unfollow another user, which isn't possible if the authenticated user wasn't following that user in the first place.

Therefore, the proper handling is to have a catch handler for WebException that can look at the status code returned, 404 Not Found, and take an action that makes sense for your application.

Jun 29, 2013 at 10:58 AM
Hi Joe,
Thanks for reply. But authenticated user is following the the user identified by the userId passed to DestroyFriendship. When i use CreateFriendship method with same userId, i am receiving the same error :(