将用户名传递给 HttpGet 请求
Passing username to a HttpGet request
我需要访问一个像这样工作的 API:
curl https://api.com/ratings/v1/ -u [your token here]:
令牌是应该传递给 HttpGet
请求的用户名。我正在尝试使用 java:
通过以下方式执行相同的操作
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("usrname", "passwrd"));
HttpHost proxy = new HttpHost("proxy.com", 8080, "http");
HttpClient httpClient = HttpClients.custom().setProxy(proxy).setDefaultCredentialsProvider(credentialsProvider).build();
HttpGet toesGet = new HttpGet("https://api.com/ratings/v1/");
toesGet.setHeader("Accept", "Application/Json");
toesGet.addHeader("Username", "[your token here]");
try {
HttpResponse toes = httpClient.execute(toesGet);
System.out.println(toes.getStatusLine());
System.out.println(toes.getEntity().toString());
} catch (Exception e) {
e.printStackTrace();
}
我是一个后台代理,所以我正在创建一个 HttpHost
的代理详细信息,为 HttpClient
对象设置代理并使用 credentialsProvider
传递代理身份验证的凭据在以下代码行中:
HttpHost proxy = new HttpHost("proxy.com", 8080, "http");
HttpClient httpClient = HttpClients.custom().setProxy(proxy).setDefaultCredentialsProvider(credentialsProvider).build();
我通过添加 header
将 username
传递给 HttpGet
,如下所示:
toesGet.addHeader("Username", "[your token here]");
当我 运行 代码时,我得到这个响应:HTTP/1.1 401 UNAUTHORIZED
这表明我没有以正确的方式将 username
传递给 HttpGet
请求(或者这是否意味着其他含义?)。那么将用户名传递给 get 请求的正确方法是什么?
任何帮助将不胜感激,谢谢!
注意:我在credentialsProvider中设置的usrname
和passwrd
是为了代理认证。它们与 HttpGet 请求本身无关。我需要传递的令牌与凭据中提供的 usrname
不同。
我想,您的服务器使用基本身份验证,那么您需要添加 "Authorization" header 而不是 "Username":
String user = "[your token here]";
String pwd = ""; // blank
toesGet.addHeader("Authorization", "Basic " + Base64.encodeToString((user + ":" + pwd).getBytes(), Base64.NO_WRAP));
或者如果您的令牌包含用户名和密码,那么就这样尝试:
String token = "[your token here]";
toesGet.addHeader("Authorization", "Basic " + Base64.encodeToString(token.getBytes(), Base64.NO_WRAP));
我没有使用过 Apache HttpComponents,但我的理解是您必须为特定主机设置凭据:
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
HttpHost proxy = new HttpHost("proxy.com", 8080, "http");
credentialsProvider.setCredentials(new AuthScope(proxy),
new UsernamePasswordCredentials("usrname", "passwrd"));
credentialsProvider.setCredentials(new AuthScope("api.com", AuthScope.ANY_PORT),
new UsernamePasswordCredentials("apiuser", "apipassword"));
注意:实际上不要在代码中键入 "apiuser" 或 "apipassword"。我仅将这些显示为占位符。用访问 api.com 的正确用户名和密码替换它们。 (我指出这一点是因为,根据你问题中的代码,我不确定你是否理解你不应该使用文字字符串 "[your token here]"
。)
我需要访问一个像这样工作的 API:
curl https://api.com/ratings/v1/ -u [your token here]:
令牌是应该传递给 HttpGet
请求的用户名。我正在尝试使用 java:
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("usrname", "passwrd"));
HttpHost proxy = new HttpHost("proxy.com", 8080, "http");
HttpClient httpClient = HttpClients.custom().setProxy(proxy).setDefaultCredentialsProvider(credentialsProvider).build();
HttpGet toesGet = new HttpGet("https://api.com/ratings/v1/");
toesGet.setHeader("Accept", "Application/Json");
toesGet.addHeader("Username", "[your token here]");
try {
HttpResponse toes = httpClient.execute(toesGet);
System.out.println(toes.getStatusLine());
System.out.println(toes.getEntity().toString());
} catch (Exception e) {
e.printStackTrace();
}
我是一个后台代理,所以我正在创建一个 HttpHost
的代理详细信息,为 HttpClient
对象设置代理并使用 credentialsProvider
传递代理身份验证的凭据在以下代码行中:
HttpHost proxy = new HttpHost("proxy.com", 8080, "http");
HttpClient httpClient = HttpClients.custom().setProxy(proxy).setDefaultCredentialsProvider(credentialsProvider).build();
我通过添加 header
将 username
传递给 HttpGet
,如下所示:
toesGet.addHeader("Username", "[your token here]");
当我 运行 代码时,我得到这个响应:HTTP/1.1 401 UNAUTHORIZED
这表明我没有以正确的方式将 username
传递给 HttpGet
请求(或者这是否意味着其他含义?)。那么将用户名传递给 get 请求的正确方法是什么?
任何帮助将不胜感激,谢谢!
注意:我在credentialsProvider中设置的usrname
和passwrd
是为了代理认证。它们与 HttpGet 请求本身无关。我需要传递的令牌与凭据中提供的 usrname
不同。
我想,您的服务器使用基本身份验证,那么您需要添加 "Authorization" header 而不是 "Username":
String user = "[your token here]";
String pwd = ""; // blank
toesGet.addHeader("Authorization", "Basic " + Base64.encodeToString((user + ":" + pwd).getBytes(), Base64.NO_WRAP));
或者如果您的令牌包含用户名和密码,那么就这样尝试:
String token = "[your token here]";
toesGet.addHeader("Authorization", "Basic " + Base64.encodeToString(token.getBytes(), Base64.NO_WRAP));
我没有使用过 Apache HttpComponents,但我的理解是您必须为特定主机设置凭据:
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
HttpHost proxy = new HttpHost("proxy.com", 8080, "http");
credentialsProvider.setCredentials(new AuthScope(proxy),
new UsernamePasswordCredentials("usrname", "passwrd"));
credentialsProvider.setCredentials(new AuthScope("api.com", AuthScope.ANY_PORT),
new UsernamePasswordCredentials("apiuser", "apipassword"));
注意:实际上不要在代码中键入 "apiuser" 或 "apipassword"。我仅将这些显示为占位符。用访问 api.com 的正确用户名和密码替换它们。 (我指出这一点是因为,根据你问题中的代码,我不确定你是否理解你不应该使用文字字符串 "[your token here]"
。)