有人可以帮助我。我正在使用下面的代码来访问我通过浏览器访问的网页,其中包含一些用户密码验证。 但是当我尝试相同的时候我得到了403异常,我这样做错了吗?
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.PasswordAuthentication;
import java.net.Authenticator;
import java.net.InetAddress;
import java.lang.reflect.Proxy;
import java.lang.System;
import sun.misc.*;
import java.net.HttpURLConnection;
public class summa {
//static final String kuser = " "; // your account name
static final String kpass = " "; // your password for the account
//static class MyAuthenticator extends Authenticator {
//public PasswordAuthentication getPasswordAuthentication() {
// I haven't checked getRequestingScheme() here, since for NTLM
// and Negotiate, the usrname and password are all the same.
//System.err.println("Feeding username and password for " + //getRequestingScheme());
//return (new PasswordAuthentication(kuser, kpass.toCharArray()));
//}
//}
public static void main(String[] args) {
try {
URL google = new URL("http://www.google.com/");
HttpURLConnection yc =(HttpURLConnection)google.openConnection();
System.setProperty("http.proxyHost","171.160.82.70") ;
System.setProperty("http.proxyPort", "80") ;
String userPassword = " ";
String encoding = new sun.misc.BASE64Encoder().encode (userPassword.getBytes());
//Authenticator.setDefault(new MyAuthenticator());
yc.setRequestProperty ("Authorization", "Basic " + encoding);
yc.setRequestProperty(userPassword, encoding);
yc.addRequestProperty("User-Agent","ie-7 (compatible; MSIE 6.0; Windows NT 5.0)");
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine="s";
inputLine=in.readLine();
System.out.println(inputLine);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}