[英]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服务。
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.