Easiest way to fetch SSL page via a proxy in Java
Asked Answered
V

3

6

I would like to fetch a SSL page in Java. The problem is, that I have to authenticate against a http proxy.

So I want a simple way to fetch this page. I tried the Apache Commons httpclient, but it's too much overhead for my problem.

I tried this piece of code, but it does not contain an authentication action:

import java.io.*;
import java.net.*;

public class ProxyTest {

  public static void main(String[] args) throws ClientProtocolException, IOException {

    URL url = new URL("https://ssl.site");
    Socket s = new Socket("proxy.address", 8080);
    Proxy proxy = new Proxy(Proxy.Type.HTTP, s.getLocalSocketAddress());

    URLConnection connection = url.openConnection(proxy);
    InputStream inputStream = connection.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
    String tmpLine = "";

    while ((tmpLine = br.readLine()) != null) {
      System.out.println(tmpLine);
    }

  }
}

Can anyone provide some information how to implement it on an easy way?

Thanks in advance

Vagina answered 19/2, 2009 at 9:39 Comment(0)
B
4

org.apache.commons.httpclient.HttpClient is your friend,

Sample code from http://hc.apache.org/httpclient-3.x/sslguide.html

  HttpClient httpclient = new HttpClient();
  httpclient.getHostConfiguration().setProxy("myproxyhost", 8080);
  httpclient.getState().setProxyCredentials("my-proxy-realm", " myproxyhost",
  new UsernamePasswordCredentials("my-proxy-username", "my-proxy-password"));
  GetMethod httpget = new GetMethod("https://www.verisign.com/");
  try { 
    httpclient.executeMethod(httpget);
    System.out.println(httpget.getStatusLine());
  } finally {
    httpget.releaseConnection();
  }
Bless answered 19/2, 2009 at 9:56 Comment(4)
This piece of code does not work. I included commons-net-2.0, commons-loggin-1.1.1, commons-codec-1.3, httpclient-4.0-beta2, httpmime-4.0-beta2, httpcore-4.0-beta3, http-nio-4.0-beta3 ... Error message: Connot instantiate HttpClient.Vagina
it works for me with commons-httpclient-3.1.jar, commons-codec-1.3.jar, commons-logging-1.1.1.jarBless
Okay, it works, but not well... there's the strange " Credentials cannot be used for NTLM authentication" error...Vagina
hc.apache.org/httpclient-3.x/authentication.html#NTLM I just googled it. it looks like what you are after. Good luck.Bless
C
6

You need to set a java.net.Authenticator before you open your connection:

...

public static void main(String[] args) throws Exception {
    // Set the username and password in a manner which doesn't leave it visible.
    final String username = Console.readLine("[%s]", "Proxy Username");
    final char[] password = Console.readPassword("[%s"], "Proxy Password:");

    // Use a anonymous class for our authenticator for brevity
    Authenticator.setDefault(new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    });

    URL url = new URL("https://ssl.site");
    ...
}

To remove your authenticator after you're finished, call the following code:

Authenticator.setDefault(null);

The authenticator in Java SE 6 supports HTTP Basic, HTTP Digest and NTLM. For more information, see the Http Authentication documentation at sun.com

Cutcherry answered 19/2, 2009 at 9:54 Comment(1)
Little mistake: final char[] password = "".toCharArray();Vagina
B
4

org.apache.commons.httpclient.HttpClient is your friend,

Sample code from http://hc.apache.org/httpclient-3.x/sslguide.html

  HttpClient httpclient = new HttpClient();
  httpclient.getHostConfiguration().setProxy("myproxyhost", 8080);
  httpclient.getState().setProxyCredentials("my-proxy-realm", " myproxyhost",
  new UsernamePasswordCredentials("my-proxy-username", "my-proxy-password"));
  GetMethod httpget = new GetMethod("https://www.verisign.com/");
  try { 
    httpclient.executeMethod(httpget);
    System.out.println(httpget.getStatusLine());
  } finally {
    httpget.releaseConnection();
  }
Bless answered 19/2, 2009 at 9:56 Comment(4)
This piece of code does not work. I included commons-net-2.0, commons-loggin-1.1.1, commons-codec-1.3, httpclient-4.0-beta2, httpmime-4.0-beta2, httpcore-4.0-beta3, http-nio-4.0-beta3 ... Error message: Connot instantiate HttpClient.Vagina
it works for me with commons-httpclient-3.1.jar, commons-codec-1.3.jar, commons-logging-1.1.1.jarBless
Okay, it works, but not well... there's the strange " Credentials cannot be used for NTLM authentication" error...Vagina
hc.apache.org/httpclient-3.x/authentication.html#NTLM I just googled it. it looks like what you are after. Good luck.Bless
A
0

with apache commons-http-client 4 : you'll find a lot of examples @ https://github.com/apache/httpcomponents-client/tree/master/httpclient5/src/test/java/org/apache/hc/client5/http/examples

and especially https://github.com/apache/httpcomponents-client/blob/master/httpclient5/src/test/java/org/apache/hc/client5/http/examples/ClientProxyAuthentication.java

Anglomania answered 19/2, 2009 at 10:20 Comment(2)
The code provided in the second link does not work, because there's a strange logging message, that the NTLM auth method is not supported.Vagina
Because of the logging library, there's no stacktrace. I think it's my problem and not the initial question anymore. But thanks for your help :)Vagina

© 2022 - 2024 — McMap. All rights reserved.