使用httpclient下载需要登录的网页
2007-04-07 18:08
555 查看
import java.io.IOException;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.cookie.*;
import org.apache.commons.httpclient.methods.*;
public class JClientLoginTest {
public static void main(String[] args) throws HttpException, IOException {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost( "bbs.cpcw.com" , 80, "http" );
PostMethod post = new PostMethod( "/logging.php?action=login" );
NameValuePair name = new NameValuePair( "username" , "dongle2001" );
NameValuePair pass = new NameValuePair( "password" , "1a9i8h1a" );
NameValuePair quest = new NameValuePair( "questionid" , "0" );
NameValuePair answer = new NameValuePair( "answer" , "" );
NameValuePair cookietime = new NameValuePair( "cookietime" , "315360000" );
NameValuePair submit = new NameValuePair( "loginsubmit" , "提 交" );
post.setRequestBody( new NameValuePair[]{name,pass,quest,answer,cookietime,submit});
int status = client.executeMethod(post);
post.releaseConnection();
CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
Cookie[] cookies = cookiespec.match("bbs.cpcw.com", 80, "/" , false , client.getState().getCookies());
if (cookies.length == 0) {
System.out.println( "None" );
} else {
for ( int i = 0; i < cookies.length; i++) {
System.out.println(cookies[i].toString());
}
}
GetMethod get=new GetMethod("/viewthread.php?tid=883329");
client.executeMethod(get);
String response = new String(get.getResponseBodyAsString().getBytes("8859-1"));
System.out.println(response);
get.releaseConnection();
}
}
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.cookie.*;
import org.apache.commons.httpclient.methods.*;
public class JClientLoginTest {
public static void main(String[] args) throws HttpException, IOException {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost( "bbs.cpcw.com" , 80, "http" );
PostMethod post = new PostMethod( "/logging.php?action=login" );
NameValuePair name = new NameValuePair( "username" , "dongle2001" );
NameValuePair pass = new NameValuePair( "password" , "1a9i8h1a" );
NameValuePair quest = new NameValuePair( "questionid" , "0" );
NameValuePair answer = new NameValuePair( "answer" , "" );
NameValuePair cookietime = new NameValuePair( "cookietime" , "315360000" );
NameValuePair submit = new NameValuePair( "loginsubmit" , "提 交" );
post.setRequestBody( new NameValuePair[]{name,pass,quest,answer,cookietime,submit});
int status = client.executeMethod(post);
post.releaseConnection();
CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
Cookie[] cookies = cookiespec.match("bbs.cpcw.com", 80, "/" , false , client.getState().getCookies());
if (cookies.length == 0) {
System.out.println( "None" );
} else {
for ( int i = 0; i < cookies.length; i++) {
System.out.println(cookies[i].toString());
}
}
GetMethod get=new GetMethod("/viewthread.php?tid=883329");
client.executeMethod(get);
String response = new String(get.getResponseBodyAsString().getBytes("8859-1"));
System.out.println(response);
get.releaseConnection();
}
}
相关文章推荐
- 使用httpclient下载需要登录的网页续
- 接口测试入门(2)--get和post初级请求/使用httpclient做一个获取信息list的请求(需要登录才可以)
- 使用httpClient下载网页
- 【搜索引擎Jediael开发笔记2】使用HttpClient下载网页至本地文件
- 【搜索引擎Jediael开发笔记2】使用HttpClient下载网页至本地文件
- 使用httpclient jsoup 及jetty 全自动登录网站 抓取网页,解析并展示
- 使用HttpClient下载网页
- 【搜索引擎Jediael开发笔记2】使用HttpClient下载网页至本地文件 分类: C_OHTERS 2014-05-19 15:07 1108人阅读 评论(0) 收藏
- 【搜索引擎Jediael开发笔记2】使用HttpClient下载网页至本地文件
- 使用Servlet制作简单登录验证,response下载文件与网页跳转
- 【搜索引擎Jediael开发笔记2】使用HttpClient下载网页至本地文件
- 无可救药的拖延症——关于asp网页使用Access及大图浏览效果以及爬虫登录及下载文件
- 【搜索引擎Jediael开发笔记2】使用HttpClient下载网页至本地文件
- 使用HttpClient模拟登录 博客园
- 采集需要登录后的网页(重定向后cookie丢失问题)
- php中使用gd库实现下载网页中所有图片
- php中使用gd库实现下载网页中所有图片
- Facebook登录-前端网页以及在angular中的使用方法
- 夺命雷公狗---微信开发19----使用网页调试工具调试该接口进行文件的上传与下载
- 使用BHO技术检测IE在浏览网页时下载到IE cache中的文件