2011-08-23 89 views
1

试图消耗WebService时,我不断收到以下错误:WCF REST消费误差

HTTP请求是未经授权的客户端身份验证方案“基本”。从服务器收到的身份验证头是'Basic Realm'。

该web服务是用WCF编写的REST。身份验证基本上通过https。

修复错误的任何帮助都会被认可。

这里是我试过的代码:

WebHttpBinding webBinding = new WebHttpBinding(); 
    webBinding.Security.Mode = WebHttpSecurityMode.Transport; 
    webBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; 

    ChannelFactory<ServiceReferences.BTService.FLDT_WholesaleService> factory = new ChannelFactory<ServiceReferences.BTService.FLDT_WholesaleService>(webBinding, 
                     new EndpointAddress(
                      "https://wholesale.fluidata.co.uk/FLDT_BT_wholesale/Service.svc")); 
    factory.Endpoint.Behaviors.Add(new WebHttpBehavior()); 
    factory.Credentials.UserName.UserName = "username"; 
    factory.Credentials.UserName.Password = "password"; 

    ServiceReferences.BTService.FLDT_WholesaleService proxy = factory.CreateChannel(); 

    proxy.AvailabilityCheck("123"); 
+0

您是否在IIS上托管REST服务? – Codo

+0

@Codo是的。我在IIS中托管它 –

+0

谁应该检查用户名和密码? IIS或WFC服务? – Codo

回答

1

只要你暴露RESTful服务,你可以尝试使用Fiddler - http://www.fiddler2.com/fiddler2/和/或正常的HttpRequest/HttpResponse对象。你尝试过那样的事吗?

+0

我可以使用httpwebrequest和HttpResponse。我需要使用ChannelFactory来使用它,所以我使用类而不是XML。 –

0

Franek先生的回答很有用 - 您将在WCF工作期间使用Fiddler。我可以添加一点点...这里发生的是,您已经将“Basic”指定为您的身份验证方案作为客户端。服务器说“我只允许'基本领域'”作为认证方案。什么是'领域'?基本上凭证命名空间:

Realm for HTTP basic authentication

下面是另一个有用的链接:Authentication in WinHTTP

我无法找到一个属性或方法重载携带境界......我可能会尝试构建Authenticate-手动标题WWW

那会去是这样的:

request.Headers.Add("WWW-Authenticate", string.Format("basic realm=\"{0}\", realm)); 

“境界”是什么的服务器要求的值,例如,“www.targetsite.com”。

+0

您可以发布一个如何手动委托Authenticate-WWW头的例子吗? –