httpClient4模拟登录新浪微博

4 篇文章 0 订阅
2 篇文章 0 订阅

请查看最新新浪微博Rsa2加密方式,登录



import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
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.client.params.CookiePolicy;
import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

public class Sina {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		DefaultHttpClient client = new DefaultHttpClient();
		client.getParams().setParameter("http.protocol.cookie-policy",
				CookiePolicy.BROWSER_COMPATIBILITY);
		client.getParams().setParameter(
				HttpConnectionParams.CONNECTION_TIMEOUT, 5000);
		try {
			HttpPost post = new HttpPost(
					"http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.3.19)");

			String data = getServerTime();

			String nonce = makeNonce(6);

			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			nvps.add(new BasicNameValuePair("entry", "weibo"));
			nvps.add(new BasicNameValuePair("gateway", "1"));
			nvps.add(new BasicNameValuePair("from", ""));
			nvps.add(new BasicNameValuePair("savestate", "7"));
			nvps.add(new BasicNameValuePair("useticket", "1"));
			nvps.add(new BasicNameValuePair("ssosimplelogin", "1"));
			nvps.add(new BasicNameValuePair("su",
					encodeAccount(用户名)));
			nvps.add(new BasicNameValuePair("service", "miniblog"));
			nvps.add(new BasicNameValuePair("servertime", data));
			nvps.add(new BasicNameValuePair("nonce", nonce));
			nvps.add(new BasicNameValuePair("pwencode", "wsse"));
			nvps.add(new BasicNameValuePair("sp", new SinaSSOEncoder().encode(
					密码, data, nonce)));

			nvps.add(new BasicNameValuePair(
							"url",
							"http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack"));
			nvps.add(new BasicNameValuePair("returntype", "META"));
			nvps.add(new BasicNameValuePair("encoding", "UTF-8"));
			nvps.add(new BasicNameValuePair("vsnval", ""));

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

			HttpResponse response = client.execute(post);
			String entity = EntityUtils.toString(response.getEntity());

			
			String url = entity.substring(entity
					.indexOf("http://weibo.com/ajaxlogin.php?"), entity
					.indexOf("code=0") + 6);

			// 获取到实际url进行连接
			HttpGet getMethod = new HttpGet(url);

			response = client.execute(getMethod);
			entity = EntityUtils.toString(response.getEntity());
			entity = entity.substring(entity.indexOf("userdomain") + 13, entity
					.lastIndexOf("\""));
			System.out.println(entity);

			getMethod = new HttpGet("http://weibo.com/u/2745482124?topnav=1");
			response = client.execute(getMethod);
			entity = EntityUtils.toString(response.getEntity());
			// Document doc =
			// Jsoup.parse(EntityUtils.toString(response.getEntity()));
			System.out.println(entity);

		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	private static String encodeAccount(String account) {
		String userName = "";
		try {
			userName = Base64.encodeBase64String(URLEncoder.encode(account,
					"UTF-8").getBytes());
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userName;
	}

	private static String makeNonce(int len) {
		String x = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		String str = "";
		for (int i = 0; i < len; i++) {
			str += x.charAt((int) (Math.ceil(Math.random() * 1000000) % x
					.length()));
		}
		return str;
	}

	private static String getServerTime() {
		long servertime = new Date().getTime() / 1000;
		return String.valueOf(servertime);
	}
}




public class SinaSSOEncoder {
	private boolean i = false;
    private int g = 8;

    public SinaSSOEncoder() {

    }

    public String encode(String psw, String servertime, String nonce) {
        String password;
        password = hex_sha1("" + hex_sha1(hex_sha1(psw)) + servertime + nonce);
        return password;
    }

    private String hex_sha1(String j) {
        return h(b(f(j, j.length() * g), j.length() * g));
    }

    private String h(int[] l) {
        String k = i ? "0123456789ABCDEF" : "0123456789abcdef";
        String m = "";
        for (int j = 0; j < l.length * 4; j++) {
            m += k.charAt((l[j >> 2] >> ((3 - j % 4) * 8 + 4)) & 15) + ""
                    + k.charAt((l[j >> 2] >> ((3 - j % 4) * 8)) & 15);
        }
        return m;
    }

    private int[] b(int[] A, int r) {
        A[r >> 5] |= 128 << (24 - r % 32);
        A[((r + 64 >> 9) << 4) + 15] = r;
        int[] B = new int[80];
        int z = 1732584193;
        int y = -271733879;
        int v = -1732584194;
        int u = 271733878;
        int s = -1009589776;
        for (int o = 0; o < A.length; o += 16) {
            int q = z;
            int p = y;
            int n = v;
            int m = u;
            int k = s;
            for (int l = 0; l < 80; l++) {
                if (l < 16) {
                    B[l] = A[o + l];
                } else {
                    B[l] = d(B[l - 3] ^ B[l - 8] ^ B[l - 14] ^ B[l - 16], 1);
                }
                int C = e(e(d(z, 5), a(l, y, v, u)), e(e(s, B[l]), c(l)));
                s = u;
                u = v;
                v = d(y, 30);
                y = z;
                z = C;
            }
            z = e(z, q);
            y = e(y, p);
            v = e(v, n);
            u = e(u, m);
            s = e(s, k);
        }
        return new int[] { z, y, v, u, s };
    }

    private int a(int k, int j, int m, int l) {
        if (k < 20) {
            return (j & m) | ((~j) & l);
        }
        ;
        if (k < 40) {
            return j ^ m ^ l;
        }
        ;
        if (k < 60) {
            return (j & m) | (j & l) | (m & l);
        }
        ;
        return j ^ m ^ l;
    }

    private int c(int j) {
        return (j < 20) ? 1518500249 : (j < 40) ? 1859775393
                : (j < 60) ? -1894007588 : -899497514;
    }

    private int e(int j, int m) {
        int l = (j & 65535) + (m & 65535);
        int k = (j >> 16) + (m >> 16) + (l >> 16);
        return (k << 16) | (l & 65535);
    }

    private int d(int j, int k) {
        return (j << k) | (j >>> (32 - k));
    }

    private int[] f(String m, int r) {
        int[] l;
        int j = (1 << this.g) - 1;
        int len = ((r + 64 >> 9) << 4) + 15;
        int k;
        for (k = 0; k < m.length() * g; k += g) {
            len = k >> 5 > len ? k >> 5 : len;
        }
        l = new int[len + 1];
        for (k = 0; k < l.length; k++) {
            l[k] = 0;
        }
        for (k = 0; k < m.length() * g; k += g) {
            l[k >> 5] |= (m.charAt(k / g) & j) << (24 - k % 32);
        }
        return l;
    }
}


  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 27
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 27
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值