博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java通过httpclient获取cookie模拟登录(网络爬虫/HTTP)
阅读量:2385 次
发布时间:2019-05-10

本文共 2177 字,大约阅读时间需要 7 分钟。

package 
Step1;
 
import 
org.apache.commons.httpclient.Cookie;
import 
org.apache.commons.httpclient.HttpClient;
import 
org.apache.commons.httpclient.NameValuePair;
import 
org.apache.commons.httpclient.cookie.CookiePolicy;
import 
org.apache.commons.httpclient.methods.GetMethod;
import 
org.apache.commons.httpclient.methods.PostMethod;
/**
 
*
    
* @ClassName: HttpLogin
    
* @Description:  java通过httpclient获取cookie模拟登录
    
* @author zeze
    
* @date 2015年11月10日 下午4:18:08
    
*
 
*/
 
public 
class 
HttpLogin {
 
    
public 
static 
void 
main(String[] args) {
        
// 登陆 Url
        
String loginUrl = 
"http://passport.mop.com/?targetUrl=http://hi.mop.com/?&g=1447141423230&loginCheck=UNLOGINED"
;
        
// 需登陆后访问的 Url
        
String dataUrl = 
"http://hi.mop.com/?"
;
        
HttpClient httpClient = 
new 
HttpClient();
 
        
// 模拟登陆,按实际服务器端要求选用 Post 或 Get 请求方式
        
PostMethod postMethod = 
new 
PostMethod(loginUrl);
 
        
// 设置登陆时要求的信息,用户名和密码
        
NameValuePair[] data = { 
new 
NameValuePair(
"loginName"
"chzeze123"
), 
new 
NameValuePair(
"loginPasswd"
"**"
) };
        
postMethod.setRequestBody(data);
        
try 
{
            
// 设置 HttpClient 接收 Cookie,用与浏览器一样的策略
            
httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
            
int 
statusCode=httpClient.executeMethod(postMethod);
                             
            
// 获得登陆后的 Cookie
            
Cookie[] cookies = httpClient.getState().getCookies();
            
StringBuffer tmpcookies = 
new 
StringBuffer();
            
for 
(Cookie c : cookies) {
                
tmpcookies.append(c.toString() + 
";"
);
                
System.out.println(
"cookies = "
+c.toString());
            
}
            
if
(statusCode==
302
){
//重定向到新的URL
                
System.out.println(
"模拟登录成功"
);
                
// 进行登陆后的操作
                
GetMethod getMethod = 
new 
GetMethod(dataUrl);
                
// 每次访问需授权的网址时需带上前面的 cookie 作为通行证
                
getMethod.setRequestHeader(
"cookie"
, tmpcookies.toString());
                
// 你还可以通过 PostMethod/GetMethod 设置更多的请求后数据
                
// 例如,referer 从哪里来的,UA 像搜索引擎都会表名自己是谁,无良搜索引擎除外
                
postMethod.setRequestHeader(
"Referer"
"http://passport.mop.com/"
);
                
postMethod.setRequestHeader(
"User-Agent"
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36"
);
                
httpClient.executeMethod(getMethod);
                
// 打印出返回数据,检验一下是否成功
                
String text = getMethod.getResponseBodyAsString();
                
System.out.println(text);
            
}
            
else 
{
                
System.out.println(
"登录失败"
);
            
}
        
}
        
catch 
(Exception e) {
            
e.printStackTrace();
        
}
    
}
}

转载地址:http://zhjab.baihongyu.com/

你可能感兴趣的文章
两个linux性能测试程序LMbench和SysBench
查看>>
rsa asia 2015 ppt
查看>>
nikto 2.1 coming
查看>>
How to own a Windows Domain
查看>>
Longcat – multi-protocol stress testing tool
查看>>
数据流0day原理+实践
查看>>
淺談以STIX實現網路威脅情報標準化框架
查看>>
Top IT management trends - the next 5 years
查看>>
推荐 OWASP - Transport Layer Protection Cheat Sheet
查看>>
AutoNessus v1.3.2 released
查看>>
hack tools
查看>>
rhel5中管理swap空间
查看>>
/proc filesystem allows bypassing directory permissions on Linux
查看>>
nginx dos
查看>>
RASP解决方案包括开源方案
查看>>
Linux下共享文件系统文件传输的简单设计(转载)
查看>>
Benchmarking Mail Relays and Forwarders
查看>>
Yokoso! – Web Infrastructure Fingerprinting & Delivery Tool
查看>>
WEB安全工具大收集
查看>>
Username Enumeration Vulnerabilities
查看>>