我最近想获取一些新浪微博的信息,而且新浪SDK的API不提供方法,所以在尝试使用HttpClient实现模拟登录.我在网上找了些资料和代码,试过很多版本的代码,都大同小异,而且基本都是1.3.xx的新浪登录版本(目前的版本已经是:1.4.11),我都进行了对应的修改并尝试登录,但是始终都不能不成功,贴上代码,希望有了解这块的高手帮忙指点一下,感激不尽.public class TestSina {  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.4.11)");
      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("[email protected]")));
      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("haosouke123", 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", ""));
      nvps.add(new BasicNameValuePair("useticket", "1"));
      nvps.add(new BasicNameValuePair("vsnf", "1"));
      post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
      HttpResponse response = client.execute(post);
      String entity = EntityUtils.toString(response.getEntity());
//      System.out.println("---------------------->"+entity);
      entity= entity.substring(entity.indexOf("location.replace")+"location.replace".length()+2);
      entity = entity.substring(0,entity.indexOf("\")"));
      HttpGet getMethod = new HttpGet(entity);
      response = client.execute(getMethod);
//      System.out.println("====================================>"+EntityUtils.toString(response.getEntity()));
      getMethod = new HttpGet("http://weibo.com/");
      response = client.execute(getMethod);
      entity = EntityUtils.toString(response.getEntity());
      System.out.println(entity);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 
  private static String encodeAccount(String account) {
    String userName = "";
    try {
      userName = Base64.encodeBase64String(URLEncoder.encode(account,
      "UTF-8").getBytes());
    } catch (Exception e) {
      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;
}
}
HttpClient新浪微博模拟登录求助Java

解决方案 »

  1.   

    给我qq 我有全套写好的代码 新浪 搜狐 腾讯  直接main 方法运行 ,开发一个 gcd 监测言论的系统
      

  2.   

    好像有两种加密方式  不知道怎么判断的  我抓了几次都是用的rsa2的 if (b.loginType & x && b.servertime && sinaSSOEncoder && sinaSSOEncoder.RSAKey) {
    e.servertime = b.servertime;
    e.nonce = b.nonce;
    e.pwencode = "rsa2";
    e.rsakv = b.rsakv;
    var f = new sinaSSOEncoder.RSAKey;
    f.setPublic(b.rsaPubkey, "10001");
    c = f.encrypt([b.servertime, b.nonce].join("\t") + "\n" + c)
    } else if (b.loginType & y && b.servertime && sinaSSOEncoder && sinaSSOEncoder.hex_sha1) {
    e.servertime = b.servertime;
    e.nonce = b.nonce;
    e.pwencode = "wsse";
    c = sinaSSOEncoder.hex_sha1("" + sinaSSOEncoder.hex_sha1(sinaSSOEncoder.hex_sha1(c)) + b.servertime + b.nonce)
    }
    e.sp = c;
    return e