MailChimp Integration in Java
<p>I want to integrate MailChimp API in my java project. When I call Rest call using HttpURLConnection class, it responds with 401 code.</p>

<p>Here is my code:</p>

<pre><code>URL url = new URL("https://us13.api.mailchimp.com/3.0/lists");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "apikey &lt;my-key&gt;");

String input = "&lt;json data&gt;";

OutputStream os = conn.getOutputStream();
//os.write(input.getBytes());
os.flush();

if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
    throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}

BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
    System.out.println(output);
}

conn.disconnect();
</code></pre>