HttpRestClient() via Proxy?

96 Views Asked by At

I am playing around and trying to learn how API works in java, currently messing around with the Reddit one: https://github.com/karan/jReddit

Code that I am trying out which works just fine: https://github.com/karan/jReddit/blob/master/src/main/java/examples/UpvoteExample.java

I guess that this is where the connection/login is made (correct me if I am wrong):

User user = new User(restClient, Authentication.getUsername(), Authentication.getPassword());
user.connect();

Submissions subms = new Submissions(restClient, user);
MarkActions submAct = new MarkActions(restClient, user);

Would it be possible to connect via a Proxy in the code above?

1

There are 1 best solutions below

0
On

You can try this

        final RequestConfig globalConfig = RequestConfig.custom()
                .setCookieSpec(CookieSpecs.IGNORE_COOKIES)
                .setConnectionRequestTimeout(10000).build();
        HttpHost proxy = new HttpHost("YOUR_PROXY_IP", YOUR_PROXY_PORT);
        final HttpClient httpClient = HttpClients.custom()
                .setProxy(proxy)
                .setDefaultRequestConfig(globalConfig).build();
        final ResponseHandler<Response> responseHandler = new RestResponseHandler();
        final RestClient restClient = new HttpRestClient(httpClient, responseHandler);
        restClient.setUserAgent("bot/1.0 by name");

        // Connect the user 
        User user = new User(restClient, Authentication.getUsername(), Authentication.getPassword());