The user name and password are optional, so updating the Java code won't require updating any LotusScript code using the Java code. The way we've implemented this is by setting up variables, initially empty strings, to hold the user name and password. If the variables are empty strings at the time the URL is read, then nothing extra is done. If the variables have been set through a new method added to the class (setUserNameAndPassword), then those will be used to access the web page. The updated Java here still goes into a Java library, just like the previous tips:
import java.io.*;
import java.net.*;
import java.util.*;
public class GetHTML {
private Vector result = new Vector(); // An array of each line of HTML
private String userName = "";
private String password = "";
public void readHTML(String urlToRead) {
URL url; // The URL to read
HttpURLConnection conn; // The actual connection to the web page
BufferedReader rd; // Used to read results from the web page
String line; // An individual line of the web page HTML
try {
url = new URL(urlToRead);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
if ( (userName.equals("") == false) && (password.equals("") == false) ) {
String userPwd = userName + ":" + password;
String encoding = new sun.misc.BASE64Encoder().encode(userPwd.getBytes());
conn.setRequestProperty("Authorization", "Basic " + encoding);
}
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
result.addElement(line);
}
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public int numLines() {
return result.size();
}
public String getHTML(int pos) {
if (pos < result.size()) {
return (String)result.elementAt(pos);
} else {
return "";
}
}
public void setUserNameAndPassword(String u, String p) {
userName = u;
password = p;
}
}
Friday, September 28, 2007
Subscribe to:
Posts (Atom)
