Alright well I decided I wanted to widen my learning a bit with java and decided I was going to write a program that had to do with networking side of things.

It took me a little bit of time to come up with something, so I asked my friend and he had an idea for what I should do. That being said, I have a few questions. I'll begin by stating what I want to do and then ask the questions.

The tutorial I have read up on is located at:

http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html

Seems pretty straight forward, which is why I have chosen it.

My friend has a site where he is testing who connects to his site, downloads which files, and at specific times. So he asked if I would write something to help him test it. I agreed as it should be a pretty good learning experience. I have little knowledge on proxies, from what I understand they allow a person to temporarily use another ip address which is what he is looking for. So from what my friend told me, I'm wanting to do the following...

I'm wanting to:

  • URL = textfield1.getText();
  • Open a ProxyList.txt
  • Check for Proxy type and whether its a good or bad proxy
  • If proxy is good, add to an array (so it can be written to a new file later)
  • Change proxy if proxy can't connect after 2 attempts
  • Change proxy if download was successful and download file again from a new proxy till proxyList.txt is empty (about 50 proxies)
  • Use javascript to automatically click download button

Extra options:

  • URL = multiple URLs at a time (perhaps through a list, text file, or arraylist)
  • Different proxy per download
  • Use an int to count unsuccessful attempts
  • Use an int to count successful attempts

A lot of it is covered in the tutorial that I have read, but I'm still looking for advice on how you might go about completing what I'm wanting to do.

* Thus far, I have created a Proxy class that extends ProxySelector (very much like the one in the tutorial/example)

* I have also created the method that handles the bad proxies for two bad connections.

* I have created a nice simlpe GUI for it as well

* I have told him to write the javascript for his site because he is the one who coded it and should know it better than myself. I figure it shouldn't be too hard to implement some javascript in java code. Something like this should work (right?) :

public class RunScriptFile {
  public static void main(String[] args) {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("js");
    try {
      FileReader reader = new FileReader("yourFile.js");
      engine.eval(reader);
      reader.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

My attempt to make my class pull proxies from proxyList.txt looks like this:

HashMap<SocketAddress, InnerProxy> proxies = new HashMap<SocketAddress, InnerProxy>();

OpenProxy(ProxySelector def) throws FileNotFoundException {
    //Save previous default
    defsel = def;
    
    //Populate the HashMap with proxies from text file
    String myAddress;
    String myPort;
    
    BufferedReader reader = new BufferedReader(new FileReader("myDirectory/proxyList.txt"));

        try {
            while (reader.ready()) {
                String proxyLines = reader.readLine();

                    String[] data = proxyLines.split(":" , 2);
                    myAddress = data[0];
                    myPort = data[1];
                    int myPortNumber = Integer.parseInt(myPort);
                                       
                    InnerProxy i = new InnerProxy(new InetSocketAddress(myAddress , myPortNumber));
                    proxies.put(i.address(), i);
            }
    } catch (Exception e) {
    e.printStackTrace();
    }
}

To do list:

  • Make sure I'm adding proxies from txt to HashMap correctly
  • I need to change proxy after successful attempt
  • Add good proxies to arraylist
  • Multiple URLs/downloads at a time
  • Different proxy per link
  • Account for successful and unsuccessful attempts

If anyone wants to help shed some light on the tasks I would appreciate it. Any pointers, code snippets, etc are welcome. Or if you think you have a better alternative to some of the ways I'm doing things let me know.

Dani AI

Generated

Good direction, — you’ve got the right pieces (proxy list, success/fail tracking, GUI). For per-URL proxying avoid a single global selector when you need different proxies per request: create and pass a Proxy instance for each connection and run downloads inside a worker pool so each task can pick its own proxy. Pre-validate proxies with a very short HEAD/GET to a stable host and use aggressive connect/read timeouts so a slow/misbehaving proxy doesn’t stall a worker.

Proxy px = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("1.2.3.4", 8080));
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(px);
conn.setConnectTimeout(5000);
conn.setReadTimeout(10000);
int code = conn.getResponseCode();
// handle stream if code == 200

That per-connection approach gives precise control (use Proxy.Type.SOCKS for SOCKS proxies). Wrap each attempt in try/catch, always close streams, and record success/failure counts atomically. For concurrency use an ExecutorService and a thread-safe proxy pool (ConcurrentLinkedQueue or ConcurrentHashMap for metadata) so workers can atomically claim and return or retire proxies.

A key gap in the thread: running a local JS engine (javax.script) does not execute client-side page scripts in a remote browser — it just evaluates JS locally. If the site needs a DOM-interaction (button click, token generation), use a headless browser (Selenium, HtmlUnit) or reverse-engineer the download endpoint and call it directly. If proxies require auth, supply credentials via java.net.Authenticator. Implement rotation policy: mark a proxy bad after N failures, optionally revive it after a cooldown, and avoid reusing the same proxy for rapid successive downloads to reduce server-side blocks.

Thanks to for the proxy-list tip — vet third-party proxies carefully, never send sensitive data through unknown proxies, and confirm you have permission to stress-test the target site.

I'm surprised there hasn't been a reply yet, perhaps there will be a reply for my next question.

I'm wondering how I would go about opening a url with a different proxy per url?

Hi, I'm not a java expert but I can point an URL where you can download TXT proxy lists for free. Is going to help you in the develop and testing of your project.

By the way, just now that I'm searching for some proxy IPs I see your post, is quite old. Are you still with this develop? May be you have it ready!

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.