Hello!

I have many big lists of free proxies on text files. I need to write a small program which can test these proxies if they work or not. To do that i need to send ping on each proxy, but this is not a 100% solution because many proxies block ping. So someone tell me to use "extended ping" mathod. With this method i will be able to send to any site a ping using a proxy. So i can understand if the proxy is alive. After i found the alive proxies, can Java set them as system(operation system's) default proxy? Is this possible on latest version of SunJava for Linux and Windows?

Thank you!

You can use the new (since Java SE 5) Proxy class to define a proxy to use when making a connection to a URL. So you could try using each of your proxies to access (say) google.com

http://download.oracle.com/javase/6/docs/technotes/guides/net/proxies.html
(topic 3 "proxy class) about 1/2 way down)

Once you have a proxy you can use System.setProperty to set a default proxy (same reference as above, section 2)

JamesCherrill i look your links which you gave me. I write this code to test the functions:

import java.net.*;

public class Main {
	public static void main(String[] args) {
		try{
		                SocketAddress addr = new InetSocketAddress("149.169.227.131", 3128);
				Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
				URL url;
				URLConnection conn;
				url = new URL("http://www.google.com/");	
				conn = url.openConnection(proxy);
				if(conn==null)
				{
					System.out.println("con1 eror");
				}
					
				URL url2 = new URL("http://www.google.com/");
				URLConnection conn2 = url2.openConnection(Proxy.NO_PROXY);
				if(conn2==null)
				{
					System.out.println("con2 eror");
				}		
		}
                catch(Exception e){
			System.out.println("error");
		}
		System.out.println("finish");
	}
}

But it never gives me any error. I change the google.com with www.rewrfewfvedfwefeFEWRE.com it does not give me an error. I think this is not a way to understand if the proxy works or not. Am i right ? Can you please help me again ?

I tried something very similar, but I went on to try to read the site via the proxy. Here's the code I used - - the proxy is pretty poor, so this worked about 1/2 the time.

try {
         // create a Proxy at a known internet address (this one is Chinese and slow)...
         SocketAddress proxyAddr = new InetSocketAddress("118.182.20.242", 8080);
         Proxy proxy = new Proxy(Proxy.Type.HTTP, proxyAddr);
         
         // create a URL and try to connect to it via the proxy...
         URL url = new URL("http://google.com"); // hnown high-availability site
         URLConnection urlc = url.openConnection(proxy); // connect via proxy
         
         // read input from site and print it (should be Google's http code)...
         InputStream in = urlc.getInputStream(); 
         char c;
         while ((c = (char) in.read()) != -1) {
            System.out.print(c); 
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.