Friday, 21 September 2012

How to make a request to a web application with proxy settings set up

Here is the code

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
 * @author visruth
public class HttpClientWithProxySettings {

    private static final HttpClient httpClient = new HttpClient();

    static {
        String proxyAddress = "";
        int proxyPort = 3125;
        String userName = "visruth";
        String password = "visruth";
        setProxyServer(proxyAddress, proxyPort, userName, password);
/*comment the static block if you do not use proxy*/

    public static void setProxyServer(String proxyAddress, int proxyPort, String userName, String password) {
        System.out.println("proxy server");
        final String PROXY_HOST = proxyAddress;
        final int PROXY_PORT = proxyPort;
        HostConfiguration hostConfig = httpClient.getHostConfiguration();
        hostConfig.setProxy(PROXY_HOST, PROXY_PORT);
        Credentials credentials = new UsernamePasswordCredentials(userName, password);
        AuthScope authScope = new AuthScope(PROXY_HOST, PROXY_PORT);
        httpClient.getState().setProxyCredentials(authScope, credentials);

    public static synchronized String getResponse(String url, Properties parameters) {
        GetMethod to make get method request, PostMethod to make post method request type,
        HttpMethod httpMethod = new GetMethod(url);
        String response = "";
        try {
            if (parameters != null) {
                NameValuePair[] nameValuePairs = new NameValuePair[parameters.size()];
                Set keySet = parameters.keySet();
                Iterator iterator = keySet.iterator();
                for (int i = 0; iterator.hasNext(); i++) {
                    String key =;
                    String value = parameters.getProperty(key);
                    NameValuePair nameValuePair = new NameValuePair(key, value);
                    nameValuePairs[i] = nameValuePair;

            if (httpMethod.getStatusCode() == HttpStatus.SC_OK) {
                response = httpMethod.getResponseBodyAsString();
        } catch (IOException e) {
        } finally {

        return response;

    public static void main(String[] args) {

        Properties map = new Properties();
        map.setProperty("fistName", "sony");
        map.setProperty("lastName", "sony");
        String response = getResponse("", map);
/* pass null instead of map if you don't want to pass get/post parameters*/

Download API (jar library)
If you want any java code which you could not find in the internet, please request us as a comment, we will provide you in our next post....
Usage of JSON


  1. This comment has been removed by the author.

  2. This comment has been removed by a blog administrator.