Java HTTP Proxy Authentifizierung

HTTP-Proxy-Authentifikation

Ggf. erfordert der HTTP-Proxy eine Authentifikation. Dabei ist für normale JAVA-Web Zugriffe Folgendes notwendig:

URL url = new URL(theUrl);
URLConnection conn = url.openConnection();
String encoded = getBas64EncodeCredentials(getProxyUser(), getProxyPasswd());
conn.setRequestProperty("Proxy-Authorization", "Basic " + encoded);
conn.connect();

Base64 codierte Credentials:

Liefert die Base64 codierten Credentials des Benutzers.

private String getBas64EncodeCredentials(String user, String passwd) {
	String userPasswd = user + ":" + passwd;
        String encoded = new String(Base64.encode(userPasswd.getBytes()));
        return encoded;
}

Add comment 11.Februar 2008

Picasa Web Album JAVA API

Das Picasa Web Albums API ermöglicht das anzeigen und bearbeiten von Picasa Alben. Es ist Bestandteil des Google Data API. Das Developers Guide beschreibt die Benutzung des API.

Benutzung im Java Desktop Client

Laden der Alben eines Benutzers

PicasawebService picasa = new PicasawebService("exampleCo-exampleApp-1");
String url = "http://picasaweb.google.com/data/feed/api/user/<username>?kind=album";
URL albumsUrl = new URL(url);
// Send the request for the user's albums.
AlbumFeed albums = picasa.getFeed(albumsUrl, AlbumFeed.class);
List<AlbumEntry> entries = albums.getEntries(AlbumEntry.class);
for (AlbumEntry album : entries) {
	//do something with the albums
}

Albumdetails Abrufen und Bilder eines Albums anzeigen

//URL for the AlbumFeed
String url = albumEntry.getFeedLink().getHref() + "?kind=photo";
//the albuFeed contains the album details
AlbumFeed feed = getService().getFeed(new URL(url), AlbumFeed.class);
//a photoEntry contains all photo infos including the urls to the thubnails and image data
List<PhotoEntry> photos = feed.getPhotoEntries();

Benutzung eines HTTP-Proxys

In Java lässt sich die Benutzung eines HTTP-Proxys für http/https Zugriffe wie folgt vornehmen:

Properties systemSettings = System.getProperties();
systemSettings.put("https.proxySet", "true");
systemSettings.put("http.proxyHost", proxyHost);
systemSettings.put("http.proxyPort", proxyPort);
systemSettings.put("proxySet", "true");
systemSettings.put("https.proxyHost", proxyHost);
systemSettings.put("https.proxyPort", proxyPort);
System.setProperties(systemSettings);

Http- und https-Zugriffe sollten nun die Proxy-Einstellungen berücksichtigen.

HTTP-Proxy-Authentifikation

Ggf. erfordert der HTTP-Proxy eine Authentifikation. Dabei ist für normale JAVA-Web Zugriffe Folgendes notwendig:

URL url = new URL(theUrl);
URLConnection conn = url.openConnection();
String encoded = getBas64EncodeCredentials(getProxyUser(), getProxyPasswd());
conn.setRequestProperty("Proxy-Authorization", "Basic " + encoded);
conn.connect();

Googles API kann ebenfalls mit einer Authentifizierung an einem HTTP-Proxy umgehen; dazu ist Folgendes setzen:

String base64encodedCredentials = "Basic " + getBas64EncodeCredentials(getProxyUser(), getProxyPasswd());
picasaService.getRequestFactory().setPrivateHeader("Proxy-Authorization", base64encodedCredentials);

Base64-codierte Credentials:

private String getBas64EncodeCredentials(String user, String passwd) {
	String userPasswd = user + ":" + passwd;
        String encoded = new String(Base64.encode(userPasswd.getBytes()));
        return encoded;
}

1 comment 6.Februar 2008


del.icio.us

Kategoriewolke

authentication Google DATA API