httpclient 4如何登陆csdn呀

爱dota爱生活 发布于 2014/03/17 09:03
阅读 589
收藏 2

package csdn;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.li.bean.Result;
import com.li.utli.SendRequest;

public class GetCsdn {
 /**
  * @param args
  * @throws IOException
  * @throws ClientProtocolException
  * @throws ParseException
  */
 public static void main(String[] args) throws ParseException, ClientProtocolException, IOException {
  
  String uname="";
  String upwd="";
  Map<String,String> headers = new HashMap<String,String>();
  headers.put("Referer", "https://passport.csdn.net/ajax/accounthandler.ashx");
  //headers.put("Host", "localhost:8080");
  DefaultHttpClient httpclient = new DefaultHttpClient();
  SendRequest sb=new SendRequest();
  Map<String,String> formparams = new HashMap<String,String>();
  formparams.put("t", "log");
  formparams.put("u", uname);
  formparams.put("p", upwd);
  formparams.put("remember", "0");
  formparams.put("f", "http://www.csdn.net/");
  formparams.put("rand", "0.07983653520170819");
  UrlEncodedFormEntity uefEntity;
  Result rs=sb.sendGet("https://passport.csdn.net/ajax/accounthandler.ashx?t=log&u="+uname+"&p="+upwd+"&remember=0&f=http%3A%2F%2Fwww.csdn.net%2F&rand=0.07983653520170819", headers, formparams, "utf-8");
  String cookie =  SendRequest.sendGet("https://passport.csdn.net/ajax/accounthandler.ashx", null, formparams, "utf-8").getCookie();         
  HashMap<String, String> header = new HashMap<String, String>();                
  header.put("Cookie",cookie);                                    
  System.out.println(EntityUtils.toString( SendRequest.sendGet("http://my.csdn.net/my/album", header, null, "utf-8").getHttpEntity(),"utf-8"));
  
 }
}

谁能帮忙看看,登陆失败

QQ:502086525

加载中
0
一堆BUG
一堆BUG

使用httpclient 4.1进行登陆

public class Csdn {
	public static void main(String[] args) throws Exception {
		Csdn c = new Csdn();
		c.login("username", "password");
	}

	public List<Cookie> login(String username, String password)
			throws Exception {
		DefaultHttpClient client = new DefaultHttpClient();

		HttpPost post = new HttpPost(
				"https://passport.csdn.net/ajax/accounthandler.ashx");

		post.setHeader("Accept", "*/*");
		post.setHeader("Accept-Encoding", "gzip, deflate");
		post.setHeader("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
		post.setHeader("Host", "passport.csdn.net");
		post.setHeader("Referer", "https://passport.csdn.net/account/login");
		post.setHeader("User-Agent",
				"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0");
		post.setHeader("X-Requested-With", "XMLHttpRequest");

		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("u", username));
		nvps.add(new BasicNameValuePair("p", password));
		nvps.add(new BasicNameValuePair("t", "log"));
		nvps.add(new BasicNameValuePair("remember", "0"));
		nvps.add(new BasicNameValuePair("f", "http%3A%2F%2Fwww.csdn.net%2F"));
		nvps.add(new BasicNameValuePair("rand", "0.4835865827484527"));

		post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

		HttpResponse resp = client.execute(post);

		CookieStore cs = client.getCookieStore();
		List<Cookie> cookies = cs.getCookies();
		for (Cookie cookie : cookies) {
			System.out.println(cookie.getName() + "\t" + cookie.getValue()
					+ "\t" + cookie.getDomain());
		}

		HttpEntity entity = resp.getEntity();
		String string = EntityUtils.toString(entity);

		System.out.println(string);
		return cookies;

	}
}

最终,返回这个登陆的成功后的Cookies

AU		E73	csdn.net
UD		%e8%bf%98%e5%9c%a8%e5%8a%aa%e5%8a%9b%e5%ad%a6%e4%b9%a0JAVA%e5%92%8cC%23%e4%b8%ad%7e	csdn.net
UN		username	csdn.net
UserInfo	WB8J%2fH1xsgZSNd9G6scUuMWSkg9w3nPhDC4EAwggDgdwpcDMEP9K7gjPeBDAEBGX7Q0OrmdShdAPeUqSxdWwRGt80tebo2Axo8NaZQMqIk3K1qLMR5P6Ru%2fszIylqhO7	csdn.net
UserName	username	csdn.net
UserNick	username	csdn.net
access-token	25c61d1c-b990-4cef-ae78-47f4573791a9	csdn.net
{"status":true,"error":"","data":{"userId":660387,"userName":"username","password":"ebe28a96075c59fe6afbca9f4a2c3baf","email":"username@163.com","lastLoginTime":"2014-03-17 15:15:16","loginTimes":192,"lastLoginIP":"61.187.56.62","isDeleted":false,"registerIP":"","registerTime":"2003-12-27 22:52:00","isActived":true,"role":0,"userType":0,"isLocked":false,"encryptUserInfo":"WB8J/H1xsgZSNd9G6scUuMWSkg9w3nPhDC4EAwggDgdwpcDMEP9K7gjPeBDAEBGX7Q0OrmdShdAPeUqSxdWwRGt80tebo2Axo8NaZQMqIk3K1qLMR5P6Ru/szIylqhO7"}}



有了Cookies后面的操作就好做了,可以直接使用这个Cookies进行一些需要登陆后才能使用的操作




0
开源中国时时彩理财师
开源中国时时彩理财师
参考:http://www.oschina.net/question/864540_134478
0
爱dota爱生活

有木有实现好的呀

0
DayByDay
DayByDay
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost post = new HttpPost("http://xxxxx/login.html");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("username",username));
nvps.add(new BasicNameValuePair("password",password));
post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
HttpResponse response = httpclient.execute(post);
HttpEntity entity = response.getEntity();
if (entity != null) {
	EntityUtils.toString(entity);
}



0
爱dota爱生活

像楼上写的这么简单,那就好了。关键不是的呀。

0
DayByDay
DayByDay

引用来自“周子龙s”的答案

像楼上写的这么简单,那就好了。关键不是的呀。

先把浏览器缓存和cookie清除了,然后,1.访问主页,2.访问登录页面,3.输入用户名密码等进行登录,把步骤123的包都抓下来,找出响应中带有set-cookie的请求和post方式的请求,然后编程模拟步骤123的一系列过程,应该就ok了。反正之前我这么搞过两个网站都成功了
返回顶部
顶部