HttpClient4.3.4新浪微博cookie登录

        使用chrome浏览器(或者是火狐的httpfox,IE的httpwatch),F12,访问weibo.com,点击调试窗口的Network,选择最开始的Path:weibo.com,可以看到右边的一些属性信息:Request Method,Request Headers 等Request Headers 中有cookie属性值,整个header信息都需要在cookie登录时使用。

  

   String cookie_value = "cookie_value";
		String[] cookie_value_arr = cookie_value.split(";");
		
		CookieStore cookieStore = new BasicCookieStore();
		for (String v : cookie_value_arr) {
			String[] arr = v.split("=");
			
			BasicClientCookie cookie = new BasicClientCookie(arr[0],arr[1]);
			cookie.setDomain(".weibo.com");
			cookie.setVersion(0);
			cookie.setPath("/");
			cookieStore.addCookie(cookie);
		}
		CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();

		HttpContext httpcontext = new BasicHttpContext();
		httpcontext.setAttribute("http.cookie-store", cookieStore);
		
		HttpGet httpget = new HttpGet("http://weibo.com");
		httpget.addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
		httpget.addHeader("Accept-Encoding","gzip,deflate,sdch");
		httpget.addHeader("Accept-Language","zh-CN,zh;q=0.8,en;q=0.6");
		httpget.addHeader("Cache-Control","no-cache");
		httpget.addHeader("Connection","keep-alive");
		httpget.addHeader("Host","weibo.com");
		httpget.addHeader("Pragma","no-cache");
		httpget.addHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36");
		
		HttpResponse response = httpclient.execute(httpget,httpcontext);
		String ttt =  FileUtil.read(response.getEntity().getContent(), "", "UTF-8");
       System.out.println(ttt);


猜你喜欢

转载自itace.iteye.com/blog/2105605
今日推荐