0
我必須向api發出請求。 的作品的捲曲是:在java中請求curl
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響應代碼。 我在做什麼錯?
你說得對。憑證表單是apiKey:x,其中x是密碼。我將API密鑰保存在應用程序屬性文件中,比如「apikey」,當我使用它時,它變成了「」apikey「」。謝謝。 –