我正在开发一个带有SOAP WS的Android应用程序,当我从请求中获取属性时它工作正常,但是当我将SAML令牌添加到Header时,服务器每次都返回相同的错误。
Error Reading XMLStreamReader
发送标题元素时的WS请求:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wss="...URL....">
<soapenv:Header>
<wsse:Security .....>
<saml:Assertion ID=""........></Assertion>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<wss:consultaUnitats>
<!--Optional:-->
<identificadorMissatge></identificadorMissatge>
<data></data>
</wss:consultaUnitats>
</soapenv:Body>
</soapenv:Envelope>
我的Java代码:
private boolean doUnits(String tokenID, String data) throws IOException {
request = new SoapObject(NAMESPACE, METHOD_NAME); request.addProperty("identificadorMissatge", ""); //Optional
request.addProperty("data", data);
envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.headerOut = new Element[1];
envelope.headerOut[0] = getHeader();
envelope.setAddAdornments(false);
envelope.implicitTypes = true;
envelope.setOutputSoapObject(request);
androidHttpsTransport = new HttpTransportSE(URL);
androidHttpsTransport.debug = true;
try {
androidHttpsTransport.call(SOAP_ACTION, envelope);
Log.i("ENVELOPE", androidHttpsTransport.requestDump);
Log.i("ENVELOPE", androidHttpsTransport.responseDump);
if (envelope.bodyIn instanceof SoapFault) {
String str = ((SoapFault) envelope.bodyIn).faultstring;
Log.i("", str);
} else {
response = (SoapObject) envelope.bodyIn;
Log.d("WS", String.valueOf(response));
}
return result;
}
private Element getHeader() {
Element header = new Element().createElement("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "wsse:Security");
header.addChild(Node.TEXT, tokenID);
return header;
}
我的问题:
也许,我在创建新元素时需要添加Element而不是TEXT? * tokenID是一个String,我在里面添加了SAML。这是问题吗?
答案 0 :(得分:0)
最后我修好并完美地工作。
我为另一个人分享我的代码有同样的问题。
如果将SAML代码放在String中,则需要解析并重新解析为Document。 怎么样?这是我的代码:
private Element getHeader() throws XmlPullParserException, IOException {
// First Parse
XmlPullParserFactory factory = null;
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput((Reader)new StringReader(tokenID));
// Second Parse and create a Document
Document doc = new Document();
doc.parse(xpp);
//Create Element and add Child
Element header = new Element().createElement("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security");
header.addChild(Node.ELEMENT, doc.getChild(0));
return header;
}
将标题添加到Envelope.headerOut:
envelope.headerOut = new Element[1];
envelope.headerOut[0] = getHeader();
我希望这段代码可以帮助其他开发人员。