简体   繁体   English

我正在尝试使用HTTPClient发出简单的身份验证请求,但收到错误

[英]I am trying to make a simple authentication request using HTTPClient but am receiving an error

http://developer.dnb.com/docs/2.0/common/authentication-process http://developer.dnb.com/docs/2.0/common/authentication-process

The authentication process of the service I am trying to access is explained in the url above. 我尝试访问的服务的身份验证过程已在上面的网址中进行了说明。 It is a simple REST authentication process wherein the user-id and password are passed in as follows: 这是一个简单的REST身份验证过程,其中,用户ID和密码按以下方式传递:

POST https://maxcvservices.dnb.com/rest/Authentication
x-dnb-user: MyUsername
x-dnb-pwd: MyPassword

I have tried the following code but am receiving HTTP/1.1 400 ERROR . 我尝试了以下代码,但是正在接收HTTP/1.1 400 ERROR I am unable to figure out what I am doing wrong as I am very new to Java and HttpClient in particular. 我无法弄清楚我在做什么错,因为我对Java和HttpClient特别陌生。 Any help would be much appreciated. 任何帮助将非常感激。

I have thus far tried the following code: 到目前为止,我已经尝试了以下代码:

public static void main(String[] args) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpPost httpPost = new HttpPost("https://maxcvservices.dnb.com/rest/Authentication");
            List <NameValuePair> nvps = new ArrayList <NameValuePair>();
            nvps.add(new BasicNameValuePair("x-dnb-user", "myusername"));
            nvps.add(new BasicNameValuePair("x-dnb-pwd", "mypassword"));
            httpPost.setEntity(new UrlEncodedFormEntity(nvps));
            CloseableHttpResponse response2 = httpclient.execute(httpPost);

            try {
                System.out.println(response2.getStatusLine());
                HttpEntity entity2 = response2.getEntity();
                // do something useful with the response body
                // and ensure it is fully consumed
                EntityUtils.consume(entity2);
            } finally {
                response2.close();
            }
        } finally {
            httpclient.close();
        }
    }

you need to set the username and password to http header 您需要将用户名和密码设置为http标头

            httPost.addHeader("x-dnb-user", "myusername");
            httPost.addHeader("x-dnb-pwd", "mypassword");

as it expects those in the http header 因为它期望http标头中的那些

You can use Jersey - RESTful Web Services in Java. 您可以在Java中使用Jersey-RESTful Web服务。

Download Jersey . 下载Jersey

Client client = Client.create();
    String auth = new String(Base64.encode("UserName:Password"));
   String url = "https://maxcvservices.dnb.com/rest/Authentication";
    WebResource webResource = client
           .resource(url);
    ClientResponse response = webResource.header("Authorization", "Basic " + auth).type(MediaType.APPLICATION_JSON_TYPE).accept("application/json").get(ClientResponse.class);

   if (response.getStatus() != 200) {
        throw new RuntimeException("Failed : HTTP error code : "
                + response.getStatus());
    }

    String output = response.getEntity(String.class);
    System.out.println(output);

Change Username and Password. 更改用户名和密码。

 public static void main(String[] args) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
//          
            HttpPost httpPost = new HttpPost("https://maxcvservices.dnb.com/rest/Authentication");
            httpPost.setHeader("x-dnb-user","myusername");
            httpPost.setHeader("x-dnb-pwd","mypassword");
            //List <NameValuePair> nvps = new ArrayList <NameValuePair>();
            //nvps.add(new BasicNameValuePair("x-dnb-user", "myusername"));
            //nvps.add(new BasicNameValuePair("x-dnb-pwd", "mypassword"));
            //httpPost.setEntity(new UrlEncodedFormEntity(nvps));
            CloseableHttpResponse response2 = httpclient.execute(httpPost);

            try {
                System.out.println(response2.getStatusLine());
                HttpEntity entity2 = response2.getEntity();
                // do something useful with the response body
                // and ensure it is fully consumed
                EntityUtils.consume(entity2);
            } finally {
                response2.close();
            }
        } finally {
            httpclient.close();
        }
    }

Apparently the username and password was supposed to be passed in using a Header (although I don't know what the advantage or security implications of this design choice are) in the HttpPost request which is what the following two lines in the above code achieve: 显然,应该在HttpPost请求中使用标头(尽管我不知道此设计选择的优点或安全性含义)来传递用户名和密码,这是上述代码中的以下两行实现的功能:

 httpPost.setHeader("x-dnb-user","myusername");
 httpPost.setHeader("x-dnb-pwd","mypassword");

The d&b documentation says that the username and password should be sent in request header s, but your code is sending them in the body. d&b文档说用户名和密码应在请求标头s中发送,但您的代码会将其发送到正文中。

Instead of adding them as name/ value pairs in an entity, set them as request headers, eg 与其将它们作为名称/值对添加到实体中,不如将它们设置为请求标头,例如

httpPost.setHeader("x-dnb-user", "my username");

暂无
暂无

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

相关问题 我正在尝试访问页面,但收到 500 错误 - I am trying to reach page and I am receiving 500 error 我正在尝试在 JSwing 中制作一个简单的计算器 - I am trying to make a simple calculator in JSwing 我正在尝试制作一个简单的计算器 - I am trying to make a calculator, a simple one 我正在尝试制作一个简单的程序,用“*”符号替换&#39;a&#39;,&#39;e&#39;,&#39;i&#39;,但我收到此错误代码 - I am trying make a simple program that replaces 'a', 'e', 'i' with the “*” symbol but i'm getting this error code 为什么在尝试将 Button 添加到 GridPane 时会收到错误消息? - Why am I receiving an error when trying to add a Button to a GridPane? 为什么我收到JSON请求时收到500错误? - Why am I receiving 500 error in response to my JSON request? 我正在尝试这个简单的数独 - I am trying this simple sudoku 我正在尝试使用 servlet 做简单的 hello world 程序。 但我收到错误 HTTP 状态 405 - 不允许的方法 - I am trying to do simple hello world program using servlets. But i am getting an error HTTP status 405 - Method not allowed 我正在尝试使用jconsole连接到具有身份验证的JMX,但我无法这样做。 但是我无需身份验证就可以连接它 - I am trying to connect to JMX with authentication using jconsole, but I am not able to do so. But I am able to connect it without authentication 当我尝试使用 Tomcat 服务器在 java servlet 中发送 GET 请求时,我收到错误实例化 servlet class - when I am trying to send a GET request in java servlet using Tomcat server I am getting error instantiating servlet class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM