I'm writing an http request that requires the use of http trailer, i've searched around a bit but didn't find any example using plain java 8 (no spring or servlets).
This is the method i've currently written:
public WebResponse uploadFileSend(String URL, String httpMethod, String pdfPath, String secret, String hash256) throws IOException {
byte[] pdfContent = Files.readAllBytes(Paths.get(pdfPath));
String requestUrl = new StringBuilder()
.append(URL)
.toString();
URL url = new URL(requestUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(false);
conn.setChunkedStreamingMode(-1);
conn.setRequestMethod(httpMethod.toUpperCase());
conn.setRequestProperty("x-api-key", apiKey);
conn.setRequestProperty("Content-Type", "application/pdf");
conn.setRequestProperty("x-amz-meta-secret", secret);
conn.setRequestProperty("transfer-encoding", "chunked");
conn.setRequestProperty("Trailer", "x-amz-checksum-sha256");
conn.setRequestProperty("charset", "utf-8");
conn.setUseCaches(false);
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.write(pdfContent);
wr.flush();
wr.close();
BufferedReader bf = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = bf.readLine()) != null) {
response.append(line);
}
return new WebResponse(conn.getResponseCode(), conn.getHeaderField("x-amz-version-id"));
}
I'm not sure how to proceed from here to actually send the trailer in the request.
This is the method i've currently written:
public WebResponse uploadFileSend(String URL, String httpMethod, String pdfPath, String secret, String hash256) throws IOException {
byte[] pdfContent = Files.readAllBytes(Paths.get(pdfPath));
String requestUrl = new StringBuilder()
.append(URL)
.toString();
URL url = new URL(requestUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(false);
conn.setChunkedStreamingMode(-1);
conn.setRequestMethod(httpMethod.toUpperCase());
conn.setRequestProperty("x-api-key", apiKey);
conn.setRequestProperty("Content-Type", "application/pdf");
conn.setRequestProperty("x-amz-meta-secret", secret);
conn.setRequestProperty("transfer-encoding", "chunked");
conn.setRequestProperty("Trailer", "x-amz-checksum-sha256");
conn.setRequestProperty("charset", "utf-8");
conn.setUseCaches(false);
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.write(pdfContent);
wr.flush();
wr.close();
BufferedReader bf = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = bf.readLine()) != null) {
response.append(line);
}
return new WebResponse(conn.getResponseCode(), conn.getHeaderField("x-amz-version-id"));
}
I'm not sure how to proceed from here to actually send the trailer in the request.