0

有人可以帮助我吗?我正在使用下面的代码来访问我可以通过具有一些用户密码身份验证的浏览器访问的网页。但是当我尝试同样的方法时,我得到了 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(); 
        } 
    } 
}
4

1 回答 1

0

请参阅HTTP 403:禁止

于 2011-05-28T16:27:15.350 回答