Original link: http://yuncode.net/code/c_542f981eec3be26
Package Com.shiyimm.crawler.weibo;
Import java.io.FileNotFoundException;
Import Java.io.FileReader;
Import java.io.IOException;
Import java.io.UnsupportedEncodingException;
Import Java.net.URLDecoder;
Import Java.net.URLEncoder;
Import java.util.ArrayList;
Import Java.util.Date;
Import java.util.List;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;
Import javax.script.Invocable;
Import Javax.script.ScriptEngine;
Import Javax.script.ScriptEngineManager;
Import javax.script.ScriptException;
Import Net.sf.json.JSONObject;
Import org.apache.commons.codec.binary.Base64;
Import Org.apache.http.NameValuePair;
Import org.apache.http.client.ClientProtocolException;
Import org.apache.http.client.HttpClient;
Import org.apache.http.impl.client.DefaultHttpClient;
Import Org.apache.http.message.BasicNameValuePair;
Import Com.shiyimm.crawler.util.MyUrlUtil;
Import Com.shiyimm.crawler.util.UrlUtil;
public class Sinaweibo {
Private HttpClient client;
Private String username; Login account (Clear text)
private String password; Login password (plaintext)
Private String su; Login account (BASE64 encryption)
Private String sp; Login password (various parameters RSA encrypted cipher)
Private long servertime; The time stamp returned by the server when the initial login is used for password encryption and login
Private String nonce; At initial login, the server returns a string of characters used for password encryption and login
Private String rsakv; At initial login, the server returns a string of characters used for password encryption and login
Private String PubKey; The RSA public key returned by the server at initial logon
Private String Errinfo; Error message when login fails
Private String location; Jump connections after a successful login
Private String URL;
Public Sinaweibo (string Username, string password) {
Client = new Defaulthttpclient ();
This.username = Username;
This.password = password;
}
/**
* Initial Login Information <br>
* Return FALSE to indicate initial failure
* @return
*/
public Boolean prelogin () {
Boolean flag = false;
try {
Su = new String (base64.encodebase64 (Urlencoder.encode (username, "UTF-8"). GetBytes ());
String url = "http://login.sina.com.cn/sso/prelogin.php?entry=weibo&rsakt=mod&checkpin=1&" +
"Client=ssologin.js (v1.4.5) &_=" + Gettimestamp ();
url = + "&su=" + su;
String content;
Content = httptools.getrequest (client, URL);
SYSTEM.OUT.PRINTLN (content);
System.out.println ("content------------" + content);
Jsonobject json = jsonobject.fromobject (content);
SYSTEM.OUT.PRINTLN (JSON);
Servertime = Json.getlong ("Servertime");
Nonce = json.getstring ("nonce");
RSAKV = json.getstring ("rsakv");
PubKey = json.getstring ("PubKey");
Flag = Encodepwd ();
catch (Unsupportedencodingexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (Clientprotocolexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return flag;
}
/**
* Login
* @return true: Login Successful
*/
public Boolean login () {
if (Prelogin ()) {
String url = "Http://login.sina.com.cn/sso/login.php?client=ssologin.js (v1.4.5)";
list<namevaluepair> parms = new arraylist<namevaluepair> ();
Parms.add (New Basicnamevaluepair ("entry", "Weibo"));
Parms.add (New Basicnamevaluepair ("Geteway", "1"));
Parms.add (New Basicnamevaluepair ("from", ""));
Parms.add (New Basicnamevaluepair ("SaveState", "7"));
Parms.add (New Basicnamevaluepair ("Useticket", "1"));
Parms.add (New Basicnamevaluepair ("Pagerefer", "http://login.sina.com.cn/sso/logout.php?entry=miniblog&r=http %3A%2F%2FWEIBO.COM%2FLOGOUT.PHP%3FBACKURL%3D%2F "));
Parms.add (New Basicnamevaluepair ("VSNF", "1"));
Parms.add (New Basicnamevaluepair ("Su", su));
Parms.add (New Basicnamevaluepair ("service", "Miniblog"));
Parms.add (New Basicnamevaluepair ("Servertime", Servertime + ""));
Parms.add (New Basicnamevaluepair ("Nonce", nonce));
Parms.add (New Basicnamevaluepair ("Pwencode", "RSA2"));
Parms.add (New Basicnamevaluepair ("Rsakv", rsakv));
Parms.add (New Basicnamevaluepair ("SP", SP));
Parms.add (New Basicnamevaluepair ("Encoding", "UTF-8"));
Parms.add (New Basicnamevaluepair ("Prelt", "182"));
Parms.add (New Basicnamevaluepair ("url", "http://weibo.com/ajaxlogin.php?framelogin=1&callback= Parent.sinaSSOController.feedBackUrlCallBack "));
Parms.add (New Basicnamevaluepair ("ReturnType", "META"));
try {
String content = httptools.postrequest (client, URL, parms);
System.out.println ("Content----------" + content);
String regex = "location\\.replace\\ (\" (. +?) \"\\);";
Pattern p = pattern.compile (regex);
Matcher m = p.matcher (content);
if (M.find ()) {
Location = M.group (1);
if (Location.contains ("reason=")) {
Errinfo = location.substring (Location.indexof ("reason=") + 7);
Errinfo = Urldecoder.decode (Errinfo, "GBK");
} else {
String result = httptools.getrequest (client, location);
SYSTEM.OUT.PRINTLN ("result--------------" + result);
return true;
}
}
catch (Clientprotocolexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
url = "Http://www.weibo.com/hm";
System.out.println (Myurlutil.getresource (URL));
}
return false;
}
/**
* Password for RSA encryption <br>
* Return FALSE to indicate encryption failure
* @return
*/
Private Boolean encodepwd () {
Scriptenginemanager sem = new Scriptenginemanager ();
ScriptEngine se = Sem.getenginebyname ("javascript");
try {
FileReader FR = new FileReader ("E:\\encoder.js");
Se.eval (FR);
Invocable invocableengine = (invocable) se;
String callbackvalue = (string) invocableengine.invokefunction ("Encodepwd", PubKey, Servertime, nonce, password);
SP = Callbackvalue;
return true;
catch (FileNotFoundException e) {
TODO auto-generated Catch block
SYSTEM.OUT.PRINTLN ("Cryptographic script encoder.sj not found");
catch (Scriptexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (Nosuchmethodexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Errinfo = "Password encryption failed!";
return false;
}
Public String Geterrinfo () {
return errinfo;
}
/**
* Get time stamp
* @return
*/
Private Long Gettimestamp () {
Date now = new Date ();
return Now.gettime ();
}
public static void Main (string[] args) throws Clientprotocolexception, IOException {
Sinaweibo Weibo = new Sinaweibo ("Account", "password");
if (Weibo.login ()) {
System.out.println ("Landing success". ");
String url = "Http://www.weibo.com/hm";
String Source = myurlutil.getresource (URL);
SYSTEM.OUT.PRINTLN (source);
} else {
SYSTEM.OUT.PRINTLN ("Login failed.") ");
}
}
}
Package Com.shiyimm.crawler.weibo;
Import java.io.IOException;
Import java.util.List;
Import org.apache.http.HttpEntity;
Import Org.apache.http.HttpResponse;
Import Org.apache.http.NameValuePair;
Import org.apache.http.client.ClientProtocolException;
Import org.apache.http.client.HttpClient;
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.util.EntityUtils;
public class Httptools {
/**
* Normal Get mode HTTP request
* @param Client
* @param URL
* @return
* @throws clientprotocolexception
* @throws IOException
*/
public static string Getrequest (HttpClient client, String URL) throws Clientprotocolexception, IOException {
HttpGet get = new HttpGet (URL);
Get.addheader ("User-agent", "mozilla/5.0 (Windows NT 6.1) applewebkit/537.11 (khtml, like Gecko) chrome/23.0.1271.64 safari/537.11 ");
Get.addheader (" Referer", "http://2013.weibo.com/");
HttpResponse response = Client.execute (get);
httpentity entity = response.getentity ();
String content = entityutils.tostring (entity, "GBK");
SYSTEM.OUT.PRINTLN (content);
/*entityutils.consume (entity);
return content;
}
/**
* Normal Post Mode HTTP request
* @param Client
* @param URL
* @param parms
* @return
* @throws clientprotocolexception
* @throws IOException
*/
public static string Postrequest (HttpClient client, String URL, list<namevaluepair> parms) throws Clientprotocolexception, IOException {
HttpPost post = new HttpPost (URL);
Post.addheader ("User-agent", "mozilla/5.0 (Windows NT 6.1) applewebkit/537.11 (khtml, like Gecko) chrome/23.0.1271.64 safari/537.11 ");
Post.addheader ("Content-type", "application/x-www-form-urlencoded");
Post.addheader (" Referer", "http://2013.weibo.com/");
Urlencodedformentity postentity = new Urlencodedformentity (parms, "UTF-8");
Post.setentity (postentity);
HttpResponse response = Client.execute (POST);
httpentity entity = response.getentity ();
String content = entityutils.tostring (entity, "GBK");
/*entityutils.consume (entity);
return content;
}
}
Original link: http://yuncode.net/code/c_542f981eec3be26