Friday 21 September 2012

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

Here is the code





import java.io.IOException;
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 = "192.168.1.100";
        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 = iterator.next().toString();
                    String value = parameters.getProperty(key);
                    NameValuePair nameValuePair = new NameValuePair(key, value);
                    nameValuePairs[i] = nameValuePair;
                }
                httpMethod.setQueryString(nameValuePairs);
            }

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

        return response;
    }


    public static void main(String[] args) {

        Properties map = new Properties();
        map.setProperty("fistName", "sony");
        map.setProperty("lastName", "sony");
        System.out.println(map.size());
        String response = getResponse("http://192.168.1.102:8084/WebApp/forandroid/getDetails", map);
/* pass null instead of map if you don't want to pass get/post parameters*/
        System.out.println(response);
    }
}

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