rest使用Java获取MailChimp响应
我想使用MailChimp api添加订户。作为一个开始,我想读一下我正在试图从MailChimp api获得回复的其他部分
我似乎做了正确的授权,因为我得到了状态200,但由于某种原因,我没有得到响应
以下是迄今为止的代码:
public void doPostAction() throws IOException{
// BASIC Authentication
String name = "user";
String password = apikey;
String authString = name + ":" + password;
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
URL urlConnector = new URL(url);
HttpURLConnection httpConnection = (HttpURLConnection) urlConnector.openConnection();
httpConnection.setRequestMethod("GET");
httpConnection.setDoOutput(true);
httpConnection.setDoInput(true);
httpConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
httpConnection.setRequestProperty("Accept", "application/json");
httpConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
InputStream is = httpConnection.getInputStream();
// check status
System.out.println("DoPost: status: " + httpConnection.getResponseCode());
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
System.out.println("DoPost response: \n" + line);
br.close();
}
看着MailChimp游乐场,我好像错过了很多
我如何得到回应
***/编辑/**** 如果有人正在查看上述代码,则输出应为:
System.out.println("DoPost response: \n" + sb); // not line
# 1 楼答案
好的,上面的代码可以工作。基本错误
我在检查line变量时它是空的,而不是响应
当我改为:
。。。它起作用了