[html] view plain copy
print?- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.SocketException;
- import java.net.URL;
- import java.util.Properties;
- public class Test2 {
- public static void main(String[] args) {
- // getHost("http://www.youdaili.net/Daili/http/2572_4.html") ;
- File f = null ;
- BufferedReader br = null ;
- FileReader fr = null ;
- String s = "" ;
- String[] host = new String[2] ;
- try {
- f = new File("E:\\host.txt") ;
- fr = new FileReader(f) ;
- br = new BufferedReader(fr) ;
- s = br.readLine() ;
- while(s!=null) {
- System.out.println("换一次ip" + s);
- host = s.split(":") ;
- //******************************
- Properties sys = System.getProperties();
- sys.put("proxySet","true");
- sys.put("proxyHost",host[0]);
- sys.put("proxyPort",host[1]);
- System.setProperties(sys);
- //******************************
- getData() ;
- s = br.readLine() ;
- }
- } catch (Exception e) {
- System.out.println(e);
- }finally {
- try {
- if(br != null) {
- br.close();
- }
- if(fr != null) {
- fr.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public static void getData() {
- System.out.println("进入getData");
- URL url ;
- int responseCode ;
- HttpURLConnection httpURLConnection ;
- String str = "" ;
- BufferedReader br = null ;
- StringBuffer sb = new StringBuffer() ;
- InputStreamReader isr = null ;
- try {
- for(int i=0;i<200;i++){
- url = new URL("http://www.uggaustralia.cn/thisisugg/api.php?ctrl=page&action=voteWeibo&pid=4218") ;
- httpURLConnection = (HttpURLConnection)url.openConnection() ;
- httpURLConnection.setConnectTimeout(5000);
- httpURLConnection.setReadTimeout(5000);
- httpURLConnection.setRequestProperty("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11") ;
- responseCode = httpURLConnection.getResponseCode() ;
- if(responseCode ==200){
- isr = new InputStreamReader(httpURLConnection.getInputStream(),"GBK") ;
- br = new BufferedReader(isr) ;
- while((str=br.readLine())!=null){
- sb.append(str) ;
- }
- }else{
- throw new SocketException() ;
- }
- System.out.println(sb.toString());
- if(sb.toString().contains("\"data\":2")) {
- break ;
- }
- sb = new StringBuffer() ;
- }
- } catch (Exception e) {
- System.out.println(e);
- }finally {
- try {
- if(br != null) {
- br.close();
- }
- if(isr != null) {
- isr.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 获得有代理网上的ip
- */
- public static void getHost(String dailiURL) {
- URL url ;
- int responseCode ;
- HttpURLConnection httpURLConnection ;
- String str = "" ;
- BufferedReader br = null ;
- StringBuffer sb = new StringBuffer() ;
- InputStreamReader isr = null ;
- try {
- url = new URL(dailiURL) ;
- httpURLConnection = (HttpURLConnection)url.openConnection() ;
- httpURLConnection.setConnectTimeout(5000);
- httpURLConnection.setReadTimeout(5000);
- httpURLConnection.setRequestProperty("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11") ;
- responseCode = httpURLConnection.getResponseCode() ;
- if(responseCode ==200){
- isr = new InputStreamReader(httpURLConnection.getInputStream(),"utf-8") ;
- br = new BufferedReader(isr) ;
- str = br.readLine() ;
- while(br.readLine()!=null){
- if(str.contains("@HTTP#")){
- str = str.replace("<p>", "").trim() ;
- str = str.substring(0, str.indexOf("@")) ;
- str += "\r\n" ;
- sb.append(str) ;
- }
- str = br.readLine() ;
- }
- System.out.println(sb.toString());
- HostwriteToFile(sb.toString()) ;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }finally {
- try {
- if(br != null) {
- br.close();
- }
- if(isr != null) {
- isr.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 把拿到的代理ip写入文件中
- * @param str
- */
- public static void HostwriteToFile(String str) {
- FileWriter fw = null ;
- try {
- fw = new FileWriter("E:\\host.txt",true) ;
- fw.write(str);
- fw.flush();
- fw.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }