当前访客身份:游客 [ 登录 | 加入开源中国 ]

代码分享

当前位置:
代码分享 » Java  » 编程基础
分享到: 
收藏 +0
2
自动登录51CTO签到、领取下载豆。
这个程序仅仅只是实现了功能,可以说是相当的简陋,欢迎大家多提意见。
基于httpcomponents-client-4.2.5实现的,请自行加入一下三个jar包:
commons-logging-1.1.1.jar
httpclient-4.2.5.jar
httpcore-4.2.4.jar
标签: HttpComponents

代码片段(1) [全屏查看所有代码]

1. [代码][Java]代码     跳至 [1] [全屏预览]

package me.qiu.http;

import java.io.*;
import java.util.*;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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 DailySign {

	public static final String URL_LOGIN = "http://home.51cto.com/index.php?s=/Index/doLogin";
	public static final String URL_SIGN = "http://home.51cto.com/index.php?s=/Home/toSign";
	public static final String URL_FREE_CREDITS = "http://down.51cto.com/download.php?do=getfreecredits&t="+System.currentTimeMillis();
	
	
	/**
	 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	 * 请修改成自己的用户名跟密码 
	 */
	private static String USER = "jeff.Qiu";
	private static String PASSWD = "******";
	
	public static final DefaultHttpClient client = new DefaultHttpClient();
	
	public static void main(String[] args) throws Exception {
		//创建登陆form
		UrlEncodedFormEntity formEntity = generateLoginFormEntity();		
		//创建登陆post请求
		HttpPost loginPost = new HttpPost(URL_LOGIN);
		loginPost.setEntity(formEntity);		
		//执行登陆请求
		HttpResponse loginResponse = client.execute(loginPost);
		//处理登陆响应
		processResponse(loginResponse);		
		//登陆成功获取cookie
		String authStr = getCookieInfo();		
		//创建签到请求
		HttpGet signGet = new HttpGet(URL_SIGN+authStr);
		//执行签到请求
		HttpResponse signResponse = client.execute(signGet);
		//处理响应
		showResult(signResponse);
		//创建领取下载豆请求
		HttpGet freeCreditsGet = new HttpGet(URL_FREE_CREDITS+authStr);
		//执行领取下载豆请求
		HttpResponse freeCreditsResponse = client.execute(freeCreditsGet);
		//处理响应
		showResult(freeCreditsResponse);
	}

	/**
	 * 创建登陆form
	 */
	private static UrlEncodedFormEntity generateLoginFormEntity() throws UnsupportedEncodingException {
		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
		formparams.add(new BasicNameValuePair("email", USER));
		formparams.add(new BasicNameValuePair("passwd", PASSWD));
		UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
		return formEntity;
	}
	/**
	 * 从cookie中获取SESSIONID,token等信息,从而保证下个请求能够通过登陆验证
	 */
	private static String getCookieInfo() {
		List<Cookie> cookies = client.getCookieStore().getCookies();
		String str = "";
		for (Cookie cookie : cookies) {
			str += "&"+cookie.getName()+"="+cookie.getValue();
		}
		return str;
	}

	/**
	 * 处理登陆响应
	 * 登陆成功后,会经过一个过渡页面,这个页面或想其他的子站点发请求来告知是否通过登陆验证
	 * 
	 */
	private static void processResponse(HttpResponse response) throws IllegalStateException, IOException{
		HttpEntity entity = response.getEntity();
		if(entity != null){
			//读取相应内容
			InputStream instream = entity.getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
			String line = null;
			while((line = reader.readLine()) != null){
				line = new String(line.getBytes(),"UTF-8");
				int begin = line.indexOf("http://");
				int end = line.indexOf("\"></script>", begin);
				HttpGet get = null;
				//截取子站点的网址并发送请求
				while(begin != -1){
					String url = line.substring(begin, end);
					System.out.println(url);
					get = new HttpGet(url);
					response = client.execute(get);
					System.out.println(response.getStatusLine().getStatusCode());
					get.abort();
					line = line.substring(end);
					begin = line.indexOf("http://");
					end = line.indexOf("\"></script>", begin);
				}				
			}
		}
		EntityUtils.consume(entity);
	}

	/**
	 * 读取相应内容并输出
	 */
	public static void showResult(HttpResponse response) throws IOException,
			UnsupportedEncodingException {
		int status = response.getStatusLine().getStatusCode();
		System.out.println(status);
		HttpEntity entity = response.getEntity();
		InputStream instream = null;
		if(entity != null){     
			instream  = entity.getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
			String line = null;
			while((line = reader.readLine()) != null){
				line = new String(line.getBytes(),"UTF-8");
				System.out.println(line);
			}
		}
		instream.close();
		EntityUtils.consume(entity);
	}
}


开源中国-程序员在线工具:Git代码托管 API文档大全(120+) JS在线编辑演示 二维码 更多»

发表评论 回到顶部 网友评论(10)

  • 1楼:都市网达 发表于 2013-05-08 18:46 回复此评论
    我来测试 一下
  • 2楼:jeff-qq 发表于 2013-05-09 21:42 回复此评论

    引用来自“都市网达”的评论

    我来测试 一下
    我天天用它自动签到,领豆……
  • 3楼:黄俊俊 发表于 2013-05-12 15:14 回复此评论
    简单的java post案例
  • 4楼:jeff-qq 发表于 2013-05-13 21:56 回复此评论

    引用来自“黄俊俊”的评论

    简单的java post案例
    这还真不是“简单”的post
  • 5楼:lxhxklyy 发表于 2013-07-12 22:47 回复此评论

    引用来自“都市网达”的评论

    我来测试 一下
    能用不?
  • 6楼:菠萝啊哈哈 发表于 2013-08-06 11:00 回复此评论
    好用
  • 7楼:wugang789 发表于 2013-08-08 16:53 回复此评论
    请问,请求地址怎么分析的
  • 8楼:aabbccli 发表于 2013-08-11 09:53 回复此评论
    请求地址用抓包工具抓
  • 9楼:rxw4703 发表于 2014-03-28 13:53 回复此评论
    现在已经不能用了
  • 10楼:lxhxklyy 发表于 2014-10-26 20:06 回复此评论
    我想知道什么做到的,抓包分析什么的,想学下