使用saaj在soap标头中添加会话ID

时间:2014-01-15 09:59:31

标签: java soap saaj

我使用的是使用jax ws的Php soap服务器和java客户端。 如何在后续请求中在soap标头中添加sessionId? 它的格式是什么?

到目前为止,我做到了这一点:

String url = "http://localhost:8888/server/soap/server.php";

    // login

MessageFactory mf = MessageFactory.newInstance();
SOAPMessage request = mf.createMessage();
SOAPPart soapPart = request.getSOAPPart();
MimeHeaders headers = request.getMimeHeaders();
headers.addHeader("SOAPAction", url);              

SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("var", url);        

SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("call", "var");

SOAPElement action = soapBodyElem.addChildElement("action", "var");
action.addTextNode("LOGON");        

SOAPElement params = soapBodyElem.addChildElement("params", "var");       


SOAPElement uid = soapBodyElem.addChildElement("uid ", "var");
uid .addTextNode("tester");

SOAPElement password = soapBodyElem.addChildElement("password ", "var");
password .addTextNode("");

params.addChildElement(uid );
params.addChildElement(password);

request.saveChanges();

    /* Print the request message */
    System.out.print("Request SOAP Message:");
    request.writeTo(System.out);
    System.out.println();

    SOAPMessage response = soapConnection.call(request, url);

上述请求有效,因为它处于登录阶段,即将获得会话ID。但后来的要求呢?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:

获取您的登录响应的cookie并在下一个请求中设置它 cookie内容如下所示:

PHPSESSID=sqv184d99ndodebh4te9lmpji5; path=/

这是代码:

 MimeHeaders session = response.getMimeHeaders();     
 String []cookies = session.getHeader("Set-Cookie");             
 request.getMimeHeaders().addHeader("Cookie", cookies[0]);