gpt4 book ai didi

android - Ksoap2 android - 传递简单的 SOAP 头

转载 作者:行者123 更新时间:2023-11-29 21:02:40 26 4
gpt4 key购买 nike

我正在使用 ksoap2 调用 WCF 服务,据我所知,这个库允许您以不同的方式创建带有 header 的请求,但我找不到创建带有最简单 header 的请求的方法。我想要实现的是在将要发送的 xml 中具有此结构:

<soap:Header>

<authToken>tokenhere</authToken>

</soap:Header>

我能得到的最接近的是

Element[] header = new Element[1];
header[0] = new Element().createElement(null, "authToken");


header[0].setAttribute(null, "authToken" ,authToken);
envelope.headerOut = header;

产生这个:

<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:d="http://www.w3.org/2001/XMLSchema"
xmlns:c="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
<v:Header><authToken authToken="PD94bWwgdmVyc" /></v:Header>
<v:Body></v:Body>
</v:Envelope>

最佳答案

这对我来说很有用,可以在 Android 的 kSOAP 库中提供安全请求 header

SOAP 要求。 header :

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cif="http://www.mawarid.ae/linkedCardsSummary/CRM/CIF.xsd">
<soapenv:Header>
<wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:UsernameToken wsu:Id="UsernameToken-14CBAE357AC169AFA614664925178422">
<wsse:Username>XXXXXXXXXXXX</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">XXXXXXXXXXXX</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>

安卓代码:

public static Element buildAuthHeader() {
Element headers[] = new Element[1];
headers[0]= new Element().createElement("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security");
headers[0].setAttribute(null, "mustUnderstand", "1");
Element security=headers[0];

//user token
Element usernametoken = new Element().createElement(security.getNamespace(), "UsernameToken");
usernametoken.setAttribute("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "Id", "UsernameToken-14CBAE357AC169AFA614664925178422");

//username
Element username = new Element().createElement(security.getNamespace(), "Username");
username.addChild(Node.TEXT, HttpConstant.REQ_HEADER_USERNAME);
usernametoken.addChild(Node.ELEMENT,username);

// password
Element password = new Element().createElement(security.getNamespace(), "Password");
password.setAttribute(null, "Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
password.addChild(Node.TEXT, HttpConstant.REQ_HEADER_PASSWORD);
usernametoken.addChild(Node.ELEMENT,password);


headers[0].addChild(Node.ELEMENT, usernametoken);

return headers[0];
}





SoapSerializationEnvelope sSerialaEnvelop = new SoapSerializationEnvelope(SoapEnvelope.VER11);
sSerialaEnvelop.dotNet = true;
sSerialaEnvelop.headerOut = new Element[1];
sSerialaEnvelop.headerOut[0] = buildAuthHeader(); //// add security request header
sSerialaEnvelop.bodyOut = sObject;
sSerialaEnvelop.setOutputSoapObject(sObject);

关于android - Ksoap2 android - 传递简单的 SOAP 头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25545976/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com