Java 我试图使用HTTPClient发出一个简单的身份验证请求,但收到一个错误

Java 我试图使用HTTPClient发出一个简单的身份验证请求,但收到一个错误,java,http,rest,authentication,apache-httpclient-4.x,Java,Http,Rest,Authentication,Apache Httpclient 4.x,上面的url中解释了我尝试访问的服务的身份验证过程。这是一个简单的REST身份验证过程,其中用户id和密码按如下方式传递: POST https://maxcvservices.dnb.com/rest/Authentication x-dnb-user: MyUsername x-dnb-pwd: MyPassword 我尝试了以下代码,但收到了HTTP/1.1400错误。我不知道我做错了什么,因为我对Java和HttpClient非常陌生。任何帮助都将不胜感激 到目前为止,我已经尝试了以下

上面的url中解释了我尝试访问的服务的身份验证过程。这是一个简单的REST身份验证过程,其中用户id和密码按如下方式传递:

POST https://maxcvservices.dnb.com/rest/Authentication
x-dnb-user: MyUsername
x-dnb-pwd: MyPassword
我尝试了以下代码,但收到了
HTTP/1.1400错误
。我不知道我做错了什么,因为我对Java和HttpClient非常陌生。任何帮助都将不胜感激

到目前为止,我已经尝试了以下代码:

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();
        }
    }
publicstaticvoidmain(字符串[]args)引发异常{
CloseableHttpClient httpclient=HttpClients.createDefault();
试一试{
HttpPost HttpPost=新的HttpPost(“https://maxcvservices.dnb.com/rest/Authentication");
List-nvps=newarraylist();
添加(新的BasicNameValuePair(“x-dnb-user”、“myusername”);
添加(新的BasicNameValuePair(“x-dnb-pwd”、“我的密码”);
setEntity(新的UrlEncodedFormEntity(nvps));
CloseableHttpResponse response2=httpclient.execute(httpPost);
试一试{
System.out.println(response2.getStatusLine());
HttpEntity entity2=response2.getEntity();
//对响应体执行一些有用的操作
//并确保它被完全消耗
EntityUtils.consume(entity2);
}最后{
response2.close();
}
}最后{
httpclient.close();
}
}

您需要将用户名和密码设置为http头

            httPost.addHeader("x-dnb-user", "myusername");
            httPost.addHeader("x-dnb-pwd", "mypassword");
 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();
        }
    }

正如在http头中所期望的那样,您需要将用户名和密码设置为http头

            httPost.addHeader("x-dnb-user", "myusername");
            httPost.addHeader("x-dnb-pwd", "mypassword");
 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();
        }
    }

正如在http头中所期望的那样,您需要将用户名和密码设置为http头

            httPost.addHeader("x-dnb-user", "myusername");
            httPost.addHeader("x-dnb-pwd", "mypassword");
 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();
        }
    }

正如在http头中所期望的那样,您需要将用户名和密码设置为http头

            httPost.addHeader("x-dnb-user", "myusername");
            httPost.addHeader("x-dnb-pwd", "mypassword");
 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();
        }
    }

正如http头中所期望的那样,d&b文档说用户名和密码应该在请求头中发送,但您的代码将它们发送到正文中

            httPost.addHeader("x-dnb-user", "myusername");
            httPost.addHeader("x-dnb-pwd", "mypassword");
 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();
        }
    }
不要将它们作为名称/值对添加到实体中,而是将它们设置为请求头,例如


httpPost.setHeader(“x-dnb-user”、“我的用户名”)

d&b文档说明用户名和密码应该在请求头s中发送,但您的代码将它们发送到正文中

不要将它们作为名称/值对添加到实体中,而是将它们设置为请求头,例如


httpPost.setHeader(“x-dnb-user”、“我的用户名”)

d&b文档说明用户名和密码应该在请求头s中发送,但您的代码将它们发送到正文中

不要将它们作为名称/值对添加到实体中,而是将它们设置为请求头,例如


httpPost.setHeader(“x-dnb-user”、“我的用户名”)

d&b文档说明用户名和密码应该在请求头s中发送,但您的代码将它们发送到正文中

不要将它们作为名称/值对添加到实体中,而是将它们设置为请求头,例如


httpPost.setHeader(“x-dnb-user”、“我的用户名”)

您可以在Java中使用Jersey-RESTful Web服务

下载


更改用户名和密码。

您可以在Java中使用Jersey-RESTful Web服务

下载


更改用户名和密码。

您可以在Java中使用Jersey-RESTful Web服务

下载


更改用户名和密码。

您可以在Java中使用Jersey-RESTful Web服务

下载


更改用户名和密码。

响应正文通常包含有意义的错误消息。响应正文通常包含有意义的错误消息。响应正文通常包含有意义的错误消息。响应正文通常包含有意义的错误消息。我认为自通过httpsi进行的通信不认为有太多的安全隐患,因为通过httpsi进行的通信不认为有太多的安全隐患,因为通过httpsi进行的通信不认为有太多的安全隐患,因为通过https进行的通信