Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,19 @@ private HttpURLConnection createConnectionObject(String path, Method method) thr

// Send HTTP data (payload) to server
private void sendData(HttpURLConnection conn, String data) throws SparkPostException {
String lenStr;
try {
lenStr = Integer.toString(data.getBytes(DEFAULT_CHARSET).length);
} catch (UnsupportedEncodingException e) {
byte[] bytes = null;
try
{
bytes = data.getBytes(DEFAULT_CHARSET);
}
catch (UnsupportedEncodingException e)
{
// This should never happen. UTF-8 should always be available but we
// have to catch it so pass it on if it fails.
throw new SparkPostException(e);
}

String lenStr = Integer.toString(bytes.length);
conn.setRequestProperty("Content-Length", lenStr);
conn.setRequestProperty("Content-Type", "application/json");

Expand All @@ -184,7 +189,7 @@ private void sendData(HttpURLConnection conn, String data) throws SparkPostExcep
// Send data. At this point connection to server may not be established,
// but writing data to it will trigger the connection.
try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
wr.write(data.getBytes(DEFAULT_CHARSET));
wr.write(bytes);
wr.flush();
} catch (IOException ex) {
throw new SparkPostException("Error sending request data:" + ex.toString());
Expand Down