/*
* Created on 2005-9-22
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.softbrain.wangzl.net.http;import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;/**
* @author wangzl
*
* @MSN [email protected]
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class SendCookie { public static void main(String[] args) {
try {
// Create a URLConnection object for a URL
URL url = new URL("http://community.csdn.net/expert/forum.asp");
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) {
} }
}
* Created on 2005-9-22
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.softbrain.wangzl.net.http;import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;/**
* @author wangzl
*
* @MSN [email protected]
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class SendCookie { public static void main(String[] args) {
try {
// Create a URLConnection object for a URL
URL url = new URL("http://community.csdn.net/expert/forum.asp");
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) {
} }
}
解决方案 »
- 精通Hibernate Validator的请进
- MyEclipse连接SQLServer2008的URL
- 兄弟们.....头大了..地址栏LOGO...
- 关于三级联动菜单的问题,盼高手出现!
- 帮我解决一下分页路劲问题……
- java技术:在Swing中展现报表
- 求助:如何在页面上实现选择本地路径的控件?
- JSP访问JAVABEAN中的私有变量难道有错吗?
- 我想要一个现成的论坛代码,数据库是用oracle的
- IIS + Resin 环境下的一个JSP运行问题!
- 启动不了tomcat
- for JDBC][SQLServer]用户 'sa' 登录失败。原因: 未与信任 SQL Server 连接相关联 就十分了全给了 源程序肯定没错 以前用过
* Created on 2005-9-22
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.softbrain.wangzl.net.http;import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;/**
* @author wangzl
*
* @MSN [email protected]
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class GetCookie { public static void main(String[] args) {
try {
// Create a URLConnection object for a URL
URL url = new URL("http://community.csdn.net/expert/forum.asp");
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];
System.out.print(cookieValue + "\n");
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];
System.out.print(expires + "\n");
} else if ("domain".equalsIgnoreCase(f[0])) {
domain = f[1];
System.out.print(domain + "\n");
} else if ("path".equalsIgnoreCase(f[0])) {
path = f[1];
System.out.print(path + "\n");
}
}
}
// Save the cookie...
}
}
} catch (MalformedURLException e) {
} catch (IOException e) {
} }
}