【发布时间】:2016-07-13 13:00:42
【问题描述】:
我正在尝试两种在 url 上发布的方法,除了一个字段外,一切正常。
如果我使用 HttpConnection,我将接收 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 访问相同的 url,我不会收到任何 Set-Cookie 字段,这是一种奇怪的行为。
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);
}
}
回复:
Status code: 200
Key: Date
Value: Wed, 13 Jul 2016 14:06:43 GMT
Key: Server
Value: Jetty(9.2.16.v20160414)
Key: X-Application-Context
Value: gateway:8080
Key: X-Content-Type-Options
Value: nosniff
Key: X-XSS-Protection
Value: 1; mode=block
Key: Cache-Control
Value: no-cache, no-store, max-age=0, must-revalidate
Key: Pragma
Value: no-cache
Key: Expires
Value: 0
Key: X-Frame-Options
Value: DENY
Key: Content-Type
Value: application/json; charset=UTF-8
Key: Via
Value: 1.1 localhost:8080
Key: Vary
Value: Accept-Encoding
Key: Transfer-Encoding
Value: chunked
我用来获取这个的 RestTemplate 代码:
public static <T> ResponseEntity<T> postServiceCall(String url, Class<T> responseType,
Object postBody,MultiValueMap<String, String> headers) {
HttpEntity<Object> request = new HttpEntity<Object>(postBody, headers);
ResponseEntity<T> response=restTemplate.exchange(url, HttpMethod.POST, request,responseType);
return response;
}
【问题讨论】:
-
您好,请问您找到解决方案了吗?我遇到了同样的问题
标签: java spring cookies resttemplate