前言:
现时咱们对“javapost模拟”大体比较关怀,朋友们都想要了解一些“javapost模拟”的相关文章。那么小编也在网络上网罗了一些对于“javapost模拟””的相关内容,希望咱们能喜欢,看官们快快来学习一下吧!import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Date;
import java.util.List;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.httpclient.methods.PostMethod;
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;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.util.SimpleNodeIterator;
public class Sina {
/**
*
* @param args
*/
private static DefaultHttpClient client = new DefaultHttpClient();
private static HttpGet getMethod;
private static int i = 1;
private static boolean state = true;
private static DB db = new DB();
private static String address;
private static String category = "";
private static String oldAddress = "";
public void login() {
String entity = "";
client.getParams().setParameter("http.protocol.cookie-policy",
CookiePolicy.BROWSER_COMPATIBILITY);
client.getParams().setParameter(
HttpConnectionParams.CONNECTION_TIMEOUT, 5000);
try {
HttpPost post = new HttpPost(
";);
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",
";));
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);
entity = EntityUtils.toString(response.getEntity());
String url = entity.substring(entity
.indexOf(";), 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);
String rnd = String.valueOf(System.currentTimeMillis());
} catch (Exception e) {
// TODO: handle exception
}
}
public void postweibo() {
try {
String rnd = String.valueOf(System.currentTimeMillis());
HttpPost po = new HttpPost(
"; + rnd);
po.setHeader("Host", "weibo.com");
po
.setHeader(
"User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.27) Gecko/20120216 Firefox/3.6.27");
po
.setHeader("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
po.setHeader("Accept-Language", "zh-cn,zh;q=0.5");
po.setHeader("X-Requested-With", "XMLHttpRequest");
po.setHeader("Connection", "keep-alive");
po.setHeader("Keep-Alive", "115");
// po.setHeader("Accept-Encoding", "gzip,deflate");
// po.setHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");
po.setHeader("Content-Type",
"application/x-www-form-urlencoded; charset=UTF-8");
po
.setHeader(
"Cookie",
"UOR=,; ULV=1340765073888:8:8:4:4688432092614.525.1340765073834:1340703054967; myuid=2564779282; un=noumiandc817@163.com; ALF=1341212662; wvr=3.6; ads_ck=0; SinaRot/3/336396964%3Fwvr%3D3.6%26lf%3Dreg=34; SinaRot/3/336396964=44; SUS=SID-2564779282-1340765058-JA-n7e8d-961416038237762579a7ab19ca080fcc; SUE=es%3Da60b06450448833f66be957ff9972026%26ev%3Dv1%26es2%3Df5bb9e1a309ba646bd7a95b7f69731a2%26rs0%3DsftZRxYfASmFFINvWsEz2Ji3wLWir6XbvBKoe0y56kHouwuPekRdDf5YzKKJI6xjwjtBrjpAOMLzIiZbqB5uEcpMsD2wgiqqhuDHaAt%252B%252Fe5K5hgm60s4%252FKOAtRWqw%252BPj2t96338HGMCrby5WbRDt%252BNs0KSEwFWlvn35wj1hN30I%253D%26rv%3D0; SUP=cv%3D1%26bt%3D1340765058%26et%3D1340851458%26d%3Dc909%26i%3D9db4%26us%3D1%26vf%3D0%26vt%3D0%26ac%3D0%26uid%3D2564779282%26user%3Dnoumiandc817%2540163.com%26ag%3D4%26name%3Dnoumiandc817%2540163.com%26nick%3Dgooleee%26fmp%3D%26lcp%3D; SSOLoginState=1340764910; USRHAWB=usrmdins211_114; _s_tentry=login.sina.com.cn; Apache=4688432092614.525.1340765073834; SINAGLOBAL=4688432092614.525.1340765073834");
List<NameValuePair> npost = new ArrayList<NameValuePair>();
npost.add(new BasicNameValuePair("text", "weibo"));
npost.add(new BasicNameValuePair("pic_id", ""));
npost.add(new BasicNameValuePair("rand", ""));
npost.add(new BasicNameValuePair("_surl", ""));
npost.add(new BasicNameValuePair("module", "stissue"));
npost.add(new BasicNameValuePair("_t", "0"));
po.setEntity(new UrlEncodedFormEntity(npost, HTTP.UTF_8));
HttpResponse responses = client.execute(po);
System.out.println(EntityUtils.toString(responses.getEntity(),
"UTF-8"));
} catch (Exception e) {
}
}
public static String getHtml(String url1) {
String entity = "";
try {
getMethod = new HttpGet(url1);
HttpResponse response = client.execute(getMethod);
entity = EntityUtils.toString(response.getEntity(), "utf-8");
// Document doc =
// Jsoup.parse(EntityUtils.toString(response.getEntity()));
// System.out.println(entity);
} catch (Exception e) {
// TODO: handle exception
}
return entity;
}
public static String testHtml(String url) {
String sCurrentLine;
String sTotalString;
sCurrentLine = "";
sTotalString = "";
try {
java.io.InputStream l_urlStream;
java.net.URL l_url = new java.net.URL(url);
java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) l_url
.openConnection();
l_connection.connect();
l_urlStream = l_connection.getInputStream();
java.io.BufferedReader l_reader = new java.io.BufferedReader(
new java.io.InputStreamReader(l_urlStream));
// File file = new File("D:/a.txt");
// BufferedReader l_reader = null;
// l_reader = new BufferedReader(new FileReader(file));
int line = 1;
// ζУnull
while ((sCurrentLine = l_reader.readLine()) != null) {
sTotalString += sCurrentLine;
}
// System.out.println(sTotalString);
// System.out.println(" ==================== ");
// findAllImgTagSrc(sTotalString);
// System.out.println(testText);
} catch (Exception e) {
e.printStackTrace();
}
return sTotalString;
}
public static void getWeibo() {
Set<String> slist = new HashSet();
Parser parser = new Parser();
NodeFilter filter;
NodeList list = new NodeList();
try {
parser.setInputHTML(getHtml("; + address));
System.out.println("; + address);
System.out.println(getHtml("; + address));
filter = new TagNameFilter("a");
list = parser.extractAllNodesThatMatch(filter);
} catch (ParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("IMG tags number :" + list.size());
SimpleNodeIterator iterator = list.elements();
while (iterator.hasMoreNodes()) {
String s = "";
//
Node node = iterator.nextNode();
TagNode tagNode = new TagNode();
// TagNode е
tagNode.setText(node.toHtml());
if (tagNode.getAttributeEx("onclick") != null
&& tagNode.getAttributeEx("title") != null) {
// if(tagNode.getAttributeEx("title")!=null){
s = tagNode.getAttributeEx("title").toString();
// if(s.indexOf("@")!=-1){
s = s.substring(7, s.length() - 1);
// state=true;
slist.add(s);
}
if (tagNode.getAttributeEx("href") != null
&& tagNode.getAttribute("class") != null) {
String ss = tagNode.getAttribute("class").toString();
// System.out.println(ss);
if (ss.equals("W_btn_a")) {
address = tagNode.getAttributeEx("href").toString();
// if(s.indexOf("@")!=-1){
address = address.substring(6, address.length() - 1);
}
}
}
/*
* while(iterator.hasMoreNodes()){ String s = ""; // Node
* node = iterator.nextNode(); TagNode tagNode = new TagNode(); //
* TagNode е tagNode.setText(node.toHtml());
* if (tagNode.getAttributeEx("class").equals("W_btn_a")) { //
* if(tagNode.getAttributeEx("title")!=null){ s =
* tagNode.getAttributeEx("href").toString(); // if(s.indexOf("@")!=-1){
* System.out.println(s); // state=true; } }
*/
Iterator<String> it = slist.iterator();
while (it.hasNext()) {
String str = it.next();
// System.out.println("@"+str);
// int start=str.indexOf("(");
// int end=str.indexOf(")");
// str=str.substring(start+1, end);
System.out.println("@" + str);
if (db.checkUser(str) != 1) {
db.addUser(str, category);
}
/*
* String url2 = java.net.URLDecoder(
* "\u62b1\u6b49\uff0c\u6b64\u5fae\u535a\u4e0d\u5b58\u5728\u54e6\uff0c\u6362\u4e00\u4e2a\u8bd5\u8bd5\u5427\u3002"
* , "utf-8");
*/
/* System.out.println(url2); */
}
// getWeibo(";+address);
/*System.out.println(address);
// address = "; + address;
if (!oldAddress.equals(address)) {
oldAddress = address;
getWeibo();
}*/
/*
* i++; testHtml();
*/
}
public static void main(String[] args) throws UnsupportedEncodingException {
Sina sina = new Sina();
sina.login();
//Sina.category = "太胖";
//db.addCategory(category);
//String url2 = java.net.URLEncoder.encode(category, "utf-8");
//System.out.println(url2);
//Sina.address = "/weibo/" + url2;
sina.getWeibo();
//WriteTxt wt = new WriteTxt();
//wt.WriteFile(category, db.getAll(category));
//sina.getHtml(";);
// sina.postweibo();
/*
* sina.login();sina.getWeibo(
* ";);
*/
/*
* 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(
*
*
*
*
*
*
*
*
* ";);
*
* 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("noumiandc817@163.com")));
*
* 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(
*
* "jde0417", data, nonce)));
*
* nvps .add(new BasicNameValuePair(
*
* "url",
*
* ";
* ));
*
* 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(";), 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); String rnd =
* String.valueOf(System.currentTimeMillis()); getMethod = new HttpGet(
* ";);
*
* response = client.execute(getMethod);
*
* entity = EntityUtils.toString(response.getEntity(), "utf-8");
*
* // Document doc =
*
* // Jsoup.parse(EntityUtils.toString(response.getEntity()));
*
* System.out.println(entity);
*
*
* HttpPost po = new HttpPost(
*
* ";+rnd);
*
*
*
* List<NameValuePair> npost = new ArrayList<NameValuePair>();
* npost.add(new BasicNameValuePair("text", "weibo")); npost.add(new
* BasicNameValuePair("pic_id", "")); npost.add(new
* BasicNameValuePair("rand", "")); npost.add(new
* BasicNameValuePair("_surl", "")); npost.add(new
* BasicNameValuePair("module", "stissue")); npost.add(new
* BasicNameValuePair("_t", "0")); po.setEntity(new
* UrlEncodedFormEntity(npost, HTTP.UTF_8));
*
* HttpResponse responses = client.execute(po);
* System.out.println(responses.getEntity());
*
* } 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;
}
}
将账号存入数据库
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class DB {
private Connection conn;
private Statement stmt;
private ResultSet rs;
public Connection getConn() {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/weibo?useUnicode=true&characterEncoding=utf-8";
String user = "root";
String password = "root";
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public int checkUser(String s) {
int i = 0;
getConn();
try {
stmt = conn.createStatement();
String sql = "select * from users where name='" + s + "'";
rs = stmt.executeQuery(sql);
if (rs.next()) {
i = 1;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
closeConn();
return i;
}
public List<String> getAll(String s) {
List<String> list = new ArrayList();
getConn();
try {
stmt = conn.createStatement();
String sql = "select name from users where categoryId=(select id from category where name='"
+ s + "')";
// System.out.println(sql);
rs = stmt.executeQuery(sql);
while (rs.next()) {
list.add(rs.getString(1));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
closeConn();
return list;
}
public void addUser(String s, String name) {
getConn();
try {
stmt = conn.createStatement();
String sql = "insert into users(name,categoryId) values('" + s
+ "',(select Id from category where name='" + name + "') )";
stmt.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
closeConn();
}
public void addCategory(String name) {
getConn();
try {
stmt = conn.createStatement();
String sql = "select * from category where name='" + name + "'";
rs = stmt.executeQuery(sql);
if (!rs.next()) {
sql = "insert into category(name) values('" + name + "')";
stmt.executeUpdate(sql);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
closeConn();
}
public void closeConn() {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
根据关键词输出所有账号到txt文件
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
public class WriteTxt {
String file = "";
private static String category="";
public void WriteFile(String s, List<String> list) {
file = "D:/weibo/"+s+".txt";
BufferedWriter write = null;
try {
write = new BufferedWriter(new FileWriter(file));
for (int i = 0; i < list.size(); i++) {
write.write("@" + list.get(i));
write.newLine();
}
write.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
WriteTxt wt = new WriteTxt();
WriteTxt.category="关键字";
DB db=new DB();
wt.WriteFile(category, db.getAll(category));
}
}
数据库两张表
CREATE TABLE `category` (
`Id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=75 DEFAULT CHARSET=utf8;
CREATE TABLE `users` (
`Id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
`state` varchar(255) default '0',
`categoryId` int(11) default NULL,
PRIMARY KEY (`Id`),
KEY `categoryId` (`categoryId`),
CONSTRAINT `users_ibfk_1` FOREIGN KEY (`categoryId`) REFERENCES `category` (`Id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=22273 DEFAULT CHARSET=utf8;
用了下几分钟可以抓一千个号,我已经抓了两万多个了,不过最近新浪改版了,得重新设计程序 了
标签: #javapost模拟