如何从Web服务中保存cookie响应?

时间:2015-09-15 05:48:00

标签: java cookies

我使用SOAPConnection来调用服务,问题是Web服务返回一个cookie作为响应,如何保留该cookie以进行进一步的调用?

SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
        SOAPConnection connection = sfc.createConnection();
        String text = new String(
                Files.readAllBytes(Paths.get("C:\\Users\\abhinav.malviya\\workspace\\TestWeb\\src\\SOAP.xml")),
                StandardCharsets.UTF_8);
        SOAPMessage sm = getSoapMessageFromString(text);

        URL endpoint = new URL("http://URL/ProSightWS/psPortfoliosSecurity.asmx");
        SOAPMessage response = connection.call(sm, endpoint);

        printMessage(response);

        System.out.println(response.getContentDescription());

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码创建Cookie:

 BindingProvider bindingProvider = (BindingProvider) Port;
     Map<String,Object> requestContext = bindingProvider.getRequestContext();
     Map<String, List<String>> requestHeaders = new HashMap<String, List<String>>();
     List<String> cookies = new ArrayList<String>();
     cookies.add("SMSESSION=testCookie");
     requestHeaders.put("Cookie", cookies);
     requestContext.put(MessageContext.HTTP_REQUEST_HEADERS, requestHeaders);