无法使用RestTemplate接收Set-Cookie字段

时间:2016-07-13 13:00:42

标签: java spring cookies resttemplate

我正在尝试两种方法在网址上发布POST,除了在一个字段中,一切正常。

如果我正在使用HttpConnection,我将收到Set-Cookie字段作为输出。

create or replace package body package_name is

  var_name number;

  procedure proc_name is
  begin
    do_smth;
  end;

begin
   init_smth;
end ;

这是输出中的一个字段:

输出:

create or replace package body package_name is

  var_name number;

begin
   init_smth;
end ;

  var_name2 number;

  procedure proc_name is
  begin
...

但是如果我使用RestTemplate来敲击相同的url,我没有收到任何Set-Cookie字段,这是一种奇怪的行为。

    JSONObject json = new JSONObject();
    json.put("username", "1010101010");
    json.put("password", "11two33");
    String loginContent = json.toString();
    int timeOut = 100000;
    String authLoginUrl = "http://localhost:8080/api/login";

    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Accept", "application/json");
    headers.put("Content-Type", "application/json");

    HttpExternalService httpService = new HttpExternalService();
    HttpExResponseDetails exResponse = httpService.sendRequest(authLoginUrl, loginContent, HttpMethod.POST, timeOut,
            headers, null, null, true, true, true, false, null);

    Map<String, List<String>> rsHeaders = exResponse.getResponseHeaderMap();
    for(Entry<String, List<String>> e: rsHeaders.entrySet()){
        System.out.println("Key: "+e.getKey());
        List<String> valueList = e.getValue();
        for(String str: valueList){
            System.out.println("Value: "+str);
        }
    }

回应:

Key: Transfer-Encoding
Value: chunked
Key: null
Value: HTTP/1.1 200 OK
Key: Server
Value: Jetty(9.2.16.v20160414)
Key: X-Content-Type-Options
Value: nosniff
Key: Pragma
Value: no-cache
Key: X-Application-Context
Value: gateway:8080
Key: Date
Value: Wed, 13 Jul 2016 14:08:55 GMT
Key: Via
Value: 1.1 d.eze.cc
Key: X-Frame-Options
Value: DENY
Key: Cache-Control
Value: no-cache, no-store, max-age=0, must-revalidate
Key: Vary
Value: Accept-Encoding
Key: Set-Cookie
Value: jsessionid=c5bcc245-e18e-4320-8ac2-08b3e51dcae7;Path=/api/;HttpOnly
Key: Expires
Value: Thu, 01 Jan 1970 00:00:00 GMT
Key: X-XSS-Protection
Value: 1; mode=block
Key: Content-Type
Value: application/json; charset=UTF-8

我用来获取此信息的RestTemplate代码:

    Map<String, String> json = new HashMap<String, String>();
    json.put("username", "1010101010");
    json.put("password", "11two33");

    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Accept", "application/json");
    headers.put("Content-Type", "application/json");

    ResponseEntity<ApiOutput> out = WebServiceCaller.postServiceCall(url, ApiOutput.class, json, null);
    // HttpExResponseDetails exResponse =
    // httpService.sendRequest(authLoginUrl, loginContent, HttpMethod.POST,
    // timeOut,
    // getHeaders(), null, null, true, false, false, false, null);
    System.out.println("Status code: " + out.getStatusCode());
    MultiValueMap<String, String> rsHeaders = out.getHeaders();

    for (Entry<String, List<String>> e : rsHeaders.entrySet()) {
        System.out.println("Key: " + e.getKey());
        List<String> valueList = e.getValue();
        for (String str : valueList) {
            System.out.println("Value: " + str);
        }
    }

1 个答案:

答案 0 :(得分:0)

我不确定WebServiceCaller是做什么的,但是RestTemplate#exchange应该返回所有标头。