[英]add a soap header in C#
我正在为Android构建Xamarin.forms应用程序,我们在其中使用第三方Web服务。 我已经创建了代理,并且可以看到该服务公开的方法。
但是我无法访问服务的方法,因为必须将标头添加到使用密钥的SOAP请求中。
代码段:为代理创建客户端
ThirdPartAuthService.AuthService clnt = new ThirdPartAuthService.AuthService();
clnt.getenquiry(XML);
我看不到添加标头以便进行身份验证的任何选项。 请指导我如何向我的请求添加肥皂标头。
在Android应用程序中可能会发生这种情况,因为他们正在创建添加标头并发送的SOAP对象。
样本标头xml请求:
<?xml version="1.0" encoding="utf-8"?>...
<soapenv:Header><ns1:encKey soapenv:actor="http://schemas.xmlsoap.org/
soap/actor/next" xsi:type="soapenc:string" xmlns:ns1=
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
abcde</ns1:encKey></soapenv:Header>..
我需要添加令牌,用户名和密码
基本上你不能。 您必须手动更改Soap文本,例如:
String soapBodyString = getXMLFromCache ();
int pos1 = soapBodyString.IndexOf ("<soap:Body");
int pos2 = soapBodyString.Length - pos1;
soapBodyString = soapBodyString.Substring (pos1, pos2);
string headerText = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" \n{0}>"
+ "<soapenv:Header><wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">"
+ "<wsse:UsernameToken xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">"
+ "<wsse:Username>{1}</wsse:Username>"
+ "<wsse:Password>{2}</wsse:Password>"
+ "<wsse:Signature>{3}</wsse:Signature>"
+ "</wsse:UsernameToken></wsse:Security></soapenv:Header>";
Stream appOutputStream = new MemoryStream ();
StreamWriter soapMessageWriter = new StreamWriter (appOutputStream);
headerText = string.Format (headerText,UriNamespace ,Username, Password, Signature);
soapMessageWriter.Write (headerText);
soapBodyString=soapBodyString.Replace("soap:Envelope", "soapenv:Envelope");
soapBodyString=soapBodyString.Replace("soap:Body", "soapenv:Body");
soapMessageWriter.Write(soapBodyString);
soapMessageWriter.Flush();
appOutputStream.Flush();
appOutputStream.Position = 0;
StreamReader reader = new StreamReader(appOutputStream);
StreamWriter writer = new StreamWriter(this.outputStream);
writer.Write(reader.ReadToEnd());
writer.Flush();
appOutputStream.Close();
通过覆盖reference.cs中的System.Net.WebRequest修复了此问题
protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
HttpWebRequest request;
request = (HttpWebRequest)base.GetWebRequest(uri);
NetworkCredential networkCredentials = Credentials.GetCredential(uri, "Basic");
//Other credentials
return request;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.