Re: How to program with proxy
Arne Vajh=F8j wrote:
moonhk wrote:
Checked with our Administrator, it is proxy server setting. Need to
input username/password in Browser.
BASIC authentication ? (popup in browser ?)
Try with:
Authenticator.setDefault(new MyAuthenticator());
where:
class MyAuthenticator extends Authenticator {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("xxxx", "xxxx".toCharArray());
}
}
but I think it is a bit unusual to use HTTP BASIC authentication
with a proxy server.
Arne
try. But not work. At home below java coding is worked.
C:\Example\javaux\net>java eproxy_tofile
Conneting ... http://www.new-alliance.com/Naam_NAV.htm
java.io.IOException: Server returned HTTP response code: 500 for URL:
http://www.new-alliance.com/Naam_NAV.htm
at
sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnectio=
n.java:800)
at java.net.URL.openStream(URL.java:913)
at eproxy_tofile.main(eproxy_tofile.java:53)
C:\Example\javaux\net>
/*
eproxy_tofile.java
2006/08/14 eric.leung
Environment variable set to
* CLASSPATH=c:\j2sdk1.4.1_01\lib\classes.zip;c:\example;.
*
*/
import java.util.Properties;
import java.io.*;
import java.net.*;
import java.net.Authenticator;
public class eproxy_tofile {
public static void main(String[] args) {
Properties systemSettings = System.getProperties();
class MyAuthenticator extends Authenticator {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("xxx", "xxx".toCharArray());
}
}
// String host,port,userid,passwd;
systemSettings.put("proxySet","true");
systemSettings.put("http.proxyHost",host);
systemSettings.put("http.proxyPort",port);
// System.setProperty("https.proxyUserName",userid);
// System.setProperty("https.proxyPassword",passwd);
System.setProperties(systemSettings);
Authenticator.setDefault(new MyAuthenticator());
try {
String Addr = "http://www.new-alliance.com/Naam_NAV.htm";
URL yahoo = new URL(Addr);
System.out.println("Conneting ... " + Addr);
BufferedReader in = new BufferedReader(new InputStreamReader
(yahoo.openStream()));
String inputLine;
while ((inputLine = in.readLine()) !=null)
System.out.println(inputLine);
in.close();
}
catch (Exception exc) {
exc.printStackTrace();
=09
=09
}
}
}