[code]
try {
// Create a URLConnection object for a URL
URL url = new URL("http://hostname:80");
URLConnection conn = url.openConnection();
// Set the cookie value to send
conn.setRequestProperty("Cookie", "name1=value1; name2=value2");
// Send the request to the server
conn.connect();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
try {
// Create a URLConnection object for a URL
URL url = new URL("http://hostname:80");
URLConnection conn = url.openConnection();
// List all the response headers from the server.
// Note: The first call to getHeaderFieldKey() will implicit send
// the HTTP request to the server.
for (int i=0; ; i++) {
String headerName = conn.getHeaderFieldKey(i);
String headerValue = conn.getHeaderField(i);
if (headerName == null && headerValue == null) {
// No more headers
break;
}
if (headerName == null) {
// The header value contains the server's HTTP version
}
}
} catch (Exception e) {
}
Here's a sample of headers from a website:
Key=Value
null=HTTP/1.1 200 OK
Server=Netscape-Enterprise/4.1
Date=Mon, 11 Feb 2002 09:23:26 GMT
Cache-control=public
Content-type=text/html
Etag="9fa67d2a-58-71-3bbdad3283"
Last-modified=Fri, 05 Oct 2001 12:53:06 GMT
Content-length=115
Accept-ranges=bytes
Connection=close
try {
// Create a URLConnection object for a URL
URL url = new URL("http://hostname:80");
URLConnection conn = url.openConnection();
// Get all cookies from the server.
// Note: The first call to getHeaderFieldKey() will implicit send
// the HTTP request to the server.
for (int i=0; ; i++) {
String headerName = conn.getHeaderFieldKey(i);
String headerValue = conn.getHeaderField(i);
if (headerName == null && headerValue == null) {
// No more headers
break;
}
if ("Set-Cookie".equalsIgnoreCase(headerName)) {
// Parse cookie
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];
}
}
}
// Save the cookie...
}
}
} catch (MalformedURLException e) {
} catch (IOException e) {
}
Here's a sample of cookies from two websites:
B=a43ka6gu6f4n4&b=2; expires=Thu, 15 Apr 2010 20:00:00 GMT;
path=/; domain=.yahoo.com
PREF=ID=e51:TM=686:LM=86:S=BL-w0; domain=.google.com; path=/;
expires=Sun, 17-Jan-2038 19:14:07 GMT
// Disable automatic redirects for all HTTP requests
HttpURLConnection.setFollowRedirects(false);
// Disable automatic redirects for a particular connection
try {
// Create a URLConnection object for a URL
URL url = new URL("http://hostname:80");
URLConnection conn = url.openConnection();
// Disable automatic redirects just for this connection
HttpURLConnection httpConn = (HttpURLConnection)conn;
httpConn.setInstanceFollowRedirects(false);
// Send the request to the server
conn.connect();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
[/code]
try {
// Create a URLConnection object for a URL
URL url = new URL("http://hostname:80");
URLConnection conn = url.openConnection();
// Set the cookie value to send
conn.setRequestProperty("Cookie", "name1=value1; name2=value2");
// Send the request to the server
conn.connect();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
try {
// Create a URLConnection object for a URL
URL url = new URL("http://hostname:80");
URLConnection conn = url.openConnection();
// List all the response headers from the server.
// Note: The first call to getHeaderFieldKey() will implicit send
// the HTTP request to the server.
for (int i=0; ; i++) {
String headerName = conn.getHeaderFieldKey(i);
String headerValue = conn.getHeaderField(i);
if (headerName == null && headerValue == null) {
// No more headers
break;
}
if (headerName == null) {
// The header value contains the server's HTTP version
}
}
} catch (Exception e) {
}
Here's a sample of headers from a website:
Key=Value
null=HTTP/1.1 200 OK
Server=Netscape-Enterprise/4.1
Date=Mon, 11 Feb 2002 09:23:26 GMT
Cache-control=public
Content-type=text/html
Etag="9fa67d2a-58-71-3bbdad3283"
Last-modified=Fri, 05 Oct 2001 12:53:06 GMT
Content-length=115
Accept-ranges=bytes
Connection=close
try {
// Create a URLConnection object for a URL
URL url = new URL("http://hostname:80");
URLConnection conn = url.openConnection();
// Get all cookies from the server.
// Note: The first call to getHeaderFieldKey() will implicit send
// the HTTP request to the server.
for (int i=0; ; i++) {
String headerName = conn.getHeaderFieldKey(i);
String headerValue = conn.getHeaderField(i);
if (headerName == null && headerValue == null) {
// No more headers
break;
}
if ("Set-Cookie".equalsIgnoreCase(headerName)) {
// Parse cookie
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];
}
}
}
// Save the cookie...
}
}
} catch (MalformedURLException e) {
} catch (IOException e) {
}
Here's a sample of cookies from two websites:
B=a43ka6gu6f4n4&b=2; expires=Thu, 15 Apr 2010 20:00:00 GMT;
path=/; domain=.yahoo.com
PREF=ID=e51:TM=686:LM=86:S=BL-w0; domain=.google.com; path=/;
expires=Sun, 17-Jan-2038 19:14:07 GMT
// Disable automatic redirects for all HTTP requests
HttpURLConnection.setFollowRedirects(false);
// Disable automatic redirects for a particular connection
try {
// Create a URLConnection object for a URL
URL url = new URL("http://hostname:80");
URLConnection conn = url.openConnection();
// Disable automatic redirects just for this connection
HttpURLConnection httpConn = (HttpURLConnection)conn;
httpConn.setInstanceFollowRedirects(false);
// Send the request to the server
conn.connect();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
[/code]