如何检查 Java 中的代理是否正常工作?

发布于 2024-12-13 18:09:50 字数 417 浏览 0 评论 0原文

我搜索了 google、这个网站和 JavaRanch,但找不到答案。

我的程序需要从选定的文件中获取代理(我使用 java gui FileChooser 类和 RandomAccessFile 完成了该操作)

然后我需要从 txt 文件中第一个文件开始验证代理。它将尝试连接到某个站点或端口以验证连接是否成功。如果连接成功(我得到了肯定的响应),它将将该代理添加到代理列表中,然后获取并检查列表中的下一个直到完成。

我知道该怎么做,但我遇到了一个小问题。我的问题是这个过程需要独立于连接速度,因为有人可能为要处理的连接设置 15000(毫秒)超时并设置 100 个线程,然后由于连接太慢,所有代理都不会工作。

我听说过一种叫做 ping 的方法来检查代理,但我不知道如何在 java 中使用它。

谁能给我解决方案或至少我可以使用的课程。

I searched google, this site and JavaRanch and I can not find an answer.

My program needs to obtain proxies from a selected file(I got that done using java gui FileChooser class and RandomAccessFile)

Then I need to verify the proxies starting with the one that is first in the txt file. It will try to connect to some site or port to verify if the connection was successful.If the connection was successful (I got a positive response) it will add the proxy to a list of proxies and then get and check next one in the list until it is done.

I know how to do this but I got a little problem. My Problem is that this process needs to be independent of connection speed because someone may set 15000(milliseconds) timeout for the connection to be dealt with and set 100 threads and then none of the proxies would come out working because connection is too slow.

I heard of a method called pinging to check proxies,but I do not know how to use it in java.

Could anyone give me solution or at least classes I could use.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦明 2024-12-20 18:09:51

好吧,我找到了一个解决方案,而且很简单。

我使用了 InetAddress.isReachable() 方法以及 Apache 的一些 HttpClient 。对于代理检查,我使用了 blanksite.com 因为我需要的是检查可连接性而不是代理的速度。

所以这里是代码(包括来自文件的输入,但它还不是 gui):

/* compile with 
   java -cp .;httpclient-4.5.1.jar;httpcore-4.4.3.jar ProxyMat
   run with
   java -cp .;httpclient-4.5.1.jar;httpcore-4.4.3.jar;commons-logging-1.2.jar ProxyMat
   put one proxy to check per line in the proxies.txt file in the form
   some.host.com:8080
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.net.InetAddress;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;

public class ProxyMat{

    File file=null;
    static RandomAccessFile read=null;      
    public ProxyMat(){
        file=new File("proxies.txt");
        try {
            read=new RandomAccessFile(file,"rw");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void checkproxies(){
        try{
            String line;
            for(int i=0;i<25;i++){
                if((line=read.readLine())!=null){
                    System.out.println(line);
                    String[] hp=line.split(":");
                    InetAddress addr=InetAddress.getByName(hp[0]);
                    if(addr.isReachable(5000)){
                        System.out.println("reached");
                        ensocketize(hp[0],Integer.parseInt(hp[1]));
                    }
                }
            }
        }catch(Exception ex){ex.printStackTrace();}
    }



    public void ensocketize(String host,int port){
        try{
            File pros=new File("working.txt");
            HttpClient client=new DefaultHttpClient();
            HttpGet get=new HttpGet("http://blanksite.com/");
            HttpHost proxy=new HttpHost(host,port);
            client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
            client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 15000);
            HttpResponse response=client.execute(get);
            HttpEntity enti=response.getEntity();
            if(response!=null){
                System.out.println(response.getStatusLine());
                System.out.println(response.toString());
                System.out.println(host+":"+port+" @@ working");
            }
        }catch(Exception ex){System.out.println("Proxy failed");}
    }

    public static void main(String[] args){
        ProxyMat mat=new ProxyMat();
        mat.checkproxies();
    }
}

Ok I found a solution and it is easy.

What I used it InetAddress.isReachable() method along with some HttpClient by Apache. For proxy checking I used blanksite.com because all I need is check connectability and not speed of proxies.

So here is the code(Including input from file, but it is not gui, YET):

/* compile with 
   java -cp .;httpclient-4.5.1.jar;httpcore-4.4.3.jar ProxyMat
   run with
   java -cp .;httpclient-4.5.1.jar;httpcore-4.4.3.jar;commons-logging-1.2.jar ProxyMat
   put one proxy to check per line in the proxies.txt file in the form
   some.host.com:8080
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.net.InetAddress;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;

public class ProxyMat{

    File file=null;
    static RandomAccessFile read=null;      
    public ProxyMat(){
        file=new File("proxies.txt");
        try {
            read=new RandomAccessFile(file,"rw");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void checkproxies(){
        try{
            String line;
            for(int i=0;i<25;i++){
                if((line=read.readLine())!=null){
                    System.out.println(line);
                    String[] hp=line.split(":");
                    InetAddress addr=InetAddress.getByName(hp[0]);
                    if(addr.isReachable(5000)){
                        System.out.println("reached");
                        ensocketize(hp[0],Integer.parseInt(hp[1]));
                    }
                }
            }
        }catch(Exception ex){ex.printStackTrace();}
    }



    public void ensocketize(String host,int port){
        try{
            File pros=new File("working.txt");
            HttpClient client=new DefaultHttpClient();
            HttpGet get=new HttpGet("http://blanksite.com/");
            HttpHost proxy=new HttpHost(host,port);
            client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
            client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 15000);
            HttpResponse response=client.execute(get);
            HttpEntity enti=response.getEntity();
            if(response!=null){
                System.out.println(response.getStatusLine());
                System.out.println(response.toString());
                System.out.println(host+":"+port+" @@ working");
            }
        }catch(Exception ex){System.out.println("Proxy failed");}
    }

    public static void main(String[] args){
        ProxyMat mat=new ProxyMat();
        mat.checkproxies();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文