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"));
}
}