在 C# 中添加肥皂 header
add a soap header in C#
我正在为 android 构建一个 Xamarin.forms 应用程序,我们在其中使用第三方网络服务。我已经创建了代理,我可以看到服务公开的方法。
但是我无法访问该服务的方法,因为我必须将 header 添加到我的获取密钥的 SOAP 请求中。
代码片段:为代理创建客户端
ThirdPartAuthService.AuthService clnt = new ThirdPartAuthService.AuthService();
clnt.getenquiry(XML);
我没有看到任何添加 header 的选项,以便进行身份验证。请指导我如何在我的请求中添加肥皂 header..
它可能在 android 应用程序中创建 SOAP object 附加 header 并发送。
示例 header 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;
}
我正在为 android 构建一个 Xamarin.forms 应用程序,我们在其中使用第三方网络服务。我已经创建了代理,我可以看到服务公开的方法。
但是我无法访问该服务的方法,因为我必须将 header 添加到我的获取密钥的 SOAP 请求中。
代码片段:为代理创建客户端
ThirdPartAuthService.AuthService clnt = new ThirdPartAuthService.AuthService();
clnt.getenquiry(XML);
我没有看到任何添加 header 的选项,以便进行身份验证。请指导我如何在我的请求中添加肥皂 header..
它可能在 android 应用程序中创建 SOAP object 附加 header 并发送。
示例 header 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;
}