我在java中创建一个Soap客户端,我遇到了一个奇怪的错误。
抽象客户
heapq
肥皂客户端实施
app:layout_behavior="@string/appbar_scrolling_view_behavior"
奇怪的一点是代码的这一部分:
public abstract class AbstractSoapClient {
private ServerContext context;
private String path;
private static final String WSSE = "";
private static final String CURL = "";
private static final String CURL_PASSWORD = "";
private static final String SECURITY_NODE = "";
private static final String USERNAME_TOKEN = "";
private static final String USERNAME_NODE = "";
private static final String PASSWORD_NODE = "";
public AbstractSoapClient(ServerContext context) {
this.context = context;
}
protected SOAPMessage createRequest(String path) throws SOAPException {
this.path = assembleEndpoint(path);
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), this.path);
soapConnection.close();
return soapResponse;
}
protected void setCredentials(SOAPEnvelope envelope) throws SOAPException {
SOAPHeader tHeader = envelope.getHeader();
Name tWsseHeaderName = envelope.createName(SECURITY_NODE, WSSE, CURL);
SOAPHeaderElement tSecurityElement = tHeader.addHeaderElement(tWsseHeaderName);
tSecurityElement.setMustUnderstand(false);
Name tUserTokenElementName = envelope.createName(USERNAME_TOKEN, WSSE, CURL);
SOAPElement tUserTokenElement = tSecurityElement.addChildElement(tUserTokenElementName);
tUserTokenElement.removeNamespaceDeclaration(WSSE);
tUserTokenElement.addNamespaceDeclaration("wsu", CURL);
// user name child
Name tUsernameElementName = envelope.createName(USERNAME_NODE, WSSE, CURL);
SOAPElement tUsernameElement = tUserTokenElement.addChildElement(tUsernameElementName);
tUsernameElement.removeNamespaceDeclaration(WSSE);
tUsernameElement.addTextNode(context.getUsername());
// password child
Name tPasswordElementName = envelope.createName(PASSWORD_NODE, WSSE, CURL);
SOAPElement tPasswordElement = tUserTokenElement.addChildElement(tPasswordElementName);
tPasswordElement.removeNamespaceDeclaration(WSSE);
tPasswordElement.setAttribute("Type", CURL_PASSWORD);
tPasswordElement.addTextNode(context.getPassword());
}
private String assembleEndpoint(String path) {
return context.getUrl().concat(path);
}
protected abstract SOAPMessage createSOAPRequest() throws SOAPException;
public ServerContext getContext() {
return context;
}
public String getPath() {
return path;
}
}
如果我评论此代码仅在发送之前打印请求,我会得到下一个异常:
public class SoapClient extends AbstractSoapClient {
public SoapClient(ServerContext context) {
super(context);
}
@Override
public SOAPMessage createSOAPRequest() throws SOAPException {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
setCredentials(envelope);
buildBody(envelope);
soapMessage.saveChanges();
try {
soapMessage.writeTo(System.out);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return soapMessage;
}
private void buildBody(SOAPEnvelope envelope) throws SOAPException {
envelope.addNamespaceDeclaration("sch", "------");
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("sampleData", "sampleData");
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("sampleData");
soapBodyElem1.addTextNode("sampleData");
SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("sampleData");
soapBodyElem2.addTextNode("sampleData");
SOAPElement soapBodyElem3 = soapBodyElem.addChildElement("sampleData");
soapBodyElem3.addTextNode("Y");
SOAPElement soapBodyElem4 = soapBodyElem.addChildElement("sampleData");
soapBodyElem4.addTextNode("sampleData");
SOAPElement soapBodyElem5 = soapBodyElem.addChildElement("sampleData");
soapBodyElem5.addTextNode("sampleData");
SOAPElement soapBodyElem6 = soapBodyElem.addChildElement("sampleData");
soapBodyElem6.addTextNode("sampleData");
}
public static void main(String[] args) throws SOAPException, IOException {
SoapClient client = new SoapClient(
new ServerContext("url", "user", "password"));
SOAPMessage response = client.createRequest("endpoint");
response.writeTo(System.out);
}
}
但如果我不评论这一行,我可以正确得到答案,对我来说这些没有意义,因为如果我不写请求,为什么发送 404 Notot Found 在发送它之前在控制台中。
答案 0 :(得分:0)
默认情况下,SOAPMessage接口由SoapMessageImpl实现。 如果不存在,则此实现具有添加SOAPAction标头的副作用。
在调用writeTo之后,您可以通过调用:
将其删除soapMessage.getMimeHeaders().removeHeader("SOAPAction");
话虽如此,我建议使用代理,而不是仅添加额外的代码来记录调用和响应。
如果您正在使用Eclipse,请查看TCP/Monitor View
答案 1 :(得分:0)
标识正确的“ SOAPAction”,然后发布请求。这将解决404 FileNotFoud错误。 例如:
String SOAPAction = "/Service/Service.serviceagent/ServicesEndpoint1/ACTIONNAMEFROMSERVERSIDE";
httpConn.setRequestProperty("Content-Length", String.valueOf(b.length));
httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
httpConn.setRequestProperty("SOAPAction", SOAPAction);