Der einfachste Weg, SSL-Seite über einen Proxy in Java zu holen
-
05-09-2019 - |
Frage
Ich möchte eine SSL-Seite in Java holen. Das Problem ist, dass ich gegen einen http-Proxy authentifizieren müssen.
So möchte ich eine einfache Möglichkeit, diese Seite zu holen. Ich versuchte, die Apache Commons Httpclient, aber es ist zu viel Aufwand für mein Problem.
habe ich versucht, dieses Stück Code, aber es gibt keine Authentifizierungs Aktion enthalten:
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);
}
}
}
Kann jemand einige Informationen geben, wie es auf einfache Art und Weise zu implementieren?
Vielen Dank im Voraus
Lösung
org.apache.commons.httpclient.HttpClient ist dein Freund,
Beispielcode von 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();
}
Andere Tipps
Sie benötigen einen java einzustellen. net.Authenticator , bevor Sie die Verbindung öffnen:
...
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");
...
}
Ihr Authenticator zu entfernen, nachdem Sie fertig sind, rufen Sie den folgenden Code ein:
Authenticator.setDefault(null);
Der Authenticator in Java SE 6 unterstützt HTTP Basic
, HTTP Digest
und NTLM
. Weitere Informationen finden Sie in der Http-Authentifizierung Dokumentation bei sun.com
mit Apache commons-http-Client 4: Sie werden eine Menge Beispiele @ http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/
und insbesondere http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientProxyAuthentication.java