none
401 Error Consuming REST service from code but works in browser RRS feed

  • Question

  • I am trying to consume a REST service from another company. When I use the link in the browser it works fine. However I get the 401 error when accessing from code. What am I doing wrong? Do I need to add something to the web.config of the web app?

                string URL = @"https://hdapps-qa.homedepot.com/MYTHDPassport/rs/identity/isSessionValid?thdsso=TEST&callingProgram=PSC&Submit=Submit";
                HttpWebRequest req = WebRequest.Create(URL)
                                     as HttpWebRequest;
                string result = null;
                req.Method = "GET";
                req.UseDefaultCredentials = true;
                req.PreAuthenticate = false;
                req.Credentials = CredentialCache.DefaultCredentials;
                using (HttpWebResponse resp = req.GetResponse()
                                              as HttpWebResponse)
                {
                    StreamReader reader =
                        new StreamReader(resp.GetResponseStream());
                    result = reader.ReadToEnd();
                }
    • Moved by Fred Bao Wednesday, December 4, 2013 2:50 AM It is a thread about WCF
    Tuesday, December 3, 2013 9:38 PM

Answers

  • Hi,

    I saw that your wcf rest service can work well on the browser, but can not work on code. And you meet the 401 error.

    So I think maybe your problem is caused by a permissions issue.
    My suggestion would be to try the call via fiddler from the clients machine and validate if you can get the expected result via a POST. If this is successful I would look into how you are hosting the service in IIS. I have seen on several occasions where the issue was Anonymous or Basic Authentication was turned off which caused similar issues.

    #Fiddler:
    http://fiddler2.com/ .

    Or please try to use the following to see if it works:

    <bindings>
          <webHttpBinding>
            <binding name="RESTBindingConfiguration">
              <security mode="None" />
            </binding>
         </webHttpBinding>
    </bindings>
    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, December 4, 2013 3:41 AM
    Moderator
  • Figured it out. The REST Service is returning a response code of 401 if the session is not valid. So I am catching that exception and handling it. Thanks.
    Wednesday, December 4, 2013 4:25 PM

All replies

  • Hello devilstower1,

    Since this issue is more related to WCF, I will move it to the correct forum:

    http://social.msdn.microsoft.com/Forums/en-US/home?forum=wcf

    There are WCF experts who will help you better.

    Thanks for your understanding.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, December 4, 2013 2:48 AM
  • Hi,

    I saw that your wcf rest service can work well on the browser, but can not work on code. And you meet the 401 error.

    So I think maybe your problem is caused by a permissions issue.
    My suggestion would be to try the call via fiddler from the clients machine and validate if you can get the expected result via a POST. If this is successful I would look into how you are hosting the service in IIS. I have seen on several occasions where the issue was Anonymous or Basic Authentication was turned off which caused similar issues.

    #Fiddler:
    http://fiddler2.com/ .

    Or please try to use the following to see if it works:

    <bindings>
          <webHttpBinding>
            <binding name="RESTBindingConfiguration">
              <security mode="None" />
            </binding>
         </webHttpBinding>
    </bindings>
    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, December 4, 2013 3:41 AM
    Moderator
  • The REST Service is from another company and I have no access. I tried the bindings and it still gives me the same error. The manual from the other company tells me that the method I am calling uses GET and I did use fiddler to see what was getting sent over when using the link in the browser and then from code and its identical. Any other ideas?
    Wednesday, December 4, 2013 3:45 PM
  • Figured it out. The REST Service is returning a response code of 401 if the session is not valid. So I am catching that exception and handling it. Thanks.
    Wednesday, December 4, 2013 4:25 PM