【发布时间】:2017-07-14 13:03:32
【问题描述】:
这是标题的样子
<soap:Header>
<AuthenticationHeader>
<UserName>string</UserName>
<Password>string</Password>
</AuthenticationHeader>
</soap:Header>
这是我尝试过的:
string username = "TheUserName";
string password = "ThePassword";
HttpRequestMessage requestMessage = new HttpRequestMessage(method, uri);
requestMessage.Headers.Add("UserName", username);
requestMessage.Headers.Add("Password", password);
也许我必须以某种方式设置授权标头?
requestMessage.Headers.Authorization = ??
我觉得我必须以某种方式“构建”AuthenticationHeader 元素,但我不知道该怎么做。有什么建议吗?
编辑:完整的 SOAP 信封
?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<AuthenticationHeader xmlns="http://www.test.com/testing/Security">
<UserName>string</UserName>
<Password>string</Password>
</AuthenticationHeader>
</soap:Header>
<soap:Body>
<GetMeSomething xmlns="http://www.test.com/testing/WorkFileCatalog">
<Param1>string</Param1>
<Param2>string</Param2>
<XMLRetMess>string</XMLRetMess>
</GetMeSomething>
</soap:Body>
</soap:Envelope>
【问题讨论】:
-
正常和通用的方法是将用户和密码附加到用于发送请求的对象上,例如
yourObject.Credentials = new System.Net.NetworkCredential(username, password);,您尝试过吗? -
我不确定您所说的
yourObject是什么意思。我正在尝试使用HttpClient。我实际上创建了一个服务引用,它大大简化了事情,但我应该能够使用HttpClient而只是将服务引用用于请求和响应对象。我只是在语法上有问题,因为我不知道所有这些对象如何转换为标记。 -
如果您使用
HttpClient作为请求对象,那么只需attach the credentials like this。 -
如果这不起作用,您确实需要分两步进行发送,首先发送身份验证,等待回复,然后发送您的正常请求(通常带有带有响应的标头您从身份验证中获得)...
-
@Pittfall 你知道 SOAP 标头是消息信封的一部分,它作为请求正文的端口发送,与 HTTP 请求授权标头无关?
标签: c# web-services soap httpclient