php curl模拟登录支付宝,HttpClient使用之模拟登录人人网(Post请求)

现阶段在学习网络爬虫,下面是一个简单登录人人网,跳转到我们想要看日志页面的代码。

Java实现如下:

package com.rumo.httpclient;

import java.util.ArrayList;

import java.util.List;

import org.apache.http.Header;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.NameValuePair;

import org.apache.http.client.ResponseHandler;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.BasicResponseHandler;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.protocol.HTTP;

public class LoginRenren {

private static String USER_NAME = "用户名";

private static String PASS_WORD = "你的密码";

private static String RED_URL = "http://blog.renren.com/blog/304317577/449470467";

private static String LOGIN_URL = "http://www.renren.com/PLogin.do";

// 用于取得重定向地址

private HttpResponse response;

// 在一个会话中用到的 httpClient 对象

private DefaultHttpClient httpClient = new DefaultHttpClient();

public static void main(String[] args) {

LoginRenren renren = new LoginRenren();

renren.printText();

}

/**

* 登录到页面

* @return

*/

private boolean login(){

boolean result = false;

HttpPost httpPost = new HttpPost(LOGIN_URL);

List parameters = new ArrayList();

parameters.add(new BasicNameValuePair("origURL", RED_URL));

parameters.add(new BasicNameValuePair("domain", "renren.com"));

parameters.add(new BasicNameValuePair("isplogin", "true"));

parameters.add(new BasicNameValuePair("formName", ""));

parameters.add(new BasicNameValuePair("method", ""));

parameters.add(new BasicNameValuePair("submit", "登录"));

parameters.add(new BasicNameValuePair("email", USER_NAME));

parameters.add(new BasicNameValuePair("password", PASS_WORD));

try {

HttpEntity entity = new UrlEncodedFormEntity(parameters,HTTP.UTF_8);

httpPost.setEntity(entity);

response = httpClient.execute(httpPost);

} catch (Exception e) {

e.printStackTrace();

result = false;

return result;

} finally {

httpPost.abort();

}

result = true;

return result;

}

/**

* 取得重定向地址

* @return

*/

private String getRedirectLocation(){

String result = "";

Header locationHeader = response.getFirstHeader("Location");

if(locationHeader==null){

return null;

}

result = locationHeader.getValue();

return result;

}

/**

* 根据重定向地址返回内容

* @param redirectLocation

* @return

*/

private String getText(String redirectLocation){

HttpGet httpGet = new HttpGet(redirectLocation);

ResponseHandler responseHandler = new BasicResponseHandler();

String responseBody = "";

try {

responseBody = httpClient.execute(httpGet, responseHandler);

} catch (Exception e) {

e.printStackTrace();

responseBody = null;

return responseBody;

} finally {

httpGet.abort();

httpClient.getConnectionManager().shutdown();// 关闭连接

}

return responseBody;

}

public void printText(){

if(login()){

String redirectLocation = getRedirectLocation();

if(redirectLocation!=null){

System.out.println(getText(redirectLocation));

}else{

System.out.println("获取redirectLocation失败!");

}

}else{

System.out.println("登录失败!");

}

}

}

执行结果:可以看到,已经成功登录跳转到了我们想访问的页面。

ffc4a63e43116087334ad8774efb1f34.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值