2016-12-28 62 views
0

所以即時嘗試在個人頁面上生成視圖幾個小時。我不明白爲什麼它不工作。所以我現在的代碼:(是的,即時使用spigot API,但現在並不重要)。Java - 使用Proxys生成視圖

@Override 
public boolean onCommand(CommandSender cs, Command cmd, String label, String[] args) { 
    if(args.length == 2){ 
     String link = args[0]; 
     String views = args[1]; 
     int view = 0; 
     try{ 
      view = Integer.parseInt(views); 
     }catch(Exception ex){ 
      ex.printStackTrace(); 
      cs.sendMessage("" + views + " is not a number."); 
     } 

      execute(link, view); 


     }else{ 
     System.out.println("More arguments"); 
    } 
    return true; 
} 
private int o = 1; 
private void execute(String link, int views){ 

    new BukkitRunnable() { 
     int i = views; 
     @Override 
     public void run() { 
      i--; 


      try{ 
       String fullProxy = Viewbot.getInstance().getProxys().get(i); 
       String proxy, port; 
       String[] fullProx; 
       fullProx = fullProxy.split(":"); 
       proxy = fullProx[0]; 
       port = fullProx[1]; 




      /* Proxy proxyCon = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy, Integer.parseInt(port))); 
       HttpURLConnection connection =(HttpURLConnection)new URL(link).openConnection(proxyCon); 
       connection.setDoOutput(true); 
       connection.setDoInput(true); 
       connection.setRequestProperty("Content-type", "text/xml"); 
       connection.setRequestProperty("Accept", "text/xml, application/xml"); 
       connection.setRequestMethod("POST");   
       */ 
       Properties systemProperties = System.getProperties(); 
       systemProperties.setProperty("http.proxyHost",proxy); 
       systemProperties.setProperty("http.proxyPort",port); 
       systemProperties.setProperty("https.proxyHost",proxy); 
       systemProperties.setProperty("https.proxyPort",port); 
        URL server = new URL(link); 
        HttpURLConnection connection = (HttpURLConnection)server.openConnection(); 
        connection.setUseCaches(false); 
        connection.connect(); 
        System.out.println("Respone: "+ connection.getResponseMessage()); 

        System.out.println("View #" + o + " gesendet!"); 
        o++; 
          connection.disconnect(); 


          System.out.println("Proxy: " + proxy + " Port: " + port); 
          System.out.println("System using: " + systemProperties.getProperty("http.proxyHost") + ":" + systemProperties.getProperty("http.proxyPort")); 
          System.out.println("Proxy: " + connection.usingProxy()); 


      }catch(Exception ex){ 
       Properties systemProperties = System.getProperties(); 
       ex.printStackTrace(); 
       Viewbot.getInstance().failProxys.add(systemProperties.getProperty("http.proxyHost") + ":" + systemProperties.getProperty("http.proxyPort")); 
      } 
      if(i == 5){ 
       for(int i = 0; i < Viewbot.getInstance().failProxys.size(); i++){ 
        String failed = Viewbot.getInstance().failProxys.get(i); 
        Viewbot.getInstance().getProxys().remove(failed); 


       } 
       Path file = Paths.get(Viewbot.getInstance().getDataFolder() + "/HTTPS.txt"); 
       try { 
        Files.write(file, Viewbot.getInstance().getProxys(), Charset.forName("UTF-8")); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       System.out.println("File saved"); 
      } 
      if(i == 0){ 
       cancel(); 
      } 

     } 
    }.runTaskTimerAsynchronously(Viewbot.getInstance(), 0, 2); 




} 

的是整個事情的樣本輸出:

[00:34:45 INFO]: Response: OK 
[00:34:45 INFO]: View #430 sent! 
[00:34:45 INFO]: Proxy: 41.76.44.76 Port: 3128 
[00:34:45 INFO]: System using: 45.63.16.86:8080 
[00:34:45 INFO]: Proxy: false 

輸出現在是:

[00:57:58 INFO]: Proxy: 36.80.34.225 Port: 3128 
[00:57:58 INFO]: System using: 113.18.193.26:8000 
[00:57:58 INFO]: Proxy: true 
[00:57:58 INFO]: Respone: OK 

但是當你看這裏:http://84.200.122.107/它不添加視圖..

所以它使用不同的IP地址來連接。什麼錯誤比? 這也表明它不使用代理,這應該是錯誤的。 感謝您的任何幫助。

+0

systemProperties.setProperty( 「http.proxyHost」,代理);是JVM範圍的。你有沒有另一個線程設置不同的值? –

+0

我不是那整個項目中唯一的一個。 –

回答

0

我想你迭代代理列表,每次調用BukkitRunnable將採取一個新的。

因此,如果在不同線程中調用BukkitRunnable重疊,一個設置值,另一個讀取它。

要使用一個代理每個連接應使用普通的HTTP客戶端,能夠做到這一點:

Common HTTPclient and proxy