java 服务器返回 HTTP 响应代码:401 for URL(在 URL 中传递用户名和密码以访问文件)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17850389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Server returned HTTP response code: 401 for URL (pass username and password in URL to access a file)
提问by Vicky
i write a program that will fetch a file specified by a URL. but my code gives me "Server returned HTTP response code: 401 for URL". i googled on this and found that, this error is due to authentication failure. so my question is that : how i can pass my username and password along with my URL?
here is my code
我编写了一个程序来获取由 URL 指定的文件。但我的代码给了我“服务器返回的 HTTP 响应代码:URL 的 401”。我用谷歌搜索这个,发现这个错误是由于身份验证失败。所以我的问题是:如何将我的用户名和密码与我的 URL 一起传递?
这是我的代码
String login="dostix12";
String password="@@@@@";
String loginPassword = login+ ":" + password;
String encoded = new sun.misc.BASE64Encoder().encode (loginPassword.getBytes());
URL url = new URL("https://level.x10hosting.com:2083/cpsess5213137721/frontend/x10x3/filemanager/showfile.html?file=factorial.exe&fileop=&dir=%2Fhome%2Fdostix12%2Fetc&dirop=&charset=&file_charset=&baseurl=&basedir=");
url.openConnection();
回答by Juned Ahsan
It will depend on how the server is expecting the credentials. General way of accepting the authentication details is using BASICauthentication mechanism. In Basic authentication, you need to set the Authroization
http header.
这将取决于服务器如何期望凭据。接受认证细节的一般方式是使用BASIC认证机制。在基本认证中,您需要设置Authroization
http 标头。
The Authorization header is constructed as follows:
Authorization 标头的构造如下:
Username and password are combined into a string "username:password"
The resulting string literal is then encoded using Base64
The authorization method and a space i.e. "Basic " is then put before the encoded string.
For example, if the user agent uses 'Aladdin' as the username and 'open sesame' as the password then the header is formed as follows:
用户名和密码组合成一个字符串“用户名:密码”
然后使用 Base64 对生成的字符串文字进行编码
然后将授权方法和一个空格,即“Basic”放在编码字符串之前。
例如,如果用户代理使用“Aladdin”作为用户名,“open sesame”作为密码,那么头部的构成如下:
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
授权:基本 QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Source : http://en.wikipedia.org/wiki/Basic_access_authentication
来源:http: //en.wikipedia.org/wiki/Basic_access_authentication
Here is a sample code to add the authorization header:
这是添加授权标头的示例代码:
url = new URL(targetURL);
connection = (HttpURLConnection)url.openConnection();
BASE64Encoder enc = new sun.misc.BASE64Encoder();
String userpassword = username + ":" + password;
String encodedAuthorization = enc.encode( userpassword.getBytes() );
connection.setRequestProperty("Authorization", "Basic "+
encodedAuthorization);
回答by Pankaj Khattar
Use the following the code, it would work:
使用以下代码,它会起作用:
final URL url = new URL(urlString);
Authenticator.setDefault(new Authenticator()
{
@Override
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(userName, password.toCharArray());
}
});
final URLConnection connection = url.openConnection();
回答by morgano
You need to send the Authorizationheader in your HTTP request, in it you should send username and password encoded in base64.
您需要在 HTTP 请求中发送Authorization标头,您应该在其中发送以 base64 编码的用户名和密码。
more info here: http://en.wikipedia.org/wiki/HTTP_headers