下面是我实现的读取网页的代码:(需要代理的,没代理的可以不设置)import java.io.*;
import java.net.*;
public class test{
public static void main(String args[]) throws MalformedURLException,IOException{
String urlString,username,password;System.getProperties().setProperty( "http.proxyHost", "202.119.24.35" );
System.getProperties().setProperty( "http.proxyPort", "8080" );if(args.length!=3){
System.err.println("Usage: java AuthDemo URL username password");
System.exit(-1);
}
urlString=args[0];
username=args[1];
password=args[2];
Authenticator.setDefault(new MyAuthenticator(username,password));
try{
System.out.println(URLEncoder.encode(urlString,"UTF-8"));
}catch(UnsupportedEncodingException e){
System.err.println(e);
}
URL url=new URL(urlString);
InputStream content=(InputStream)url.getContent();
BufferedReader in=new BufferedReader(new InputStreamReader(content));
String line;
StringBuffer lineBuf=new StringBuffer("");
//FileWriter out=new FileWriter(new File("F:\\URL.txt"));
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("F:\\URL.html"))));
while((line=in.readLine())!=null){
//System.out.println(line);
lineBuf.append(line+"\n");
}
out.write(lineBuf.toString());
out.flush();
out.close();System.out.println("Done.");
}//end mainstatic class MyAuthenticator extends Authenticator{
private String username,password;public MyAuthenticator(String username,String password){
this.username=username;
this.password=password;
}protected PasswordAuthentication getPasswordAuthentication(){
System.out.println("Requesting Host     :"+getRequestingHost());
System.out.println("Requesting Port     :"+getRequestingPort());
System.out.println("Requesting Prompt   :"+getRequestingPrompt());
System.out.println("Requesting Protocol :"+getRequestingProtocol());
System.out.println("Requesting Scheme   :"+getRequestingScheme());
System.out.println("Requesting Site     :"+getRequestingSite());return new PasswordAuthentication(username,password.toCharArray());
}
}//end MyAuthenticator
}//end testwhile