ずばり、J2SE のドキュメントのページ「
cookie サポート」が詳しい。以下のコードを参照のこと。
String protocol = "http";
String hostname = "yourdomain.com";
int port = 80;
String file = "/";
try {
URL url = new URL(protocol, hostname, port, file);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Input OK
conn.setDoOutput(true); // Output OK
conn.setUseCaches(false); // Cache NG
conn.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");
conn.connect(); // Connect
boolean isRedirect = HttpURLConnection.getFollowRedirects();
String cookieValue = null;
try {
Map<String, List<String>> headers = conn.getHeaderFields();
List<String> values = headers.get("Set-Cookie");
for (Iterator<String> iter = values.iterator(); iter.hasNext();) {
String v = iter.next();
if (cookieValue == null)
cookieValue = v;
else
cookieValue = cookieValue + ";" + v;
}
} catch (Exception e) {
}
System.out.println(this.getClass().getName()
+ ": cookieValue: " + cookieValue);
conn.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
トラックバック URL:
https://perltips.twinkle.cc/trackback/92