public static void initProxy(String host, int port, final String username,
final String password) {
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,
new String(password).toCharArray());
}
});
System.setProperty("proxyType", "4");
System.setProperty("proxyPort", Integer.toString(port));
System.setProperty("proxyHost", host);
System.setProperty("proxySet", "true");
}
public static void main(String[] args) throws IOException {
String url = "http://so.csdn.net/";
String proxy = "openproxy.huawei.com";
int port = 8080;
String username = "name";
String password = "pwd";
String curLine = "";
String content = "";
URL server = new URL(url);
initProxy(proxy, port, username, password);
HttpURLConnection connection = (HttpURLConnection) server
.openConnection();
connection.connect();
InputStream is = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
while ((curLine = reader.readLine()) != null) {
content += curLine;
System.out.println( curLine);
}
System.out.println("content= " + content);
is.close();
}