怎样在java代码中登陆一个网站? 怎样才能在java代码中登陆一个网站,然后获得该网站的内容?我知道用java.net.URL可以获得网站的内容,但是当一个网站需要登陆以后才能查看的话我应该怎样做到呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 (1)java里保存cookie(2)有个开源项目:httpClient cookie的方法类似于下面:这个能否使用依赖于服务器的实现。 URL url = new URL("http://hostname:80"); URLConnection conn = url.openConnection(); conn.setRequestProperty("Cookie", "name1=value1; name2=value2"); conn.connect();建议看看httpClient。 上面没说清楚。用cookie方法(1)模拟登陆,获取cookies URL url = new URL("http://hostname:80"); URLConnection conn = url.openConnection(); for (int i=0; ; i++) { String headerName = conn.getHeaderFieldKey(i); String headerValue = conn.getHeaderField(i); if (headerName == null && headerValue == null) { break; } if ("Set-Cookie".equalsIgnoreCase(headerName)) { String[] fields = headerValue.split(";\\s*"); String cookieValue = fields[0]; String expires = null; String path = null; String domain = null; boolean secure = false; 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]; } } } } }(2)取得cookies之后,请求新页面都带上cookie,就是上面回复的那个例子。就可以了。具体代码自己看着办吧,原理是这样 可能楼主的意思是用session而不是cookie 为什么有的输出流需要flush,有的却不需要? java里 alt的ascall码是什么啊 急求 如何用递归替代n 层循环,在线等求助! 如何学习JAVA int转String类型遇到的问题 java中比较快捷的实现日期格式化的函数是什么? ???關於STRUTS MULTIBOX怎麼使用?有誰知道嗎?幫幫忙啦 Applet的问题?到家门了就进来看看吧,发言者有分~~~~ 一个简单问题,给分! 怎么归档自己的包! 求jdk1.5.0_08 JDK安装检测问题
(2)有个开源项目:httpClient
URL url = new URL("http://hostname:80");
URLConnection conn = url.openConnection();
conn.setRequestProperty("Cookie", "name1=value1; name2=value2");
conn.connect();建议看看httpClient。
用cookie方法
(1)模拟登陆,获取cookies
URL url = new URL("http://hostname:80");
URLConnection conn = url.openConnection(); for (int i=0; ; i++) {
String headerName = conn.getHeaderFieldKey(i);
String headerValue = conn.getHeaderField(i);
if (headerName == null && headerValue == null) {
break;
}
if ("Set-Cookie".equalsIgnoreCase(headerName)) {
String[] fields = headerValue.split(";\\s*");
String cookieValue = fields[0];
String expires = null;
String path = null;
String domain = null;
boolean secure = false;
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];
}
}
} }
}(2)取得cookies之后,请求新页面都带上cookie,就是上面回复的那个例子。就可以了。
具体代码自己看着办吧,原理是这样