import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.CookieStore;
import java.net.HttpCookie;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class MainClass implements Runnable {
String url = "http://wx.ydswt.com/wx/hykxx-";
int index = 0;
HttpURLConnection huc;
InputStream is;
BufferedReader reader;
static CookieManager manager;
static String oldUrl = "http://wx.ydswt.com/wx/hyk-404.html?wxid=oQhWgtx5SHjuTJPmy7UI_sbt4sAg&wid=327#mp.weixin.qq.com";
static String cookieStr;
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
CookieManager manager = new CookieManager();
CookieHandler.setDefault(manager);
URL url = new URL(oldUrl);
URLConnection connection = url.openConnection();
Object obj = connection.getContent();
url = new URL(oldUrl);
connection = url.openConnection();
obj = connection.getContent();
CookieStore cookieJar = manager.getCookieStore();
List<HttpCookie> cookies = cookieJar.getCookies();
for (HttpCookie cookie : cookies) {
cookieStr = cookie.toString();
System.out.println(">>>>>>cookies:"+cookieStr);
}
}catch(Exception e){
e.printStackTrace();
}
new Thread(new MainClass()).start();
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
huc = (HttpURLConnection) new URL(oldUrl).openConnection();
FileWriter fw = new FileWriter(new File("wx_info.txt"));
for (int i = 0; i < 19999; i++) {
String curUrl = url + i + ".html";
huc = (HttpURLConnection) new URL(curUrl).openConnection();
huc.setFollowRedirects(true);
huc.setRequestMethod("GET");
huc.setUseCaches(true);
huc.setRequestProperty("Cookie",cookieStr);
huc.connect();
is = huc.getInputStream();
reader = new BufferedReader(new InputStreamReader(is,
huc.getContentType()
.equals("text-html; charset=gb2312") ? "gb2312"
: "UTF-8"));
String str;
String tempStr = null;
while ((str = reader.readLine()) != null) {
String name = "姓名";
String tel = "电话";
String data = "生日";
String ads = "地址";
str = str.trim();
int startIndex = str.indexOf("value=");
int endIndex = str.indexOf("name=");
if (str.contains(name)){
str = str.substring(startIndex, endIndex);
tempStr = name + str.substring(5).trim() + " ";
}
if (str.contains(tel)){
str = str.substring(startIndex, endIndex);
tempStr += tel + str.substring(5).trim() + " ";
}
if(str.contains(data)){
endIndex = str.indexOf("id=");
str = str.substring(startIndex, endIndex);
tempStr += data + str.substring(5).trim() + " ";
}
if(str.contains(ads)){
str = str.substring(startIndex, endIndex);
tempStr += ads + str.substring(5).trim() + " ";
}
}
if(tempStr != null){
// System.out.println(tempStr.trim());
fw.write(tempStr.trim() + "\n");
fw.flush();
}
}
fw.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
reader.close();
is.close();
huc.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}