在Visual Studio 2008中将Soap Header添加到Soap请求中
-
06-07-2019 - |
题
我正在尝试使用第三方网络服务(因此我无法访问网络服务代码)。 在Visual Studio 2008中,我创建了一个新的网站项目(ASP和c#),并添加了Web引用(不是Web服务!所以我猜它不是WCF服务......正确吗?)。
问题是,从Web服务的文档中我知道每个soap请求必须与以下信封和标题一起发送,您能告诉我如何在我的Soap请求中添加它吗? 我发现的所有解决方案都需要修改Web服务源或代理,我不能这样做,因为我无权访问Web服务源,Visual Studio 2008中客户端的Web服务代理是只读的临时文件!
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<soap:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" soap:mustUnderstand="1">
<wsse:UsernameToken xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:Username>gimme.data@stats.com</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">Ima5tatto</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soap:Header>
<soap:Body xmlns:ns2="http://neighbourhood.statistics.gov.uk/nde/v1-0/discoverystructs">
<ns2:AreaAtLevelElement>
<AreaIdWithLevelType>
<AreaId>276704</AreaId>
<LevelTypeId>12</LevelTypeId>
</AreaIdWithLevelType>
</ns2:AreaAtLevelElement>
</soap:Body>
</soap:Envelope>
解决方案
您可以使用标题将静态添加标题添加到配置文件中的邮件中 端点元素中的元素。 header元素的每个子元素都将按照邮件标题中的原样进行复制。
其他提示
我正在努力解决同样的问题,到目前为止已经写了一篇消息检查员能够访问SOAP标头,虽然我不知道如何在那里获得wsse:security的东西,而不必手动完成。我希望能够使用WS-Security模式(以及SAML模式)来构建wsse:security的东西......
我的消息检查员代码如下所示,如果我解决了这个问题,我会将它发布在这个帖子上。
这是我将行为添加到客户端的地方:
client.Endpoint.Behaviors.Add(new CustomBehavior());
msgOutput = client.ProvideAndRegisterDocumentSetXDR(msgInput);
这是消息检查器和自定义行为:
public class CustomMessageInspector : System.ServiceModel.Dispatcher.IClientMessageInspector
{
public void AfterReceiveReply(ref WCF.Message reply, object correclationState)
{
}
public Object BeforeSendRequest(ref WCF.Message request, IClientChannel channel)
{
MessageHeaders headers = new MessageHeaders(MessageVersion.Soap11WSAddressing10);
MessageHeader header = MessageHeader.CreateHeader("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "");
request.Headers.Add(header);
return null;
}
}
public class CustomBehavior : System.ServiceModel.Description.IEndpointBehavior
{
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRunTime)
{
CustomMessageInspector inspector = new CustomMessageInspector();
clientRunTime.MessageInspectors.Add(inspector);
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
}
public void Validate(ServiceEndpoint endpoint)
{
}
}
不隶属于 StackOverflow