Question

Is there a way to make get & put calls over HTTP in java ? I also need to automate any user inputs like a button click on the target web-page(any web-page, not just yahoo finance)

I tried using the apache commons library & couldn't quite crack it:

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class Fin {

    /**
     * @param args
     */
    public static void main(String[] args) {
        DefaultHttpClient httpclient = new DefaultHttpClient();

        HttpGet httpget = new HttpGet("http://finance.yahoo.com");
        try {
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            httpget.releaseConnection();
        }
    }

}

I keep getting 'java.net.ConnectException: Connection refused', though i can see it in the browser.

Was it helpful?

Solution

If you really want to automate browser-based interactions, you could go further and use Watij, which runs a browser via the JVM and is driven via a browser-based API (I.e. you identify the button you want to press and it will actually do this)

Otherwise a library like the one you've identified will normally work. You have to watch out for client-side JavaScript interactions driving the requests, and configure proxies etc (I suspect this is your problem in the above)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top