java+mail+authen_用Java获取MailChimp响应

本文档展示了如何使用Java进行基本认证(BASIC Authentication)来连接MailChimp API并获取响应。代码示例中,通过设置请求头进行授权,并从HTTP连接的输入流中读取API响应。目前的问题在于如何正确输出完整的响应内容。
摘要由CSDN通过智能技术生成

我想使用MailChimp api添加订阅服务器。首先,我想从其他的部分中得到一个消息,我试图从MailChimp api中得到一个响应。

以下是目前为止的代码:

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();

}

exirY.png

我如何得到回应?

如果有人在看上面的代码,输出应该是:

System.out.println("DoPost response: \n" + sb); // not line

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值