没有添加注释,挺兴奋的,总算弄出来了!
继续实现校内的功能
呵呵~
package Renren;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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;
import org.apache.http.entity.mime.Header;
public class MyRenRenFriend {
private static String userName = null;
private static String password = null;
private static HttpResponse response;
private static DefaultHttpClient httpClient = null;
public MyRenRenFriend(String userName, String password) {
this.userName = userName;
this.password = password;
this.httpClient = new DefaultHttpClient();
}
public HttpResponse login(){
String loginForm = "http://www.renren.com/PLogin.do";
String origURL = "http://www.renren.com/Home.do";
String domain = "renren.com";
HttpResponse response = null;
HttpPost httpPost = new HttpPost(loginForm);
NameValuePair email = new BasicNameValuePair("email", userName);
NameValuePair pass = new BasicNameValuePair("password", password);
NameValuePair dom = new BasicNameValuePair("domain", domain);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(email);
params.add(pass);
params.add(dom);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
response = httpClient.execute(httpPost);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
httpPost.abort();
}
return response;
// String loginForm = "http://www.renren.com/PLogin.do";
// String redirectURL = "http://www.renren.com/Home.do";
// String domain = "renren.com";
//
// HttpResponse response = null;
// HttpPost httpPost = new HttpPost(loginForm);
// NameValuePair email = new BasicNameValuePair("email", userName);
// NameValuePair pass = new BasicNameValuePair("password", password);
// NameValuePair dom = new BasicNameValuePair("domain", domain);
// NameValuePair origURL = new BasicNameValuePair("origURL", redirectURL);
// NameValuePair autoLogin = new BasicNameValuePair("autoLogin", "true");
// NameValuePair method = new BasicNameValuePair("method", "");
// NameValuePair submit = new BasicNameValuePair("submit", "登录");
// List<NameValuePair> params = new ArrayList<NameValuePair>();
//
// params.add(email);
// params.add(pass);
// params.add(dom);
// params.add(origURL);
// params.add(autoLogin);
// params.add(method);
// params.add(submit);
//
// try {
// httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
// response = httpClient.execute(httpPost);
// } catch (UnsupportedEncodingException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (ClientProtocolException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// } finally{
// httpPost.abort();
// }
// return response;
}
public String getAllFriends(){
String allFriendURL = "http://friend.renren.com/myfriendlistx.do";
HttpGet httpGet = new HttpGet(allFriendURL);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
StringBuffer sb = null;
try {
sb = new StringBuffer(this.httpClient.execute(httpGet, responseHandler));
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
httpGet.abort();
}
return new String(sb);
}
private String getName(String str){
StringBuffer sb = new StringBuffer(str);
int pos;
while ((pos = sb.indexOf("\\u")) > -1) {
String tmp = sb.substring(pos, pos + 6);
sb.replace(pos, pos + 6, Character.toString((char) Integer
.parseInt(tmp.substring(2), 16)));
}
str = sb.toString();
return str;
}
public void getFiendName(String str){
String str1 = "var friends=[";
String str2 = "var hotfriends=";
String friend = str.substring(str.indexOf(str1), str.indexOf(str2));
String regex = "name\":(.*?),\"head";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(friend.toString());
while(matcher.find()){
System.out.print(matcher.group(1));
System.out.println("----"+Test.getName(matcher.group(1)));
}
}
public void gc(){
}
public static void main(String[] args) {
MyRenRenFriend yww = new MyRenRenFriend("用户名", "密码");
System.out.println(yww.login());
String str = yww.getAllFriends();
System.out.println("here:");
System.out.println(str);
System.out.println("Name");
yww.getFiendName(str);
}
}
分享到:
相关推荐
使用 HttpClient 和 HtmlParser 实现简易网络爬虫
Android使用HttpClient实现上传下载功能并监控进度
HttpClient实现POST GET和文件下载 HttpClient实现POST GET和文件下载
使用HttpClient实现文件的上传所需lib
使用HttpClient登录网易邮箱 博文链接:https://bps.iteye.com/blog/136231
使用HttpClient4.5实现https请求忽略SSL证书验证工具类
模拟登录 不提供用户名密码
使用HttpClient必须的jar包 使用HttpClient必须的jar包 使用HttpClient必须的jar包
用HttpClient实现了 网易相册的登录
通过HttpClient实现远程下载,本例子通过java代码实现
用HttpClient实现文件下载需要满足:有一台web服务器,要下载的源文件需存放在web服务器的特定目录下。通过发起http请求实现文件下载功能。
这是一个经典的java爬虫例子:使用httpclient4.4登录豆瓣,发表说说 代码中即有cookie的自动管理,又有验证码的处理。 许多人只会简单的爬,但是有的网站需要登录就不会弄了,再或者有的网站有验证码也不知道如何...
HttpClient模拟登录实例,模拟登录网站,获取用户信息,登录后数据,带验证码登录,带解析HTML实例。
HttpURLConnection与HttpClient的区别: HttpClient是个很不错的开源框架(org.appache.http),封装了访问http的请求头,参数,内容体,响应等等,使用起来更方面更强大。 HttpURLConnection是java的标准类,可以...
使用 HttpClient 和 HtmlParser 实现简易爬虫
try(CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(url); StringEntity stringEntity = new StringEntity(params, Charset.forName("UTF-8")); ...
java中跨项目使用httpclient实现数据交换
Android Asynchronous HTTPClient的实现和优化
Android使用HttpClient实现上传下载功能并监控进度.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。