81,094
社区成员
发帖
与我相关
我的任务
分享
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class BaiduLoginMain {
public static void main(String[] args) {
BaiduLoginUtil.process("username", "xxxxxxx");
}
public static boolean process(String username, String password) {
boolean loginResult = false;
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
HttpEntity entity;
List<Cookie> cookies;
//组建登录的post包
try {
HttpPost httpPost = buildHttpPost(username, password);
response = httpclient.execute(httpPost);
entity = response.getEntity();
System.out.println("Login form get: " + response.getStatusLine());
EntityUtils.consume(entity);
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}
//进行页面跳转
//这里本来应该获取response中的Location,但是返回结果里面没有这个数据,所以我临时指定了跳转页面地址
String url = "http://www.baidu.com"; // 页面跳转
HttpGet httpget1 = new HttpGet(url);
response = httpclient.execute(httpget1);
// 理论上,这里应该登陆成功了
entity = response.getEntity();
if (entity != null) {
//do something...
}
// 显示结果
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(),
"UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
httpclient.getConnectionManager().shutdown();
}
return loginResult;
}
/**
* 构建request
* @param username
* @param password
*/
private static HttpPost buildHttpPost(String username, String password) {
HttpPost httpPost;
HttpClient httpclient = new DefaultHttpClient();
httpPost = new HttpPost(
"https://passport.baidu.com/v2/?login&tpl=mn&u=http://www.baidu.com/"); // 用户登录
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("TANGRAM__PSP_8__userName", username));
nvps.add(new BasicNameValuePair("TANGRAM__PSP_8__password", password));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
httpPost.addHeader("Cache-control", "no-cache");
httpPost.addHeader("Connection", "keep-alive");
httpPost.addHeader("Content-Encoding", "gzip");
httpPost.addHeader("Content-Language", "zh-CN");
httpPost.addHeader("Content-Type", "text/html;charset=UTF-8");
httpPost.addHeader("Server", "nginx");;
return httpPost;
}
}