There are also some articles on the Internet, but most of the proxy involves authorized verification,
The main problem is to use the use of Authenticator.SetDefault, and the Base64 encoding problem
The code is the most unpredictable document, and the principle of implementation is no longer explained. Please see the code to understand.
If you reprint, please indicate the source.
Java Sample Codes That Access Outer Internet THROUGH Proxy Which Require UserName and Password .use Inner instead of extends is the Most Different
Chimae@cnjsp.org
Package org.chimae.net;
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.passwordAuthentication;
Import java.net.URL;
/ **
* @Author chimae@cnjsp.org
* /
Public class proxyconntest {
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 ("http.proxytype", "4");
System.SetProperty ("http.proxyport", integer.tostring (port));
System.SetProperty ("http.proxyhost", host);
System.SetProperty ("http.proxyset", "true");
}
Public static void main (string [] args) throws oException {
String Url = "http://java.sun.com/";
String proxy = "YourProxy";
INT port = 8080;
String UserName = "UserName";
String password = "password";
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 (IS));
While ((curline = reader.readline ())! = null) {
CONTENT = CURLINE;
}
System.out.println ("Content =" Content);
Is.close ();
}
}