利用httpclient访问受保护的资源地址

模拟登录
1、session方式
package com.crawinfo.httpclient;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class HttpClientTest {

/**
* @param args
* @throws IOException
* @throws HttpException
*/

public static void main(String[] args) throws HttpException, IOException {

HttpClient httpclient=new HttpClient();
//创建一个客户端,类似打开一个浏览器

GetMethod getMethod=new GetMethod(
"受保护的地址");

PostMethod postMethod = new PostMethod(
"登录url");  
NameValuePair[] postData = new NameValuePair[2];  
postData[0] = new NameValuePair(
"loginName", "***");  
postData[1] = new NameValuePair(
"loginPswd", "**");  
postMethod.addParameters(postData);  

int statusCode=httpclient.executeMethod(postMethod);
//回车——出拳!  

statusCode= httpclient.executeMethod(getMethod);

System.out.println(
"response1=" + postMethod.getResponseBodyAsString());//察看拳头命中情况,可以获得的东西还有很多,比如head, cookies等等  

System.out.println(
"response2=" + getMethod.getResponseBodyAsString());//察看拳头命中情况,可以获得的东西还有很多,比如head, cookies等等  

getMethod.releaseConnection();
postMethod.releaseConnection();
//释放,记得收拳哦  



}

}
二、cookie方式
package com.crawinfo.rencai;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;

public class CrawTest {
static Cookie[] cookies=new Cookie[2];

public static void login() throws Exception {
cookies[0] = new Cookie(
"抓取信息网址", "SessionId",
"0", "/", -1, false);
cookies[1] = new Cookie(
"抓取信息网址", "otherproperty",
"xxx", "/", -1, false);

}

public static String getRes(String path)throws Exception{
String res=null;
HttpClient client=new HttpClient();
HttpMethod method=new GetMethod(path);
client.getState().addCookies(cookies);
client.executeMethod(method);
if(method.getStatusCode()==200){
res=method.getResponseBodyAsString();
cookies=client.getState().getCookies();
}
method.releaseConnection();
return res;
}

public static void main(String[] args) throws Exception {

CrawTest.login();

String info = CrawTest.getRes(
"抓取信息网址");
System.out.println(
"info="+info);
}

}

lunzi   2008-07-23 10:08:26 评论:2   阅读:2541   引用:0
无题 @2008-07-23 17:47:22  meiking
PHP里可以用socket直接连服务器的80端口,然后写请求数据,不知道java里面有没有这种方法?估计java里没有php里没有的方法吧。
无题 @2008-07-23 11:50:47  hofman
不错,自动点击更新搜索索引也可用它了,虽然也可用别的办法。

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

Copyright@2004-2010 powered by YuLog