Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.io.IOException:服务器返回URL https的HTTP响应代码:403://_Java - Fatal编程技术网

java.io.IOException:服务器返回URL https的HTTP响应代码:403://

java.io.IOException:服务器返回URL https的HTTP响应代码:403://,java,Java,我正在尝试通过浏览器访问URL。没有问题,但我的程序会抛出: java.io.IOException:服务器返回了URL的HTTP响应代码403: 此处URL只不过是mySharepoint Online服务器上列表项的附件文件路径。 我正在尝试获取该文件的内容。它将从浏览器中打开,但会从代码中引发异常 代码: 我已经完成了代码中的所有设置,甚至我尝试了与此主题相关的所有可能的解决方案,但仍然不起作用。不允许您访问URL/资源。检查此处403禁止响应具有以下书面含义: 服务器已理解该请求,但拒绝

我正在尝试通过浏览器访问URL。没有问题,但我的程序会抛出: java.io.IOException:服务器返回了URL的HTTP响应代码403:

此处URL只不过是mySharepoint Online服务器上列表项的附件文件路径。 我正在尝试获取该文件的内容。它将从浏览器中打开,但会从代码中引发异常

代码:


我已经完成了代码中的所有设置,甚至我尝试了与此主题相关的所有可能的解决方案,但仍然不起作用。

不允许您访问URL/资源。检查此处

403禁止响应具有以下书面含义:

服务器已理解该请求,但拒绝执行该请求。授权无效,不应重复该请求


您需要联系您尝试对话的服务器管理员,以了解请求被禁止的原因。这可能是因为他们没有启用https,或者可能是一个与https的使用完全无关的问题。

最终我们找到了解决方案,下面是解决问题的方法

代码:


向我们展示您的代码和您的尝试将有助于我们更好地了解您的问题,从而帮助您解决问题。您是否设置了用户代理标头?有些网站不允许您从web浏览器以外的任何地方下载试用版:u.openConnection();在那之后,我们完成了openStream();但仍然不起作用。
private String getAttachmentContent(String attachmentURL) throws IBSharePointException 
{
    InputStream is = null;

    try 
    {
        String fileName=attachmentURL.substring(attachmentURL.lastIndexOf("/")+1);
        String urlPath=attachmentURL.substring(0, attachmentURL.lastIndexOf("/")+1);
        fileName=URLEncoder.encode(fileName, "UTF-8");

        if(fileName.contains("+"))
            fileName=fileName.replace("+", "%20");          
        URL u=new URL(urlPath+fileName);    

        // Following Line Throws Exception : java.io.IOException: Server returned HTTP response code: 403 for URL:
        is = u.openStream();

        ByteArrayOutputStream bais = new ByteArrayOutputStream();
        byte[] byteChunk = new byte[4096];    
        int n;    
        while ( (n = is.read(byteChunk)) > 0 ) 
            { 
                bais.write(byteChunk, 0, n);                    
            }
    }catch(Exception e)
    {
        throw e;
    }
}
private String getAttachmentContent(String attachmentURL) throws IBSharePointException {
    InputStream inputStream = null;
    URLConnection urlConnection = null;
    URL url = null;
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    byte[] byteChunk = new byte[4096];
    int noOfBytes = 0;
    try {

        String fileName = attachmentURL.substring(attachmentURL.lastIndexOf("/") + 1);
        String urlPath = attachmentURL.substring(0, attachmentURL.lastIndexOf("/") + 1);
        fileName = URLEncoder.encode(fileName, "UTF-8");
        //This line is to fix bug # 966837
        if (fileName.contains("+"))
            fileName = fileName.replace("+", "%20");

        url = new URL(urlPath + fileName);
        urlConnection = url.openConnection();
        urlConnection.addRequestProperty("User-Agent", _Constants.DEFAULT_USER_AGENT_WINDOWS);
        // We need to set cookies as below.
        urlConnection.addRequestProperty("Cookie", _mSharePointSession.cookieNedToken);

        urlConnection.connect();

        inputStream = urlConnection.getInputStream();

        while ((noOfBytes = inputStream.read(byteChunk)) > 0) {
            byteOutputStream.write(byteChunk, 0, noOfBytes);
        }

        return new BASE64Encoder().encode(byteOutputStream.toByteArray());

    } catch (Exception e) {
        throw e;
    }
}