我有一个WCF服务,我试图通过c#中的测试客户端项目消费,
我正在使用SOAPUI应用程序,然后从这个应用程序,我能够传递身体与请求中的身份验证,并能够解码它。
但是我可以通过测试客户端项目传递soap body部分,但是不能在请求中将标题和body部分一起传递身份验证。
以下代码我在测试客户端项目中编写:
AService.AServiceClient client = new AService.AServiceClient();
AService.GetAByKeyRequest request = new AService.GetAByKeyRequest
{
Authorization = "xyz:123",
AKey = "123"
};
var SoapResponse = ((AService.IAService)client).GetAByKey(request);
以下是正在使用请求的代码的一部分:
GetAResponse GetAByKey(GetAByKeyRequest getAByKeyRequest)
{
//...
string basicAuthorization = request.Headers[System.Net.HttpRequestHeader.Authorization];
//...
}
请提出一些想法
不清楚SoapUI测试客户端,但如果您使用的是wcf客户端。您可以通过HttpRequestMessageProperty添加http头。
MetadataTestClient client = new MetadataTestClient();
try
{
using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
HttpRequestMessageProperty property;
if (OperationContext.Current.OutgoingMessageProperties.ContainsKey(HttpRequestMessageProperty.Name)){
property = OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
}
else
{
property = new HttpRequestMessageProperty();
//HttpRequestMessagProperty is an item in OutgoingMessageProperties
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = property;
}
// add property to HttpRequestMessagePropery could set HttpHeader
property.Headers.Add (System.Net.HttpRequestHeader.Authorization, "myAuthorization");
string re = client.HelloWorld();
}
}