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;

            Response.Write(userId);
            Response.Write("<br/>");

            var auth = new WebAuthorizer
            {
                Credentials = new SessionStateCredentials()
            };

            if (!auth.IsAuthorized)
            {
                return;
            }

            var twitterCtx = new TwitterContext(auth);

            try
            {
                Response.Write(twitterCtx.BaseUrl);
                Response.Write("<br/>");
                var user = twitterCtx.DestroyFriendship(userId, string.Empty);
                Response.Write(userId + " unfollowed ");
            }
            catch (Exception ex)
            {
                Response.Write(ex.InnerException);
                Response.Write("<br/>");
                Response.Write(ex.Message);
            }
printout is like this

1552638524
https://api.twitter.com/1.1/
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
Coordinator
Jun 29, 2013 at 4:29 AM
Hi,

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.

@JoeMayo
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 :(