【问题标题】:Server returned HTTP response code: 403 for URL:(How do I fix this?)服务器返回 HTTP 响应代码:URL 403:(我该如何解决这个问题?)
【发布时间】:2012-03-04 09:36:47
【问题描述】:

当我运行下面的代码时,我不断收到上述错误。从我所读到的所有迹象都表明 COOKIES 存在问题。如果我是正确的,我将如何实施 CookieManager 来解决这个问题?或者如果不是 COOKIES 的问题,我该如何解决?

public class Client {

    public Client(){
    }

    String executePost(String targetURL, String urlParameters){
        URL url;
        HttpURLConnection connection = null;

        try{
            //Create connection
            url = new URL(targetURL);
            connection = (HttpURLConnection)url.openConnection();
            connection.setChunkedStreamingMode(0);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", 
                    "application/x-www-form-urlencoded");
            connection.setRequestProperty("Content-Length", "" + 
                    Integer.toString(urlParameters.getBytes().length));
            connection.setRequestProperty("Content-Language", "en-US"); 

            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);

            //send Request 
            DataOutputStream dataout = new DataOutputStream(connection.getOutputStream());
            dataout.writeBytes(urlParameters);
            dataout.flush();
            dataout.close();

            //get response
            InputStream is = connection.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line;
            StringBuffer response = new StringBuffer();

            while((line = br.readLine()) != null){
                response.append(line);
                response.append('\n');

            }
            System.out.println(response.toString());
            br.close();
            return response.toString();
        }catch(Exception e){
            System.out.println("Unable to full create connection");
            e.printStackTrace();
            return null;
        }finally {

            if(connection != null) {
                connection.disconnect(); 
            }
        }
    }
}

【问题讨论】:

  • 可能是您不允许访问该网址...
  • @Tomas 我该如何解决?我正在向位于我的 PUBLIC_HTML 文件夹中的 PHP 文件发送信息。然后 PHP 文件访问 mysql 数据库并返回请求的数据。
  • @Tomas - 从表面上看,这是正确的。但是,它假定服务器正在使用正确的状态码。

标签: java http-status-code-403


【解决方案1】:

我删除了:connection.setChunkedStreamingMode(0);并且代码正常工作

String executePost(String targetURL, String urlParameters){

    URL url;
    HttpURLConnection connection = null;        
    try{
        //Create connection

        url = new URL(targetURL);
        connection = (HttpURLConnection)url.openConnection();

        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", 
                   "application/x-www-form-urlencoded");
        connection.setRequestProperty("Content-Language", "en-US"); 
        connection.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11");

        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        //send Request 
        DataOutputStream dataout = new DataOutputStream(connection.getOutputStream());
        dataout.writeBytes(urlParameters);
        dataout.flush();
        dataout.close();

        //get response
        InputStream is = connection.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line;
        StringBuffer response = new StringBuffer();

        while((line = br.readLine()) != null){
            response.append(line);
            response.append('\n');

        }
        System.out.println(response.toString());
        br.close();
        return response.toString();
    }catch(Exception e){
        System.out.println("Unable to full create connection");
        e.printStackTrace();
        return null;
    }finally {

          if(connection != null) {
            connection.disconnect(); 
          }
    }


}

【讨论】:

    【解决方案2】:

    403 响应意味着“禁止”。

    根据我所读到的内容,所有迹象都表明 COOKIES 存在问题。如果我是正确的,我将如何实施 CookieManager 来解决这个问题?或者如果不是 COOKIES 的问题,我该如何解决?

    事情没那么简单。

    • 您可能需要提供有效凭据(以 cookie、基本身份验证标头或其他形式)。但是,您希望服务器在这种情况下响应 401,或 302 以将您的浏览器重定向到登录页面。

    • 也可能是您提供了凭据,但它们不足以满足您尝试执行的请求。

    您最好的办法是弄清楚当您尝试从 Web 浏览器登录并使用该服务时究竟发生了什么。然后尝试复制它。或者,阅读站点文档或询问站点管理员该怎么做。

    如果您尝试访问的是您的站点/服务器,那么您需要弄清楚安全性是如何实现的。也许您错误地配置了服务器,或者忽略了设置/启用登录。


    您不太可能 (IMO)设置一个 Cookie 管理器来解决此问题。

    【讨论】:

    • http://:/frontend/x3/index.htmllogin_theme=cpanel&post_login= 这是我登录时的顺序。任何想法?
    • 谢谢斯蒂芬,我会看看我能想出什么。
    • 嘿斯蒂芬它似乎这个方法:connection.setChunkedStreamingMode(0);导致了这个问题。我删除了它,它现在可以正常工作。 :)
    【解决方案3】:

    如果您看到setChunkedStreamingMode 的API 文档,那里已经提到并非所有服务器都支持这种模式。您确定要连接的服务器支持此功能吗?

    【讨论】:

      猜你喜欢
      • 2011-04-21
      • 1970-01-01
      • 2015-03-28
      • 2022-01-21
      • 2015-07-17
      • 1970-01-01
      • 2013-03-29
      • 1970-01-01
      相关资源
      最近更新 更多