对于 Java 1.2,您只需用 Authenticator.setDefault() 安装一个 Authenticator。这样,当需要认证时,已安装的 Authenticator 的 getPasswordAuthentication() 方法就会被调用,然后您就可以用适当的用户名和口令来设置 PasswordAuthentication 实例。就这么简单。
package cn.arthurs.web;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.util.Properties;
import org.apache.log4j.Logger;
public class WebUnit {
private static final Logger log = Logger.getLogger( WebUnit.class );
/**
* 返回一个网站的源代码,需要传入一个网站的网址
* @param webUrl
* @return
*/
public static String getWebContent(String webUrl){
URL url;
HttpURLConnection conn;
InputStream stream;
StringBuffer text = new StringBuffer();
String temp="";
try {
url = new URL(webUrl);
//如果需要代理服务器
if(false){
Properties prop = System.getProperties();
// 设置http访问要使用的代理服务器的地址
prop.setProperty("http.proxyHost", "bmcdc.bmsa.com");
// 设置http访问要使用的代理服务器的端口
prop.setProperty("http.proxyPort", "1865");
// 设置不需要通过代理服务器访问的主机,可以使用*通配符,多个地址用|分隔
prop.setProperty("http.nonProxyHosts", "localhost|10.4.*.*");
// 设置安全访问使用的代理服务器地址与端口 // 它没有https.nonProxyHosts属性,它按照http.nonProxyHosts 中设置的规则访问
// prop.setProperty("https.proxyHost", "bmccisa-dc02.bmccisa.com");
// prop.setProperty("https.proxyPort", "443");
// 使用ftp代理服务器的主机、端口以及不需要使用ftp代理服务器的主机
// prop.setProperty("ftp.proxyHost", "192.168.0.254");
// prop.setProperty("ftp.proxyPort", "2121");
// prop.setProperty("ftp.nonProxyHosts", "localhost|192.168.0.*"); // socks代理服务器的地址与端口
// prop.setProperty("socksProxyHost", "192.168.0.254");
// prop.setProperty("socksProxyPort", "8000");
// 设置登陆到代理服务器的用户名和密码
Authenticator.setDefault(new MyAuthenticator("ebgqy@bca.com", "f3mj"));
}
conn = (HttpURLConnection) url.openConnection();
conn.connect();
stream = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
while((temp = reader.readLine()) != null){
text.append(temp+"/n");
}
} catch (MalformedURLException e) {
log.error("输入"+webUrl+"有误!!!");
} catch (IOException e) {
log.error(e);
}
return text.toString();
}
public static void main(String[] args){
// System.out.println(WebUnit.getWebContent("http://www.baidu.com"));
}
static class MyAuthenticator extends Authenticator {
private String user = "";
private String password = "";
public MyAuthenticator(String user, String password) {
this.user = user;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password.toCharArray());
}
}
}