在 java 内卷曲请求
curl to request in java
我必须向 api 提出请求。
有效的卷曲是:
curl -u apiKey:pass -H "Accept: application/json" https://subdomain.chargify.com/portal/customers/id/management_link.json
我目前的 java 代码是:
String userpass = apiKey + ":" + pass;
String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
URL url = new URL(stringUrl);
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.setRequestProperty("Accept", "application/json");
uc.setRequestProperty("Authorization", basicAuth);
InputStream content = uc.getInputStream();
int status = uc.getResponseCode();
BufferedReader in = new BufferedReader (new InputStreamReader(content));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
每次我收到 401 响应代码。
我做错了什么?
您的请求需要用户身份验证。由于请求已包含授权凭据,因此 401 响应表示已拒绝对这些凭据进行授权。检查您如何生成和验证密钥
我必须向 api 提出请求。 有效的卷曲是:
curl -u apiKey:pass -H "Accept: application/json" https://subdomain.chargify.com/portal/customers/id/management_link.json
我目前的 java 代码是:
String userpass = apiKey + ":" + pass;
String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
URL url = new URL(stringUrl);
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.setRequestProperty("Accept", "application/json");
uc.setRequestProperty("Authorization", basicAuth);
InputStream content = uc.getInputStream();
int status = uc.getResponseCode();
BufferedReader in = new BufferedReader (new InputStreamReader(content));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
每次我收到 401 响应代码。 我做错了什么?
您的请求需要用户身份验证。由于请求已包含授权凭据,因此 401 响应表示已拒绝对这些凭据进行授权。检查您如何生成和验证密钥