51job java模拟登陆_模拟登录51job个人中心

1.[代码]httpclient4.x

package my.importer.impl;

import goni.util.StringUtil;

import java.util.ArrayList;

import java.util.List;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import org.apache.http.Header;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.HttpStatus;

import org.apache.http.NameValuePair;

import org.apache.http.client.HttpClient;

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.DefaultHttpClient;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.params.CoreProtocolPNames;

import org.apache.http.util.EntityUtils;

/**

*

* Test51JobLogin.java Create on 2014-7-17 上午10:05:07

*

* @author leizi

*

*/

public class Test51JobLogin {

private final String PARAM_USER = "username";

private final String PARAM_PWD = "userpwd";

private final String CHARSET = "gb2312";

private final String URL_LOGIN = "http://my.51job.com/my/My_Pmc.php";

private String RESUME_URL = "http://my.51job.com/cv/CResume/{id}/CV_CResumeManage.php";

public String login(HttpClient client, List loginParames) {

HttpPost post = null;

String result = "";

try {

client.getParams().setBooleanParameter(

CoreProtocolPNames.USE_EXPECT_CONTINUE, false);

client.getParams().setIntParameter("http.socket.timeout", 5000); // 5秒连接不上,则超时

client.getParams().setIntParameter("http.connection.timeout", 5000); // 5秒读取不到数据,超时

post = new HttpPost(URL_LOGIN);

HttpEntity he = new UrlEncodedFormEntity(loginParames);

post.setEntity(he);

HttpResponse response = client.execute(post);

HttpEntity entity = response.getEntity();

result = EntityUtils.toString(entity, CHARSET);

int status = response.getStatusLine().getStatusCode();

if (StringUtil.isNull(result)

&& ((status == HttpStatus.SC_MOVED_TEMPORARILY)

|| (status == HttpStatus.SC_MOVED_PERMANENTLY)

|| (status == HttpStatus.SC_SEE_OTHER) ||

(status == HttpStatus.SC_TEMPORARY_REDIRECT))) {

Header header = response.getFirstHeader("location");

if (header != null) {

result = header.getValue();

}

}

} catch (Exception e) {

if (post != null) {

post.abort();

}

e.printStackTrace();

} finally {

if (post != null) {

post.abort();

}

}

return result;

}

public String login(String user, String password, HttpClient client) {

try {

List loginParames = new ArrayList();

loginParames.add(new BasicNameValuePair(PARAM_USER, user));

loginParames.add(new BasicNameValuePair(PARAM_PWD, password));

String s = login(client, loginParames);

String reg = "my.51job.com/my/(.+?)/My_Pmc.php";

Pattern p = Pattern.compile(reg);

Matcher m = p.matcher(s);

if(m.find()){

return m.group(1);

}

} catch (Exception e) {

e.printStackTrace();

return "";

}

return "";

}

public String getResume(String userName, String pwd){

String result = "";

HttpClient client = new DefaultHttpClient();

String id = new Test51JobLogin().login(userName, pwd, client);

if(StringUtil.isNull(id)){

return "";

}

String resumeURl = RESUME_URL.replace("{id}", id);

try {

HttpGet get = new HttpGet(resumeURl);

HttpResponse response = client.execute(get);

HttpEntity entity = response.getEntity();

result = EntityUtils.toString(entity, CHARSET);

get.abort();

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

public static void main(String[] args) {

System.out.println(new Test51JobLogin().getResume("userName", "pwd"));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值