none
C# Consume web service with BASIC authentication? RRS feed

  • 問題

  • Hello,

    I want to consume a Java web service (JAX-WS, Tomcat) from C# client.

    In the past, I will use "Add Web Reference" to generate the web service proxy code and use NetworkCredential for the BASIC HTTP authentication.

    But now, I use the "Add Service Reference" in Visual Studio 2008 SP1 to generate the web service proxy code. Would you please to teach me how can I define the BASIC authentication username and password for consuming the web service.

    When I directly retrieve the WSDL from browser, it will prompt me a login dialog for retrieving the WSDL.
    How can I set this authentication information for the generated proxy code in "Add Service Reference"

    Best regards,
    Eric
    2010年3月12日 上午 03:50

解答

  • If you do [Add Web Reference] but fail without prompting you for username and password, you can use the wsdl.exe command line tool, with /username parameter for username, and /password parameter for password
    • 已標示為解答 Lolota Lee 2010年3月19日 上午 08:27
    2010年3月16日 下午 12:13

所有回覆

  • You can apply network credentials to Service Proxy Class for client to call Web Service.

    <%@ Page Language="C#" %>
    <%@ Import Namespace="System.Net" %>

    <html>
        <script language="C#" runat="server">
           void EnterBtn_Click(Object Src, EventArgs E)
              {
                MyMath.Math math = new MyMath.Math();

                // Obtain password from a secure store.
                String SecurelyStoredPassword = String.Empty;

                // Set the client-side credentials using the Credentials property.
                ICredentials credentials = new NetworkCredential("Joe",SecurelyStoredPassword,"mydomain");
                math.Credentials = credentials;

                int total = math.Add(Convert.ToInt32(Num1.Text), Convert.ToInt32(Num2.Text));
                Total.Text = "Total: " + total.ToString();
             }

        </script>

        <body>
           <form action="MathClient.aspx" runat=server>

              Enter the two numbers you want to add and then press the Total button.
              <p>
              Number 1: <asp:textbox id="Num1" runat=server/>  +
              Number 2: <asp:textbox id="Num2" runat=server/> =
              <asp:button text="Total" Onclick="EnterBtn_Click" runat=server/>
              <p>
              <asp:label id="Total"  runat=server/>

           </form>
        </body>
     </html>


    以下為簽名檔,請勿對話入座:
    初學不是問題,但用不正確的態度來問問題,那就是很大的問題。
    請不要藉新手之名行小白之實,否則只會讓更多無辜的新手得不到幫助而已。
    如果不知道什麼是小白,請參閱:何謂小白
    2010年3月12日 上午 04:24
    版主
  • You can use the "Add Web Reference" function, not "Add Service Reference" function, when you execute the "Add Service Reference" function, please click the [Advanced] button in the Add Service Reference Dialog, you can find an "Add Web Reference" button at the bottom, you can click the "Add Web Reference", then go through like what you do with VS2005
    2010年3月12日 上午 05:46
  • Thanks for all your answers. But I don't know why I can't use "Add Web Service". When I use "Add Web Service" to consume the web service, it throws some exception, something like "applicatopm/soap+xml" not compatiple or something like that.

    But when use "Add Servive Reference", it works fine if no authentication.

    Any solution to use "Add Service Reference" and also can do the authentication? Please give me a hand.

    Thanks a lot.

    2010年3月16日 上午 09:14
  • If you do [Add Web Reference] but fail without prompting you for username and password, you can use the wsdl.exe command line tool, with /username parameter for username, and /password parameter for password
    • 已標示為解答 Lolota Lee 2010年3月19日 上午 08:27
    2010年3月16日 下午 12:13