Search code examples
androidinternet-connectionproxy-server

how to connect to the internet on my android application while i have a server with a proxy "IP:Port" and asks for a username and password


I am developing an application on android, and there is a proxy server in the company preventing it from accessing the internet.
I recall there are Http methods in java that serves the purpose, I searched for them but with no result.

So what I'd like is how to put the server IP-address and port-number, with my login-username and password, and the domain-name.

Note: I have set my emulator to connect to the internet by going to the APNs and it successfully connects to the internet via the browser.


Solution

  • try to use the following code

    /**
     * A simple example that uses HttpClient to execute an HTTP request
     * over a secure connection tunneled through an authenticating proxy.
     */
    public class ClientProxyAuthentication {
    
        public static void main(String[] args) throws Exception {
    
            DefaultHttpClient httpclient = new DefaultHttpClient();
            try {
                httpclient.getCredentialsProvider().setCredentials(
                        new AuthScope("localhost", 8080),
                        new UsernamePasswordCredentials("username", "password"));
    
                HttpHost targetHost = new HttpHost("www.verisign.com", 443, "https");
                HttpHost proxy = new HttpHost("localhost", 8080);
    
                httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    
                HttpGet httpget = new HttpGet("/");
    
                System.out.println("executing request: " + httpget.getRequestLine());
                System.out.println("via proxy: " + proxy);
                System.out.println("to target: " + targetHost);
    
                HttpResponse response = httpclient.execute(targetHost, httpget);
                HttpEntity entity = response.getEntity();
    
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                if (entity != null) {
                    System.out.println("Response content length: " + entity.getContentLength());
                }
                EntityUtils.consume(entity);
    
            } finally {
                // When HttpClient instance is no longer needed,
                // shut down the connection manager to ensure
                // immediate deallocation of all system resources
                httpclient.getConnectionManager().shutdown();
            }
        }
    }