2016-05-11 5 views
0

Ich habe unter Code für einen POST-Aufruf an RestAPI für Tableau-System, das funktioniert und sehen Antwort Ausgabe.Captaining LOGIN COOKIE über Rest Vorlage

Allerdings möchte ich Cookie von diesem Ausgang erfassen und muss für den weiteren Verbrauch verwendet werden! Kann mir jemand bei diesem Problem helfen?

Code:

package com.abc.it.automation.service; 

import java.io.FileInputStream; 
import java.io.IOException; 
import java.net.CookieStore; 
import java.net.HttpCookie; 
import java.net.URI; 
import java.security.KeyManagementException; 
import java.security.NoSuchAlgorithmException; 
import java.util.List; 
import java.util.Properties; 

import org.springframework.http.HttpHeaders; 
import org.springframework.http.RequestEntity.HeadersBuilder; 
import org.springframework.http.ResponseEntity; 
import org.springframework.http.client.ClientHttpResponse; 
import org.springframework.web.client.ResponseExtractor; 
import org.springframework.web.client.RestTemplate; 

import com.abc.it.automation.utils.SSLUtil; 

public class BIaaSTableauService { 



    private static Properties tableau_properties = new Properties(); 

    static { 

     // Loads the values from configuration file into the Properties instance 
     try { 
      tableau_properties.load(new FileInputStream("res/config.properties")); 
     } catch (IOException e) { 
      System.out.println(e); 
     } 
    } 

    private static final String loginURL = tableau_properties.getProperty("server.host"); 
    private static final String siteSearchURL = tableau_properties.getProperty("site.search.url"); 



    public static void main(String[] args) throws KeyManagementException, NoSuchAlgorithmException { 

     RestTemplate restTableau = new RestTemplate(); 

     String requestLogin = "<tsRequest>"+ 
          "<credentials name=\"svc_tableau\" password=\"xxxxxxxxx\" >"+ 
          "<site contentUrl=\"\"/>"+ 
          "</credentials>"+ 
          "</tsRequest>"; 
     SSLUtil.turnOffSslChecking(); 
     ResponseEntity<String> responseLogin = restTableau.postForEntity(loginURL, requestLogin, String.class); 
     System.out.println(responseLogin.getBody()); 

Antwort

0

Sie benötigen RestTemplate zu bauen, wie folgt.

RestTemplate restTableau = new RestTemplate(new MyClientHttpRequestFactory()); 

Erweitern Sie ClientHttpRequestFactory wie folgt.

public class MyClientHttpRequestFactory extends SimpleClientHttpRequestFactory { 


private Cookie cookie; 
//setters and getters. 

@Override 
protected void prepareConnection(HttpURLConnection connection, String httpMethod) { 
this.setCookie(connection.getRequestProperty("Cookie")); 

} 
}