Java to write network applications, the local area network has a proxy server, how to traverse the proxy server?
-------------------------------------------------- -------------
Add Properties to the main method
systemProperties = System.getProperties();
systemProperties.setProperty("http.proxyHost", "*.*.*.*");
systemProperties. setProperty("http.proxyPort", "****");
---------------------------------- -----------------------------
If you need a user and password, you also need to inherit the Authenticator class and implement the getPasswordAuthentication() method. for verification.
-------------------------------------------------- -------------
import java.net.*;
/****************************
** *
*** Username Authentication
***
** 2003.10.10
**************************
*/
public class MyAuthenticator extends Authenticator
{
private String username,password;
public MyAuthenticator(String username,String password)
{
this.username=username;
this.password=password;
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username,password.toCharArray());
}
}//end MyAuthenticator
import java.net.*;
/********************************
**
** Implement secondary proxy configuration
**
** 2003.10.10
*** ****************************
*/
public class ProxyConfiguration
{
//Proxy setting variable
private String socksProxyHost = "202.119.24.34";//socks proxy
private String socksProxyPort = "1080";//socks proxy port
private String httpProxyHost = "211.167.0.131";//http proxy
private String httpProxyPort = "80";//http proxy port
private String username = "aaaaaa";/ /usernameprivate
String password = "123456"; //password
public ProxyConfiguration()
{
init();
}
public void init()
{
System.getProperties().setProperty( "socksProxyHost",socksProxyHost);
System.getProperties().setProperty( "socksProxyPort",socksProxyPort);
System.getProperties().setProperty( "http.proxyHost",httpProxyHost);
System.getProperties().setProperty( "http.proxyPort",httpProxyPort);
confirmUser();
}
public void setSocksProxyHost(String socksProxyHost) {
this.socksProxyHost = socksProxyHost;
System.getProperties().setProperty( "socksProxyHost",socksProxyHost);
}
public String getSocksProxyHost() {
return this.socksProxyHost;
}
public void setHttpProxyHost(String httpProxyHost) {
this.httpProxyHost = httpProxyHost;
System.getProperties().setProperty( "http.proxyHost",httpProxyHost);
}
public String getHttpProxyHost() {
return this.httpProxyHost;
}
public void setSocksProxyPort(String socksProxyPort) {
this.socksProxyPort = socksProxyPort;
System.getProperties().setProperty( "socksProxyPort",socksProxyPort);
}
public String getSocksProxyPort() {
return this.socksProxyPort;
}
public void setHttpProxyPort(String httpProxyPort) {
this.httpProxyPort = httpProxyPort;
System.getProperties().setProperty( "http.proxyPort",httpProxyPort);
}
public String getHttpProxyPort() {
return this.httpProxyPort;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return this.password;
}
public void confirmUser() {
Authenticator.setDefault(new MyAuthenticator(this.username,this.password));
}
}
-------------------------------------------------- -------------
Add Properties to the main method
systemProperties = System.getProperties();
systemProperties.setProperty("http.proxyHost", "*.*.*.*");
systemProperties. setProperty("http.proxyPort", "****");
---------------------------------- -----------------------------
If you need a user and password, you also need to inherit the Authenticator class and implement the getPasswordAuthentication() method. for verification.
-------------------------------------------------- -------------
import java.net.*;
/****************************
** *
*** Username Authentication
***
** 2003.10.10
**************************
*/
public class MyAuthenticator extends Authenticator
{
private String username,password;
public MyAuthenticator(String username,String password)
{
this.username=username;
this.password=password;
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username,password.toCharArray());
}
}//end MyAuthenticator
import java.net.*;
/********************************
**
** Implement secondary proxy configuration
**
** 2003.10.10
*** ****************************
*/
public class ProxyConfiguration
{
//Proxy setting variable
private String socksProxyHost = "202.119.24.34";//socks proxy
private String socksProxyPort = "1080";//socks proxy port
private String httpProxyHost = "211.167.0.131";//http proxy
private String httpProxyPort = "80";//http proxy port
private String username = "aaaaaa";/ /usernameprivate
String password = "123456"; //password
public ProxyConfiguration()
{
init();
}
public void init()
{
System.getProperties().setProperty( "socksProxyHost",socksProxyHost);
System.getProperties().setProperty( "socksProxyPort",socksProxyPort);
System.getProperties().setProperty( "http.proxyHost",httpProxyHost);
System.getProperties().setProperty( "http.proxyPort",httpProxyPort);
confirmUser();
}
public void setSocksProxyHost(String socksProxyHost) {
this.socksProxyHost = socksProxyHost;
System.getProperties().setProperty( "socksProxyHost",socksProxyHost);
}
public String getSocksProxyHost() {
return this.socksProxyHost;
}
public void setHttpProxyHost(String httpProxyHost) {
this.httpProxyHost = httpProxyHost;
System.getProperties().setProperty( "http.proxyHost",httpProxyHost);
}
public String getHttpProxyHost() {
return this.httpProxyHost;
}
public void setSocksProxyPort(String socksProxyPort) {
this.socksProxyPort = socksProxyPort;
System.getProperties().setProperty( "socksProxyPort",socksProxyPort);
}
public String getSocksProxyPort() {
return this.socksProxyPort;
}
public void setHttpProxyPort(String httpProxyPort) {
this.httpProxyPort = httpProxyPort;
System.getProperties().setProperty( "http.proxyPort",httpProxyPort);
}
public String getHttpProxyPort() {
return this.httpProxyPort;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return this.password;
}
public void confirmUser() {
Authenticator.setDefault(new MyAuthenticator(this.username,this.password));
}
}