java
public static Cob createCharge(String token, String payload) {
Cob cob = null;
try {
Generex txid = new Generex("[a-zA-Z0-9]{26,35}");
URL url = new URL("
https://api-pix-h.gerencianet.com.br/v2/cob/" + txid.random());
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("PUT");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "Bearer " + token);
OutputStream os = conn.getOutputStream(); // O erro do connection reset ocorre aqui
os.write(payload.getBytes());
os.flush();
int responseCode = conn.getResponseCode();
StringBuilder responseBuilder = new StringBuilder();
BufferedReader br;
if (responseCode >= 400) {
br = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
} else {
br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
}
String response;
while ((response = br.readLine()) != null) {
responseBuilder.append(response);
}
//System.out.println("Código de resposta HTTP: " + responseCode);
//System.out.println("Resposta do servidor: " + responseBuilder);
JsonMapper mapper = JsonMapper
.builder()
.configure(MapperFeature.USE_ANNOTATIONS, false)
.build();
cob = mapper.readValue(responseBuilder.toString(), Cob.class);
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return (cob != null) ? cob : new Cob();
}