Кто ел мои печенья? Они не отображаются ни в заголовках, ни в cookies store. Я пытаюсь прочитать куки с http ответа
Я пытаюсь добраться до куки из http ответа. Но я получаю пустой вывод. Я проверил с помощью fiddler, ответ http содержит заголовок set-cookie. Но похоже, что Cookies нет в магазине cookie. Очевидно, я иду куда-нибудь. Где мне нужно исправить код? Пожалуйста, помогите мне.
public class Main {
public static void main(String[] args) throws Exception {
URL url = new URL("http://www.angelvestgroup.com/info.php?id=1");
CookieManager manager = new CookieManager();
manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(manager);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
Map<String, List<String>> headerFields = conn.getHeaderFields();
Set<String> headerFieldsSet = headerFields.keySet();
Iterator<String> hearerFieldsIter = headerFieldsSet.iterator();
while (hearerFieldsIter.hasNext()) {
String headerFieldKey = hearerFieldsIter.next();
if ("Set-Cookie".equalsIgnoreCase(headerFieldKey)) {
List<String> headerFieldValue=headerFields.get(headerFieldKey);
for (String headerValue : headerFieldValue) {
System.out.println("Cookie Found...");
String[] fields = headerValue.split(";\\s*");
String cookieValue = fields[0];
String expires = null;
String path = null;
String domain = null;
boolean secure = false;
// Parse each field
for (int j = 1; j < fields.length; j++) {
if ("secure".equalsIgnoreCase(fields[j])) {
secure = true;
}
else if (fields[j].indexOf('=') > 0) {
String[] f = fields[j].split("=");
if ("expires".equalsIgnoreCase(f[0])) {
expires = f[1];
}
else if ("domain".equalsIgnoreCase(f[0])) {
domain = f[1];
}
else if ("path".equalsIgnoreCase(f[0])) {
path = f[1];
}
}
}
System.out.println("cookieValue:" + cookieValue);
System.out.println("expires:" + expires);
System.out.println("path:" + path);
System.out.println("domain:" + domain);
System.out.println("secure:" + secure);
System.out.println("*****************************************");
CookieStore cookieJar = manager.getCookieStore();
List <HttpCookie> cookies =
cookieJar.get(url.toURI());
for (HttpCookie cookie: cookies) {
System.out.println("CookieHandler retrieved cookie: " + cookie);
}
}
}
}
}
}
1 ответ
Мой первый вопрос в Stack оказался ошибкой в используемой версии Java. http://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8160038