Connection au travers un proxy
J'ai une classe URLUtils qui va lire l'url http://www.yahoo.com. elle est executée comme suis :
java -Dhttp.proxyHost=eclcache1.ec.goodyear.com -Dhttp.proxyPort=8080 -Dhttp.proxyUser=NGIB962 -Dhttp.proxyPassword=xxx URLUtils
L'execution donne l'erreur suivante:
java.io.IOException: Server returned HTTP response code: 407 for URL: http://www.yahoo.com
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1170)
at URLUtils.dump(URLUtils.java:26)
at URLUtils.main(URLUtils.java:7)
Error 407 correspond à un probleme d'authentification.
Pourtant je suis sur de mes paramètres proxy
Voici le code de la classe :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| import java.net.*;
import java.io.*;
import java.util.Properties;
public class URLUtils {
public static void main(String s[]) {
URLUtils.dump("http://www.yahoo.com");
System.out.println("**************");
}
public static void dump(String URLName){
try {
DataInputStream di = null;
FileOutputStream fo = null;
byte [] b = new byte[1];
// PROXY
URL u = new URL(URLName);
HttpURLConnection con = (HttpURLConnection) u.openConnection();
//
// it's not the greatest idea to use a sun.misc.* class
// Sun strongly advises not to use them since they can
// change or go away in a future release so beware.
//
di = new DataInputStream(con.getInputStream());
while(-1 != di.read(b,0,1)) {
System.out.print(new String(b));
}
}
catch (Exception e) {
e.printStackTrace();
}
}
} |