很多网友问到登录过程,我这里放一点相关的代码上来吧。大家集中的问题主要有几个:

Q: 密码的md5算法是怎样的
A: 这个算法我稍后给出来

Q: 登录时有几个参数不知是什么意思,如何计算出来
A: 这些参数未必有意义,根据形式,可以大概伪造一下,成功率一般都是90%以上

Q: 如何获取embed_swf?后面的那几个参数,好像也是加密过的,如何获取
A: 请参看本文源码,不需要理会

Q :如何获取sessionid和uid
A: 登录后从cookie中获取,请参看本文源码

下面就是一个登录演示的代码,各位可以参考。一些注意事项,没有办法约束,只能靠大家的道德了:

  1. 请不要用这个代码干坏事;
  2. 源码为本站原创,如果在你的代码中能保留或者注明一下,我将不胜感激;
  3. 欢迎交流,如果有价值,我会在私下提供更多的资源;
  4. 没有太多时间,如果你对 swt/多线程 等比较熟,我们可以一起开发这个小工具。

注意:我使用的这个博客发文章的编辑器会自动将&替换成&  大家使用的时候修改回来。

package com.codigg.farmer;
 
import java.io.InputStream;
import java.util.List;
 
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
 
/**
 * 农场登录演示代码
 * @author www.codigg.com
 *
 */
public class Test {
 
	public static void main(String[] args) throws Exception {
		// 登录,其中后面的密码是www.codigg.com加密后的结果
		testLogin("www.codigg.com", "4b1f0bdd013adac8b11cae3f98cc7f27");
	}
 
	public static void testLogin(String username, String password)
			throws Exception {
 
		// 创建HttpClient对象
		DefaultHttpClient httpclient = new DefaultHttpClient();
 
		// 登录,其中密码是已经加密后的字符串,其它参数都可以伪造,不用理会
		HttpGet getLogin = new HttpGet(
				"http://passport.sohu.com/sso/login.jsp?userid="
					+ username
					+ "&password="
					+ password
					+ "&appid=1062&persistentcookie=1"
					+ "&s=1344036327984&b=5&w=1280&pwdtype=1");
 
		HttpResponse response = httpclient.execute(getLogin);
		HttpEntity entity = response.getEntity();
		if (entity != null)
			entity.consumeContent();
 
		// 此时就已经有登录后的会话了,然后访问农场页面
		HttpGet getFarm = new HttpGet("http://bai.sohu.com/app/farm/");
		HttpResponse response2 = httpclient.execute(getFarm);
		HttpEntity entity2 = response2.getEntity();
		InputStream input = entity2.getContent();
 
		// 从农场的页面中提取农场iframe的路径,尤其是embed_swf后面的那堆参数
		// 很多网友提问都是不知道这里怎么处理
		String farmUrl = null;
		List lines = IOUtils.readLines(input);
		String tag = "<iframe src=\"http://3rdproxy.bai.sohu.com/farm/embed_swf";
		for (String line : lines) {
			if (line.indexOf(tag) >= 0) {
				// 这里直接从HTML源码里面通过字符串查找来获取的,
				// 如果觉得土,可以用正则表达式
				farmUrl = line.substring(13, line.indexOf("\"", 20));
				break;
			}
		}
 
		if (entity2 != null)
			entity2.consumeContent();
 
		// 没有获取到iframe地址,表示登录失败
		if (farmUrl == null) {
			// TODO: 添加自己的处理代码
		}
 
		// 这里的farmUrl就是iframe的路径了,通过这个访问Flash
		HttpGet getSession = new HttpGet(farmUrl);
		HttpResponse response3 = httpclient.execute(getSession);
		HttpEntity entity3 = response3.getEntity();
		if (entity3 != null)
			entity3.consumeContent();
 
		// 接下来从农场服务器(和白社会服务器不是一个)的会话中获取sessionId和
		// 当前登录用户的uid,这两个参数都需要
		String sessionId = null;
		String uid = null;
 
		List<Cookie> cookies = httpclient.getCookieStore().getCookies();
		if (!cookies.isEmpty()) {
			for (Cookie cookie : cookies) {
				if ("sessionid".equals(cookie.getName()))
					sessionId = cookie.getValue();
				if ("suer".equals(cookie.getName()))
					uid = cookie.getValue().split("[|]")[0];
			}
		}
 
		// TODO: 接下来进行其它自己的操作
	}
}