Hi again.
I represent large e-shop and we would like to cooperate with you.
Unfortunately I'm trying to get this thing work for couple of weeks
now.
I try to use your API from Java.
For encoding I use Apache's encoder and it still doesn't work.
Here is the code:
import org.apache.commons.codec.binary.Base64;
import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
public class CampaignCreateTest {
private static final String mailChimpApiUri = "https://
us4.api.mailchimp.com/1.3/?method=campaignCreate";
private static final String archiveUri = "file:/home/domosti/
temp.zip";
public static void main(String[] args) throws IOException,
URISyntaxException {
// open connection to server
URL url = new URL(mailChimpApiUri);
HttpsURLConnection conn = (HttpsURLConnection)
url.openConnection();
conn.setInstanceFollowRedirects(false);
conn.setDoOutput(true);
// prepare request
URI uri = new URI(archiveUri);
File file = new File(uri);
FileInputStream fileInputStream = new FileInputStream(file);
// translate archive file into byte array. This code was taken
from apache.commons.IOUtils
ByteArrayOutputStream byteArrayOutputStream = new
ByteArrayOutputStream();
byte[] buffer = new byte[1024*4];
int n = 0;
while (-1 != (n = fileInputStream.read(buffer))) {
byteArrayOutputStream.write(buffer, 0, n);
}
byte[] data = byteArrayOutputStream.toByteArray();
// I use apache's encoder
String archive = Base64.encodeBase64String(data);
String request = "{\"type\":\"regular\",\"content\":{\"archive
\":\"" + archive + "\"},\"options\":{\"subject\":\"test\",\"from_name
\":\"test\",\"from_email\":\"
alex....@gmail.com\",\"to_name\":\"test
\",\"auto_footer\":false,\"list_id\":\"82ce0408ca\",\"title\":\"New\"},
\"apikey\":\"e2fb0e5f30fab6ae50275ff9f7a5ae16-us4\"}";
System.out.println(request);
OutputStream out = conn.getOutputStream();
out.write(request.getBytes());
System.out.println(conn.getResponseCode());
BufferedReader in = new BufferedReader(new
InputStreamReader(conn.getInputStream(), "UTF-8"));
String response = "";
while ((response = in.readLine()) != null)
System.out.println(response);
}
}
Substitute "archiveUri" constant with the path to you archive.
Hope you'll help.
Sincerely, Alex.