利用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);
}
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值