【问题标题】:Java - Generating Views with ProxysJava - 使用代理生成视图
【发布时间】:2016-12-28 23:41:34
【问题描述】:

所以我现在尝试在个人页面上生成视图几个小时。我不明白为什么它不起作用。所以我当前的代码:(是的,我正在使用 spigot API,但现在没关系)。

@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 地址进行连接。比什么错误? 它还表明它没有使用代理,这应该是错误的.. 感谢您的帮助。

【问题讨论】:

  • systemProperties.setProperty("http.proxyHost",proxy);是JVM范围的。你没有另一个线程设置不同的值吗?
  • 整个项目中我还不是唯一一个。

标签: java networking proxy views


【解决方案1】:

我认为你遍历代理列表,每次调用 BukkitRunnable 都会接受一个新的。

因此,如果不同线程中对 BukkitRunnable 的调用重叠,则一个设置值,另一个读取它。

要为每个连接使用一个代理,您应该使用能够做到这一点的通用 http 客户端:

Common HTTPclient and proxy

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 2010-09-30
    • 1970-01-01
    • 2018-11-15
    相关资源
    最近更新 更多