2015-09-15 23 views
0

我已經使用SOAPConnection來調用服務,問題是Web服務返回一個cookie作爲響應,我如何保存該cookie以進一步調用?如何保存來自Web服務的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()); 

回答

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); 
相關問題