import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import org.apache.http.HttpResponse;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.impl.client.LaxRedirectStrategy;
import org.apache.http.util.EntityUtils;
// jsoup class
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.apache.log4j.Logger;
/* Author: Leo Li
Email: krislee@163.com
Last updated: 2014-12-09
*/
public class renren {
private static HttpResponse response;
private static DefaultHttpClient httpClient;
private static final Logger logger = Logger.getLogger(renren.class);
public static HttpResponse do_init(String userName, String password) {
httpClient = new DefaultHttpClient();
httpClient.setRedirectStrategy(new LaxRedirectStrategy());
String loginForm = "http://www.renren.com/PLogin.do";
String origURL = "http://www.renren.com/Home.do";
String domain = "renren.com";
HttpPost httpPost = new HttpPost(loginForm);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("email", userName));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("origURL", origURL));
params.add(new BasicNameValuePair("domain", domain));
try {
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
response = postMethod(httpPost);
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
return response;
}
}
public static HttpResponse postMethod(HttpPost post) {
HttpResponse resp = null;
try {
resp = httpClient.execute(post);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// post.abort();
}
return resp;
}
public HttpResponse getMethod(String url) {
HttpGet get = new HttpGet(url);
HttpResponse resp = null;
try {
resp = httpClient.execute(get);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
get.abort();
}
return resp;
}
public static String showResult(HttpResponse response) throws IOException,
UnsupportedEncodingException {
int status = response.getStatusLine().getStatusCode();
String line = "";
String ret = "";
logger.info("Http response status:" + status);
HttpEntity entity = response.getEntity();
InputStream instream = null;
if (entity != null) {
instream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(
instream));
while ((line = reader.readLine()) != null) {
line = new String(line.getBytes(), "UTF-8");
ret += line;
}
}
instream.close();
EntityUtils.consume(entity);
return ret;
}
public static boolean do_parse(String input) {
boolean b_suc = false;
String linkHref = null;
String linkText = null;
try {
Document doc = Jsoup.parse(input, "UTF-8");
Elements links = doc.getElementsByAttribute("href");
for (Element link : links) {
linkHref = link.attr("href");
linkText = link.text();
if (linkHref.equals("http://guide.renren.com/welcome")) {
logger.debug("Welcome text (UTF-8 encoding):" + linkText);
b_suc = true;
break;
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return b_suc;
}
public static void main(String[] args) {
if (args.length != 2) {
logger.error("Usage: renren <user> <password>");
System.exit(-1);
}
String user = args[0];
String passwd = args[1];
String ret = "";
try {
response = do_init(user, passwd);
if (response != null) {
ret = showResult(response);
System.out.println("Response Text:" + ret);
// 解析页面,检查是否有关键字串出现
boolean bOK = do_parse(ret);
if (bOK) {
logger.info("You logged in successfully!");
} else {
logger.info("Login failed!");
}
} else {
logger.error("Error occurs when logging on web site!");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (httpClient != null)
httpClient.getConnectionManager().shutdown();
}
}
}
评论0
最新资源