The request failed with HTTP status 401: Unauthorized

Discussion in 'ASP .Net' started by ad, Feb 17, 2006.

  1. ad

    ad Guest

    I have a WebService in localhost, and the set in the web.config is

    <add key="WebReference.Service"
    value="http://localhost/HealthService/Service.asmx"/>
    and the code to refer the WebService is
    WebReference.Service wsHealth = new WebReference.Service();

    But when I use code to execute a function in the webservice, like
    ds =(System.Data.DataSet)wsHealth.GetSchools_Zip(txtZip1.Text);

    it result into an error:
    The request failed with HTTP status 401: Unauthorized


    I have type the web service URL into browse, it execute OK, but it can't
    call in program.

    How can I do?
    ad, Feb 17, 2006
    #1
    1. Advertising

  2. ad

    Brock Allen Guest

    Sounds like IIS is using integrated authentication for that application.
    Your browser supplies the credentials of the logged in user, whereas the
    web service proxy does not. You can either change the code for your web service
    proxy to 1) hard code the credntials, 2) prompt the user and pass those credentials,
    or 3) use the current logged in user's credentials. Check out the Credentials
    property of teh web service proxy. Set it to CredentialCache.DefaultCredentials
    to get the thrid behavior I listed. For the first or second, use an instance
    of the System.Net.NEtworkCredential class.

    -Brock
    http://staff.develop.com/ballen


    > I have a WebService in localhost, and the set in the web.config is
    >
    > <add key="WebReference.Service"
    > value="http://localhost/HealthService/Service.asmx"/>
    > and the code to refer the WebService is
    > WebReference.Service wsHealth = new WebReference.Service();
    > But when I use code to execute a function in the webservice, like
    > ds =(System.Data.DataSet)wsHealth.GetSchools_Zip(txtZip1.Text);
    > it result into an error:
    > The request failed with HTTP status 401: Unauthorized
    > I have type the web service URL into browse, it execute OK, but it
    > can't call in program.
    >
    > How can I do?
    >
    Brock Allen, Feb 17, 2006
    #2
    1. Advertising

  3. ad

    ad Guest

    Thanks,
    I use the method 3, and my problem is gone away.
    But I have some question:

    I have set virtual directory of the WebService to anonymous, why it need
    Credentials ?



    "Brock Allen" <>
    ???????:...
    > Sounds like IIS is using integrated authentication for that application.
    > Your browser supplies the credentials of the logged in user, whereas the
    > web service proxy does not. You can either change the code for your web
    > service proxy to 1) hard code the credntials, 2) prompt the user and pass
    > those credentials, or 3) use the current logged in user's credentials.
    > Check out the Credentials property of teh web service proxy. Set it to
    > CredentialCache.DefaultCredentials to get the thrid behavior I listed. For
    > the first or second, use an instance of the System.Net.NEtworkCredential
    > class.
    >
    > -Brock
    > http://staff.develop.com/ballen
    >
    >
    >> I have a WebService in localhost, and the set in the web.config is
    >>
    >> <add key="WebReference.Service"
    >> value="http://localhost/HealthService/Service.asmx"/>
    >> and the code to refer the WebService is
    >> WebReference.Service wsHealth = new WebReference.Service();
    >> But when I use code to execute a function in the webservice, like
    >> ds =(System.Data.DataSet)wsHealth.GetSchools_Zip(txtZip1.Text);
    >> it result into an error:
    >> The request failed with HTTP status 401: Unauthorized
    >> I have type the web service URL into browse, it execute OK, but it
    >> can't call in program.
    >>
    >> How can I do?
    >>

    >
    >
    ad, Feb 18, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sachin
    Replies:
    2
    Views:
    17,860
    Sachin
    Nov 4, 2005
  2. ad
    Replies:
    0
    Views:
    1,614
  3. Kevin Burton

    The request failed with HTTP status 401: Unauthorized.

    Kevin Burton, Dec 17, 2004, in forum: ASP .Net Web Services
    Replies:
    2
    Views:
    141
    Mohamed El Ashmawy
    Dec 19, 2004
  4. Sachin

    The request failed with HTTP status 401: Unauthorized

    Sachin, Nov 4, 2005, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    120
    Sachin
    Nov 4, 2005
  5. manika02

    The request failed with HTTP status 401: Unauthorized

    manika02, Mar 29, 2006, in forum: ASP .Net Web Services
    Replies:
    7
    Views:
    196
    geyser hi
    Apr 10, 2006
Loading...

Share This Page