Java Requires Chilkat v11.0.0+
Chilkat2 Java
Send POST to Bradesco Platform with Billing Ticket for Registration
See more HTTP Misc Examples
Sends a POST request to the Bradesco platform containing the JSON data of the Billing Ticket for registration.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Crypt2;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpRequest;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
public class ChilkatExample {
public static void main(String[] args) {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// First build the JSON containing the data to be sent.
// {
// "nuCPFCNPJ":"12668",
// "filialCPFCNPJ":"1",
// "ctrlCPFCNPJ":"59",
// "cdTipoAcesso":"2",
// "idProduto":"9",
// "nuNegociacao":"262200000000005577",
// "nuCliente":"SEU_NUM_CLIENTE",
// "dtEmissaoTitulo":"21.09.2016",
// "dtVencimentoTitulo":"21.09.2016",
// "vlNominalTitulo":"888888",
// "cdEspecieTitulo":"2",
// "nomePagador":"NOME DO PAGADOR",
// "logradouroPagador":"ENDERECO DO PAGADOR",
// "nuLogradouroPagador":"1145",
// "complementoLogradouroPagador":"APTO 34",
// "cepPagador":"5588",
// "complementoCepPagador":"1",
// "bairroPagador":"BAIRRO PAGADOR",
// "municipioPagador":"MUNICIPIO PAGADOR",
// "ufPagador":"SP",
// "cdIndCpfcnpjPagador":"2",
// "nuCpfcnpjPagador":"12668000159",
// "endEletronicoPagador":"PAGADOR@BRADESCO.COM.BR",
// "nomeSacadorAvalista":"NOME SACADOR AVALISTA",
// "logradouroSacadorAvalista":"ENDERECO SACADOR AVALISTA",
// "nuLogradouroSacadorAvalista":"5555",
// "complementoLogradouroSacadorAvalista":"BLOCO 23",
// "cepSacadorAvalista":"6182",
// "complementoCepSacadorAvalista":"160",
// "bairroSacadorAvalista":"BAIRRO SACADOR AVALISTA",
// "municipioSacadorAvalista":"MUNICIPIO SACADOR AVALISTA",
// "ufSacadorAvalista":"SP",
// "cdIndCpfcnpjSacadorAvalista":"2",
// "nuCpfcnpjSacadorAvalista":"12668000159",
// "endEletronicoSacadorAvalista":"SACADOR@BRADESCO.COM.BR",
// }
//
JsonObject json = new JsonObject();
json.updateString("nuCPFCNPJ", "12668");
json.updateString("filialCPFCNPJ", "1");
json.updateString("ctrlCPFCNPJ", "59");
json.updateString("cdTipoAcesso", "2");
json.updateString("idProduto", "9");
json.updateString("nuNegociacao", "262200000000005577");
json.updateString("nuCliente", "SEU_NUM_CLIENTE");
json.updateString("dtEmissaoTitulo", "21.09.2016");
json.updateString("dtVencimentoTitulo", "21.09.2016");
json.updateString("vlNominalTitulo", "888888");
json.updateString("cdEspecieTitulo", "2");
json.updateString("nomePagador", "NOME DO PAGADOR");
json.updateString("logradouroPagador", "ENDERECO DO PAGADOR");
json.updateString("nuLogradouroPagador", "1145");
json.updateString("complementoLogradouroPagador", "APTO 34");
json.updateString("cepPagador", "5588");
json.updateString("complementoCepPagador", "1");
json.updateString("bairroPagador", "BAIRRO PAGADOR");
json.updateString("municipioPagador", "MUNICIPIO PAGADOR");
json.updateString("ufPagador", "SP");
json.updateString("cdIndCpfcnpjPagador", "2");
json.updateString("nuCpfcnpjPagador", "12668000159");
json.updateString("endEletronicoPagador", "PAGADOR@BRADESCO.COM.BR");
json.updateString("nomeSacadorAvalista", "NOME SACADOR AVALISTA");
json.updateString("logradouroSacadorAvalista", "ENDERECO SACADOR AVALISTA");
json.updateString("nuLogradouroSacadorAvalista", "5555");
json.updateString("complementoLogradouroSacadorAvalista", "BLOCO 23");
json.updateString("cepSacadorAvalista", "6182");
json.updateString("complementoCepSacadorAvalista", "160");
json.updateString("bairroSacadorAvalista", "BAIRRO SACADOR AVALISTA");
json.updateString("municipioSacadorAvalista", "MUNICIPIO SACADOR AVALISTA");
json.updateString("ufSacadorAvalista", "SP");
json.updateString("cdIndCpfcnpjSacadorAvalista", "2");
json.updateString("nuCpfcnpjSacadorAvalista", "12668000159");
json.updateString("endEletronicoSacadorAvalista", "SACADOR@BRADESCO.COM.BR");
// Load out PFX file
Cert cert = new Cert();
try {
cert.loadPfxFile("qa_data/pfx/cert_test123.pfx", "test123");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
Crypt2 crypt = new Crypt2();
String sigBase64;
try {
crypt.setSigningCert(cert);
// Use SHA-256 within the signing..
crypt.setHashAlgorithm("sha256");
// Use no authenticated attributes
crypt.setSigningAttributes("{}");
json.setEmitCompact(true);
sigBase64 = crypt.opaqueSignStringENC(json.emit());
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Next, we need to send a POST to the following URL where the body of the POST
// contains the binary PKCS7 signature (which embeds the JSON).
String url = "https://cobranca.bradesconetempresa.b.br/ibpjregistrotitulows/registrotitulohomologacao";
HttpRequest req = new HttpRequest();
req.setHttpVerb("POST");
// This is strange because apparently the server wants a "text/xml" Content-Type,
// but the content in the body is not actually XML, it's binary PKCS7.
req.setContentType("text/xml");
req.loadBodyFromString(sigBase64, "utf-8");
// We don't actually pass the URL, we set the path here, and the domain is passed below..
req.setPath("/ibpjregistrotitulows/registrotitulohomologacao");
Http http = new Http();
HttpResponse resp = new HttpResponse();
try {
http.httpSReq("cobranca.bradesconetempresa.b.br", 443, true, req, resp);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("Response status code: " + resp.getStatusCode());
System.out.println("Response body:");
System.out.println(resp.getBodyStr());
}
}