http://developer.dnb.com/docs/2.0/common/authentication-process
我尝试访问的服务的身份验证过程在上面的url中解释。这是一个简单的REST身份验证过程,其中user-id和password传递如下:
POST https://maxcvservices.dnb.com/rest/Authentication
x-dnb-user: MyUsername
x-dnb-pwd: MyPassword
我尝试过以下代码,但收到了HTTP/1.1 400 ERROR
。我无法弄清楚我做错了什么,因为我对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();
}
}
答案 0 :(得分:2)
您需要将用户名和密码设置为http标头
httPost.addHeader("x-dnb-user", "myusername");
httPost.addHeader("x-dnb-pwd", "mypassword");
因为它需要http头文件中的那些
答案 1 :(得分:1)
您可以在Java中使用Jersey - RESTful Web Services。
下载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);
更改用户名和密码。
答案 2 :(得分:0)
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请求中使用Header传递(虽然我不知道这个设计选择的优点或安全含义是什么),这是上面的以下两行代码实现:
httpPost.setHeader("x-dnb-user","myusername");
httpPost.setHeader("x-dnb-pwd","mypassword");
答案 3 :(得分:0)
d&amp; b文档说用户名和密码应该在请求标头中发送,但是你的代码是在正文中发送它们。
不是将它们作为名称/值对添加到实体中,而是将它们设置为请求标头,例如
httpPost.setHeader("x-dnb-user", "my username");