使用WCF无法通过基本认证调用Web服务

前端之家收集整理的这篇文章主要介绍了使用WCF无法通过基本认证调用Web服务前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经获得了一个用 Java编写的Web服务,我无法进行任何更改.它要求用户使用基本身份验证来访问任何方法.在.NET中与此服务交互的建议方法是使用安装了WSE 3.0的Visual Studio 2005.

这是一个问题,因为该项目已经在使用Visual Studio 2008(针对.NET 2.0).我可以在VS2005中做到这一点,但是我不想将项目绑定到VS2005,也可以通过在VS2005中创建一个程序集,包括VS2008中的一个程序集).我认为这两个选项中的任何一个都会使新开发人员变得复杂,迫使他们安装WSE 3.0,并使项目能够在将来使用2008和.NET 3.5中的功能…即我真的相信使用WCF是要走的路

我一直在研究使用WCF,但是我不确定如何让WCF服务了解它需要发送身份验证头以及每个请求.当我尝试对Web服务做任何事情时,我遇到401错误.

这是我的代码看起来像:

  1. WebHttpBinding webBinding = new WebHttpBinding();
  2. ChannelFactory<MyService> factory =
  3. new ChannelFactory<MyService>(webBinding,new EndpointAddress("http://127.0.0.1:80/Service/Service/"));
  4. factory.Endpoint.Behaviors.Add(new WebHttpBehavior());
  5. factory.Credentials.UserName.UserName = "username";
  6. factory.Credentials.UserName.Password = "password";
  7.  
  8. MyService proxy = factory.CreateChannel();
  9. proxy.postSubmission(_postSubmission);

这将运行并抛出以下异常:

The HTTP request is unauthorized with client authentication scheme ‘Anonymous’. The authentication header received from the server
was ‘Basic realm=realm’.

这有一个例外:

The remote server returned an error: (401) Unauthorized.

任何关于可能导致这个问题的想法将不胜感激.

解决方法

第一个问题:这是一个SOAP或基于REST的Java服务,您正在尝试调用

现在,使用“webHttpBinding”,您正在使用基于REST的方法.如果Java服务是SOAP服务,那么您需要将绑定更改为“basicHttpBinding”.

如果是一个基于SOAP的服务,你应该尝试这样做:

  1. BasicHttpBinding binding = new BasicHttpBinding();
  2.  
  3. binding.SendTimeout = TimeSpan.FromSeconds(25);
  4.  
  5. binding.Security.Mode = BasicHttpSecurityMode.Transport;
  6. binding.Security.Transport.ClientCredentialType =
  7. HttpClientCredentialType.Basic;
  8.  
  9. EndpointAddress address = new EndpointAddress(your-url-here);
  10.  
  11. ChannelFactory<MyService> factory =
  12. new ChannelFactory<MyService>(binding,address);
  13.  
  14. MyService proxy = factory.CreateChannel();
  15.  
  16. proxy.ClientCredentials.UserName.UserName = "username";
  17. proxy.ClientCredentials.UserName.Password = "password";

我已经使用它与各种Web服务,它的工作 – 大部分时间.

如果不行,那么你必须了解更多有关Java Web服务预期的内容,以及如何发送相关信息.

渣子

猜你在找的HTML相关文章