我在将标头信息添加到SOAP消息时遇到问题。任何人都知道如何以正确的方式做到这一点?
我已成功添加Authenticator部分但仍无法在Authenticator中添加UserName和Password,如下所示。您也可以在下面看到C#代码!
<?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>
<Authenticator xmlns="http://www.namespacename.com/services/">
<UserName>string</UserName>
<Password>string</Password>
</Authenticator>
</soap:Header>
<soap:Body>
<ListItems xmlns="http://www.namespacename.com/services/">
<strCode>string</strCode>
</ListItems>
</soap:Body>
</soap:Envelope>
C#代码
var client = new MySoapClient();
client.GetListCompleted += (a, b) =>
{
var list = b.Result;
};
using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
MessageHeader header = MessageHeader.CreateHeader("Authenticator", "http://......./", credentials);
OperationContext.Current.OutgoingMessageHeaders.Add(header);
client.GetListAsync(App.CouponCampaignCode);
}
public class _ServiceCredential
{
[XmlAttribute(Form = System.Xml.Schema.XmlSchemaForm.None)]
[DataMember(Order = 2)]
public string Password;
[DataMember(Order = 1)]
[XmlAttribute(Form = System.Xml.Schema.XmlSchemaForm.None)]
public string UserName;
}
答案 0 :(得分:0)
private void callWebservice()
{
NetworkCredential credentials = new NetworkCredential(userName, Password, domain);
HttpWebRequest request = CreateWebRequest(url, credentials);
XDocument soapEnvelope = CreateSoapEnvelope(soapEnvelope );
InsertSoapEnvelopeIntoWebRequest(soapEnvelope, request);
}
private static HttpWebRequest CreateWebRequest(string url, NetworkCredential credentials)
{
string action = link;// my action link
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Credentials = credentials;
req.Headers["SOAPAction"] = action;
req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/xml";
req.Method = "POST";
return req;
}
private static XDocument CreateSoapEnvelope(string content)
{
XDocument soapEnvelopeXml = XDocument.Parse(content);
return soapEnvelopeXml;
}
private static void InsertSoapEnvelopeIntoWebRequest(XDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
webRequest.BeginGetRequestStream((IAsyncResult asynchronousResult) =>
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
Stream postStream = request.EndGetRequestStream(asynchronousResult);
soapEnvelopeXml.Save(postStream);
postStream.Close();
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}, webRequest);
}