out.write(payload.getBytes("UTF-8"));
out.close();
int status = connection.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) {
ServerFailure failure = new ServerFailure(status + " " + connection.getResponseMessage());
receiver.onTransportFailure(failure);
return;
}
List<String> cookieHeaders = connection.getHeaderFields().get("Set-Cookie");
if (cookieHeaders != null) {
for (String header : cookieHeaders) {
List<HttpCookie> headerCookies;
try {
headerCookies = HttpCookie.parse(header);
} catch (IllegalArgumentException e) {
// if we can't parse it, ignore it
continue;
}
for (HttpCookie cookie : headerCookies) {
String domain = cookie.getDomain();
if (domain == null || url.getHost().endsWith(domain)) {
String path = cookie.getPath();
if (path == null || url.getPath().startsWith(path)) {
cookies.put(cookie.getName(), cookie.getValue());
}
}
}
}
}
String encoding = connection.getContentEncoding();
InputStream in = connection.getInputStream();
if ("gzip".equalsIgnoreCase(encoding)) {
in = new GZIPInputStream(in);
} else if ("deflate".equalsIgnoreCase(encoding)) {
in = new InflaterInputStream(in);
} else if (encoding != null) {
receiver.onTransportFailure(new ServerFailure("Unknown server encoding " + encoding));
return;
}
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int read = in.read(buffer);
while (read != -1) {
bytes.write(buffer, 0, read);
read = in.read(buffer);
}
in.close();
String received = new String(bytes.toByteArray(), "UTF-8");
receiver.onTransportSuccess(received);
} catch (IOException e) {
ServerFailure failure = new ServerFailure(e.getMessage(), e.getClass().getName(), null, true);
receiver.onTransportFailure(failure);
} finally {
if (connection != null) {
connection.disconnect();
}