如何检查代理是否工作在Java? [英] How to check if proxy is working in Java?

查看:210
本文介绍了如何检查代理是否工作在Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我的程序需要从选定的文件获取代理java gui FileChooser类和RandomAccessFile)



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



我知道如何做到这一点,但我有一个小问题。我的问题是,这个procces需要独立于连接速度,因为有人可能设置15000(毫秒)超时连接被处理,并设置100线程,然后没有代理将工作,因为连接太慢。 / p>

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



任何人都可以给我一个olution或至少我可以使用的类。

解决方案

OK我找到了一个解决方案,很容易。



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



这里是代码(包括从文件的输入,但它不是gui,YET):

  / * compile with 
java -cp。; httpclient-4.5.1.jar; httpcore-4.4.3.jar ProxyMat
使用
运行java -cp。; httpclient-4.5.1.jar; httpcore-4.4.3 .jar; commons-logging-1.2.jar ProxyMat
在proxies.txt文件中输入一个代理以检查每行代码
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 = 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 ;
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();
}
}


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)

The 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 succesful.If the connection was succesful (I got a psotive 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 procces needs to be independent of connection speed because someone may set 15000(miliseconds) 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 sa olution or at least classes I could use.

解决方案

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();
    }
}

这篇关于如何检查代理是否工作在Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆