Basic Authentication
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
System.out.printf( "url=%s, host=%s, ip=%s, port=%s%n",
getRequestingURL(), getRequestingHost(),
getRequestingSite(), getRequestingPort() );
return new PasswordAuthentication("user", "pass".toCharArray());
}
});
URL url = new URL("http://www.domain.de/user/" );
System.out.println(
new Scanner(url.openStream() ).
useDelimiter( "\\Z" ).next() ); |
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
System.out.printf( "url=%s, host=%s, ip=%s, port=%s%n",
getRequestingURL(), getRequestingHost(),
getRequestingSite(), getRequestingPort() );
return new PasswordAuthentication("user", "pass".toCharArray());
}
});
URL url = new URL("http://www.domain.de/user/" );
System.out.println(
new Scanner(url.openStream() ).
useDelimiter( "\\Z" ).next() );
Content einer URL lesen
StringBuffer sb = null;
try {
URL url = new URL("https://localhost:80/index.htm");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String row;
while((row = in.readLine()) != null) {
// readLine() entfernt das/die newline-zeichen
sb.append(row);
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
System.out.println(sb.toString()); |
StringBuffer sb = null;
try {
URL url = new URL("https://localhost:80/index.htm");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String row;
while((row = in.readLine()) != null) {
// readLine() entfernt das/die newline-zeichen
sb.append(row);
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
System.out.println(sb.toString());
URL encoding and decoding
Proxy-Authentifizierung
Möglichkeit 1:
System.setProperty( "http.proxyUserName", proxyUser );
System.setProperty( "http.proxyPassword", proxyPass ); |
System.setProperty( "http.proxyUserName", proxyUser );
System.setProperty( "http.proxyPassword", proxyPass );
Möglichkeit 2:
URLConnection conn = url.openConnection();
String base64 = "Basic " +
new sun.misc.BASE64Encoder().encode((user + ":" +
passwd).getBytes() );
conn.setRequestProperty("Proxy-Authorization", "Basic " +
new sun.misc.BASE64Encoder().encode((proxyUser + ":" +
proxyPass).getBytes()) );
conn.connect();
InputStream in = conn.getInputStream(); |
URLConnection conn = url.openConnection();
String base64 = "Basic " +
new sun.misc.BASE64Encoder().encode((user + ":" +
passwd).getBytes() );
conn.setRequestProperty("Proxy-Authorization", "Basic " +
new sun.misc.BASE64Encoder().encode((proxyUser + ":" +
proxyPass).getBytes()) );
conn.connect();
InputStream in = conn.getInputStream();
registerOutParameter
CallableStatement statement = connection.prepareCall(
"{call INSERT INTO tableA (some_id) VALUES (1) RETURNING ROWID INTO ? }");
statement.registerOutParameter( 1, Types.VARCHAR );
int updateCount = statement.executeUpdate();
if (updateCount > 0) {
return statement.getString(1);
} |
CallableStatement statement = connection.prepareCall(
"{call INSERT INTO tableA (some_id) VALUES (1) RETURNING ROWID INTO ? }");
statement.registerOutParameter( 1, Types.VARCHAR );
int updateCount = statement.executeUpdate();
if (updateCount > 0) {
return statement.getString(1);
}