ProtocolException: The remote server returned an unexpected response: (405) Method Not Allowed.(Resolved)

May 15, 2010 at 12:21 pm 2 comments

ProtocolException: The remote server returned an unexpected response: (405) Method Not Allowed.(Resolved)

 

Last week I face this error while accessing the WebGet method of a restful service. Thanks to Pedrams post for providing the solution.

So here it is.

Solution

The error shows up because without creating a new context, proxy ends up using the POST verb which will eventually cause the above exception

To resolve it we need to create a new OperationContextScope everytime we make a call to a WebGet method.

Sample code:

        [ServiceContract]

        public interface ITwitterStatus

        {

            [OperationContract]

            [WebGet(UriTemplate = “/statuses/friends.xml”)]

            Message GetFriends();

        }

    public partial class _Client

    {

        protected string GetFriends()

        {

            WebHttpBinding binding = new WebHttpBinding(WebHttpSecurityMode.TransportCredentialOnly);

            binding.MaxReceivedMessageSize = 99999999;

            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

            binding.Security.Transport.Realm = “Twitter API”;

            using (WebChannelFactory<ITwitterStatus> cf = new WebChannelFactory<ITwitterStatus>(binding, new Uri(“http://www.twitter.com&#8221;)))

            {

                cf.Credentials.UserName.UserName = “username”;

                cf.Credentials.UserName.Password = “password”;

                ITwitterStatus proxy = cf.CreateChannel();

                using((IDisposable)proxy)

                using (new OperationContextScope((IContextChannel)proxy))

                {

                    Message m = proxy.GetFriends();

                    return(XDocument.Load(m.GetReaderAtBodyContents()).ToString());

                }

             }

     }

Advertisements

Entry filed under: Silverlight. Tags: , , , , .

Multi Paged Silverlight application consuming ADO.Net Data services Azure Managed Disk VS AWS EBS

2 Comments Add your own

  • 1. Tony  |  June 17, 2011 at 5:06 am

    Thank you, it helped me solve a similar problem

    Reply
  • 2. Denny  |  July 3, 2013 at 10:06 am

    I always emailed this weblog post page to all
    my associates, as if like to read it then my links will too.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Recent Posts


%d bloggers like this: