简体   繁体   English

服务器返回HTTP响应代码:401为URL:https

[英]Server returned HTTP response code: 401 for URL: https

I'm using Java to access a HTTPS site which returns the display in an XML format. 我正在使用Java访问HTTPS站点,该站点以XML格式返回显示。 I pass the login credentials in the URL itself. 我在URL本身传递登录凭据。 Here is the code snippet: 这是代码片段:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
requestURL = "https://Administrator:Password@localhost:8443/abcd";

try { 
    InputStream is = null;
    URL url = new URL(requestURL);
    InputStream xmlInputStream =new URL(requestURL).openConnection().getInputStream();
    byte[] testByteArr = new byte[xmlInputStream.available()];
    xmlInputStream.read(testByteArr);
    System.out.println(new String(testByteArr));
    Document doc = db.parse(xmlInputStream);
    System.out.println("DOC="+doc);
} catch (MalformedURLException e) {
} 

I'm creating a trust manager in the program which does not validate signed/unsigned certificates. 我正在该程序中创建一个不验证签名/未签名证书的信任管理器。 But, on running the above program, I get the error Server returned HTTP response code: 401 for URL: https://Administrator:Password@localhost:8443/abcd 但是,在运行上述程序时,我收到错误服务器返回的HTTP响应代码:401 for URL: https:// Administrator:Password @ localhost:8443 / abcd

I can use the same url on my browser and it displays the xml correctly. 我可以在浏览器上使用相同的URL,并正确显示xml。 Kindly let me know how to make this work within the Java program. 请告诉我如何在Java程序中完成这项工作。

401 means "Unauthorized", so there must be something with your credentials. 401表示“未经授权”,因此必须有您的凭据。

I think that java URL does not support the syntax you are showing. 我认为java URL不支持您显示的语法。 You could use an Authenticator instead. 您可以使用身份验证器。

Authenticator.setDefault(new Authenticator() {

    @Override
    protected PasswordAuthentication getPasswordAuthentication() {          
        return new PasswordAuthentication(login, password.toCharArray());
    }
});

and then simply invoking the regular url, without the credentials. 然后只需调用常规网址,而无需凭据。

The other option is to provide the credentials in a Header: 另一种选择是在标头中提供凭据:

String loginPassword = login+ ":" + password;
String encoded = new sun.misc.BASE64Encoder().encode (loginPassword.getBytes());
URLConnection conn = url.openConnection();
conn.setRequestProperty ("Authorization", "Basic " + encoded);

PS: It is not recommended to use that Base64Encoder but this is only to show a quick solution. PS:不建议使用该Base64Encoder,但这只是为了显示快速解决方案。 If you want to keep that solution, look for a library that does. 如果您想保留该解决方案,请查找具有该功能的库。 There are plenty. 有很多。

Try This. 试试这个。 You need pass the authentication to let the server know its a valid user. 您需要通过身份验证才能让服务器知道其有效用户。 You need to import these two packages and has to include a jersy jar. 你需要导入这两个包,并且必须包含一个jersy jar。 If you dont want to include jersy jar then import this package 如果您不想包含jersy jar,请导入此包

import sun.misc.BASE64Encoder;

import com.sun.jersey.core.util.Base64;
import sun.net.www.protocol.http.HttpURLConnection;

and then, 然后,

String encodedAuthorizedUser = getAuthantication("username", "password");
URL url = new URL("Your Valid Jira URL");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setRequestProperty ("Authorization", "Basic " + encodedAuthorizedUser );

 public String getAuthantication(String username, String password) {
   String auth = new String(Base64.encode(username + ":" + password));
   return auth;
 }

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

相关问题 Java服务器返回的HTTP响应代码:401 - Java Server returned HTTP response code: 401 服务器返回HTTP响应代码:URL的401(在URL中传递用户名和密码以访问文件) - Server returned HTTP response code: 401 for URL (pass username and password in URL to access a file) java.io.IOException:服务器返回 HTTP 响应代码:URL 的 401:https://accounts.google.com/o/oauth2/token 在 java 中使用 gmail 登录期间 - java.io.IOException: Server returned HTTP response code: 401 for URL: https://accounts.google.com/o/oauth2/token during logging with gmail in java 尝试从Neteller获取OAuth访问令牌会产生错误:“服务器返回的HTTP响应代码:URL 401” - Attempt to get OAuth access token from Neteller produces error: “Server returned HTTP response code: 401 for URL” 服务器在使用shopify API时返回HTTP响应代码:401 for url - Server returned HTTP response code: 401 for url when using shopify API 服务器返回的HTTP响应代码:406 URL - Server returned HTTP response code: 406 a URL 服务器返回HTTP响应代码:503为URL - Server returned HTTP response code: 503 for URL 服务器返回URL的HTTP响应代码503 - Server returned HTTP response code 503 for URL 服务器返回HTTP响应代码:400为URL: - Server returned HTTP response code: 400 for URL: 服务器返回 HTTP 响应代码:URL 405 - Server returned HTTP response code: 405 for URL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM