java.io.exception...http exception 403
Can someone help me.I am using the below piece of code for accessing a webpage for which i had access through a browser with some user password authentication. But when i tried the same i got 403 exception, am i doing this wrong?
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.PasswordAuthentication;开发者_JAVA技巧
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();
}
}
}
See HTTP 403: Forbidden.
精彩评论