none
如何调用带基本验证的WebService?我在msdn试过一些例子,都生成Web引用类型不对 RRS feed

  • 问题

  • 1.websercvice是用tomcat做的(http://localhost:8080/extjs/ws?wsdl)

    在浏览器中访问这个网址是需要用户名和密码的.

    这个是一开始写的例子,运行到调用集成方法时就报错

               TestWS1.HWTest.HelloWorldClient service = new TestWS1.HWTest.HelloWorldClient();
                service.ClientCredentials.UseIdentityConfiguration = true;
                service.ClientCredentials.UserName.UserName = "username";
                service.ClientCredentials.UserName.Password = "password";
                string testBox1test  = service.getHelloWorldAsString();

    异常提示为:

    Additional information: HTTP 请求未经客户端身份验证方案“Anonymous”授权。从服务器收到的身份验证标头为

    “Basic realm="Authentication required"”。

    后来又看到http://msdn.microsoft.com/en-us/bfazk0tb(zh-cn).aspx这里例子

                CredentialCache credentialCache = new CredentialCache();
                NetworkCredential credentials = new NetworkCredential("huchao", "huchao");
                credentialCache.Add(new Uri("http://localhost:8080/extjs/ws?wsdl"), "Basic", credentials);

    最后怎么也不能将credentials赋值给service

    开发工具是vs2013,Web引用是Runtime Version:4.0.30319.18449生成的

    2014年8月27日 15:13

全部回复

  • 这样尝试一下:

    // Create a new instance of the proxy class to an XML 
       // Web service method.  
        YourWebService math = new YourWebService();
     
        // Create a new instance of CredentialCache. 
        CredentialCache credentialCache = new CredentialCache(); 
     
       // Create a new instance of NetworkCredential using the client 
       // credentials. 
       NetworkCredential credentials = new 
          NetworkCredential(UserName,SecurelyStroredPassword,Domain); 
     
       // Add the NetworkCredential to the CredentialCache. 
       credentialCache.Add(new Uri(math.Url),  
                           "Basic", credentials); 
     
       // Add the CredentialCache to the proxy class credentials. 
       math.Credentials = credentialCache; 
     
         // Call the method on the proxy class. 
        math.XXX(……);

    ASP.NET Forum
    Other Discussion Forums
    FreeRice Donate
    Issues to report
    Free Tech Books Search and Download

    2014年10月25日 7:14