WCF The remote server returned an error: (401) Unauthorized - - HTTP POST URGENT RRS feed

  • Question

  • Client Code :


                String strUserName = "username";
                String strPassword = "password";
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; 
                ServicePointManager.Expect100Continue = false;
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://test.mydomain.com/Test/Service.svc/");
                request.KeepAlive = false;
                request.ProtocolVersion = HttpVersion.Version10;
                byte[] credentialBuffer = new UTF8Encoding().GetBytes(strUserName.Trim() + ":" + strPassword.Trim());
                request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(credentialBuffer);
                request.AllowAutoRedirect = true;
                request.MaximumAutomaticRedirections = 10;
                request.Method = "POST";
                request.PreAuthenticate = true;
                request.AllowWriteStreamBuffering = true;
                string postData = "Test Message";
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = byteArray.Length;
                // Get the request stream.
                Stream dataStream = request.GetRequestStream();
                // Write the data to the request stream.
                dataStream.Write(byteArray, 0, byteArray.Length);
                // Close the Stream object.
                // Get the response.
                WebResponse response = request.GetResponse();
                // Display the status.
                // Get the stream containing content returned by the server.
                dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(dataStream);
                // Read the content.
                string responseFromServer = reader.ReadToEnd();
                // Clean up the streams.

            catch (WebException wex)

                throw new WebException(wex.Message);


    Server Code :

    public interface IMyService
         [WebInvoke(UriTemplate = "", Method = "POST")]
         Stream mydata(Stream input);


    The Above code is working , when i  change the Client code 

    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(https://test.mydomain.com/Test/Service.svc);

    //Removed the slash from the URL

    Web server return the error like "The remote server returned an error: (401) Unauthorized."

    but Fiddler showing 
    Response Headers:

    HTTp:/1.1 307 Temporary Redirect

    There is no operation listening for https://test.mydomain.com/Test/Service.svc, but there is an operation listening for https://test.mydomain.com/Test/Service.svc/, so you are being redirected there.

    Please advise .

     Note : Web Server runing => 1.Basic Auth 2. Webhttpbinding  3.SSL

    Thanks, Kanna
    Wednesday, September 9, 2009 8:21 AM


All replies

  • Hi Kanna,
       It should be your User account problem
    Please check that if this user has the permission to call this opreation.

    Frank Xu Lei--谦卑若愚,好学若饥
    Focus on Distributed Applications Development and EAI based on .NET
    欢迎访问老徐的中文技术博客:Welcome to My Chinese Technical Blog
    欢迎访问微软WCF中文技术论坛:Welcome to Microsoft Chinese WCF Forum
    欢迎访问微软WCF英文技术论坛:Welcome to Microsoft English WCF Forum
    Wednesday, September 9, 2009 10:50 AM
  • Thanks for your reply ...But The Same User can access below scenario's without any problem.


    Server Code :

    [WebInvoke(UriTemplate = "postdata", Method = "POST")]

    Client Code :

     HttpWebRequest request =

    Server Code:

    [WebInvoke(UriTemplate = "", Method = "POST")]

    Client Code:

     HttpWebRequest request =

    when we used https://test.mydomain.com/Test/Service.svc URL ( without slash(/)) getting the error.

    Server Code :

    [WebInvoke(UriTemplate = "", Method = "POST")]

    Client Code :

     HttpWebRequest request =

    could you Please tell us what settings that  i have missed ?

    Custom Basic authentication ( user is stored in SQL DB)

    Thanks, Kanna
    Wednesday, September 9, 2009 2:15 PM
  • since your uritemplate is empty string.  You will have to put the trailing / to redirect the call to the correct operation contract.  This is the reason you are getting the error since it is not able to redirect your call to the correct operation when you are missing /
    Amit Sharma
    Wednesday, September 9, 2009 3:31 PM
  • No luck ...

    [WebInvoke(UriTemplate = "*", Method = "*")] also not working , getting the same error.

    Please help me

    Thanks, Kanna
    Wednesday, September 9, 2009 11:48 PM
  • 1.IIS Log Showing Error code - 307

    2.I was sure I sent the proper authorization headers with the request.
    (Application event showing the user Authentication success entry)

    3.changed client code request.AllowAutoRedirect = false ;

    Error showing like

    Temporary Redirect

    There is no operation listening for https://<Localcomputer name>/incoming/Myservice.svc, but there is an operation listening for https://Test.mydomain.com/incoming/Myservice.svc/, so you are being redirected there.

    Please advise

    Thanks, Kanna
    Thursday, September 10, 2009 12:17 AM
  • Hi Kanna ,

    Like Amit said , you defined your UriTemplate an empty string , so to access this method you need to use the path https://test.mydomain.com/Test/Service.svc/  . As for the UrlTemplate which is set to "postdata" , you will need to access it via (https://test.mydomain.com/Test/Service.svc/postdata. So if you are using https://test.mydomain.com/Test/Service.svc , it will not stand for a webservice method.

    To understand this, I suggest you to read this reference in MSDN library about the UriTemplate defination:


    Hope this could help you.

    Thanks , Quan
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Senthil Veera Tuesday, September 15, 2009 4:23 AM
    Monday, September 14, 2009 11:57 AM
  • Hi Kanna,

    For supplying basic authentication credentials with WebRequest component, you can use the NetworkCredential class instead of manually inject the http header. i.e.

      Uri uri = new Uri("http://www.asp.net");
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
                NetworkCredential netCredential = new NetworkCredential("Username", "Password");
                ICredentials basicCred = netCredential.GetCredential(uri, "Basic");
                webRequest.Credentials = basicCred;
                webRequest.PreAuthenticate = true;

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, September 15, 2009 3:37 AM