【问题标题】:java.io.exception...http exception 403java.io.exception...http 异常 403
【发布时间】:2011-05-28 16:07:01
【问题描述】:

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

【问题讨论】:

  • 请格式化您的代码,您是否考虑使用经过验证的 HTTP 客户端库,例如 hc.apache.org/httpcomponents-client-ga/index.html
  • 如果您尝试使用上述代码访问 Google,请注意:Google 允许某些用户代理访问,Java 可能就是其中之一。
  • No.i 刚刚添加了 google 作为实例。它真的会随着我们使用的网址而变化吗?如果是这样,有没有办法检查它?
  • 如果您想确保不会因此而被拒绝,请将您的 User-Agent 更改为 Firefox 字符串之类的内容。是的,谷歌是一个非常糟糕的例子,因为如果你不使用他们的 API,他们对 UA 很挑剔。
  • @jCoder:你不是说禁止吗?

标签: java authentication http-status-code-403


【解决方案1】:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    • 2023-01-26
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    • 2016-06-15
    相关资源
    最近更新 更多