发送cookie

[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]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值