简体   繁体   English

从URL读取文件时出现问题-权限被拒绝

[英]Problem reading file from URL - permission denied

I was trying to connect to some host like this 我试图连接到这样的主机

                url = new URL(urlString);
                BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                url.openStream()));

but I always get MalformedException - permission denied, I need to provide username and password ( which I know ) but I don't know how, there is no constructor in URL with those parametere neither setusername/password methods. 但是我总是会得到MalformedException-权限被拒绝,我需要提供用户名和密码(我知道),但是我不知道如何,URL中没有带有这些参数的构造函数,也没有setusername / password方法。 Where to put username and password ? 用户名和密码放在哪里?

I never has such a problem but.... try to give a look at this: 我从来没有这样的问题,但是....尝试看看:

class urllib2.HTTPPasswordMgr Keep a database of (realm, uri) -> (user, password) mappings. 类urllib2.HTTPPasswordMgr保留(realm,uri)->(user,password)映射的数据库。

maybe it can help documentation about URLLIB2 at this link: 也许它可以通过以下链接帮助有关URLLIB2的文档:

http://docs.python.org/library/urllib2.html http://docs.python.org/library/urllib2.html

hope it's going to be helpful 希望对您有所帮助

If it's an HTTP authentication, use an URL of the form http://user:password@server:80/path . 如果是HTTP身份验证,请使用http://user:password@server:80/path形式的URL。
If it's an application authentication, submit a POST/GET HTTP request with the details. 如果是应用程序身份验证,请提交包含详细信息的POST / GET HTTP请求。

I had do this in a previous project. 我在先前的项目中曾做过。 In order to access a protected resource you need to use the Authenticator class. 为了访问受保护的资源,您需要使用Authenticator类。

The username and password go in the variables defined for them towards the end but they do not have to hard coded, you can externalize them using java properties. 用户名和密码将在最后为它们定义的变量中输入,但是它们不必进行硬编码,您可以使用java属性将其外部化。

Here is an old snippet 这是一个老片段

// Install the custom authenticator
Authenticator.setDefault(new MyAuthenticator());

// Access the page
try {
    // Create a URL for the desired page
    URL url = new URL("THE URL YOU NEED TO OPEN/ACCESS");

    // Read all the text returned by the server
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String str;
    while ((str = in.readLine()) != null) {
        // str is one line of text; readLine() strips the newline character(s)
    }
    in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}

public class MyAuthenticator extends Authenticator {
    // This method is called when a password-protected URL is accessed
    protected PasswordAuthentication getPasswordAuthentication() {
        // Get information about the request
        String promptString = getRequestingPrompt();
        String hostname = getRequestingHost();
        InetAddress ipaddr = getRequestingSite();
        int port = getRequestingPort();

        // Get the username from the user...
        String username = "myusername";

        // Get the password from the user...
        String password = "mypassword";

        // Return the information
        return new PasswordAuthentication(username, password.toCharArray());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM