这是一个问题,因为该项目已经在使用Visual Studio 2008(针对.NET 2.0).我可以在VS2005中做到这一点,但是我不想将项目绑定到VS2005,也可以通过在VS2005中创建一个程序集,包括VS2008中的一个程序集).我认为这两个选项中的任何一个都会使新开发人员变得复杂,迫使他们安装WSE 3.0,并使项目能够在将来使用2008和.NET 3.5中的功能…即我真的相信使用WCF是要走的路
我一直在研究使用WCF,但是我不确定如何让WCF服务了解它需要发送身份验证头以及每个请求.当我尝试对Web服务做任何事情时,我遇到401错误.
这是我的代码看起来像:
WebHttpBinding webBinding = new WebHttpBinding(); ChannelFactory<MyService> factory = new ChannelFactory<MyService>(webBinding,new EndpointAddress("http://127.0.0.1:80/Service/Service/")); factory.Endpoint.Behaviors.Add(new WebHttpBehavior()); factory.Credentials.UserName.UserName = "username"; factory.Credentials.UserName.Password = "password"; MyService proxy = factory.CreateChannel(); 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.
任何关于可能导致这个问题的想法将不胜感激.
解决方法
现在,使用“webHttpBinding”,您正在使用基于REST的方法.如果Java服务是SOAP服务,那么您需要将绑定更改为“basicHttpBinding”.
如果是一个基于SOAP的服务,你应该尝试这样做:
BasicHttpBinding binding = new BasicHttpBinding(); binding.SendTimeout = TimeSpan.FromSeconds(25); binding.Security.Mode = BasicHttpSecurityMode.Transport; binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; EndpointAddress address = new EndpointAddress(your-url-here); ChannelFactory<MyService> factory = new ChannelFactory<MyService>(binding,address); MyService proxy = factory.CreateChannel(); proxy.ClientCredentials.UserName.UserName = "username"; proxy.ClientCredentials.UserName.Password = "password";
我已经使用它与各种Web服务,它的工作 – 大部分时间.
如果不行,那么你必须了解更多有关Java Web服务预期的内容,以及如何发送相关信息.
渣子
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。