Soap call using HttpsUrlConnection gives a 500 using java

560 Views Asked by At

I have been trying for over a month already reading everything available to me on stackoverflow and I still couldn't get to solve this issue, I need to call a soap webservice and get a response which would contain a url. I get an error 500/400 based on my diffrerent tests. Any help would be deeply appreciated (1):

The client code is as follows:

URL oURL = null;
    HttpsURLConnection con = null;
    
    try {
        // Create SOAP Connection
        oURL = new URL(soapEndpointUrl);
        con = (HttpsURLConnection) oURL.openConnection();
        
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-type", "application/soap+xml; charset=utf-8");
        con.setDoOutput(true);
        con.setUseCaches (false);
        // Send SOAP Message to SOAP Server
        
        SOAPMessage message = createSOAPRequest(oppBean, context, soapEndpointUrl, soapAction);
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        message.writeTo(bout);
        String strMsg = new String(bout.toByteArray());
        
        OutputStream out = con.getOutputStream();
        out.write(reproducerUrlEncodedString((strMsg)).getBytes());
        out.close();
        
        int responseCode = con.getResponseCode();
        
        Reader streamReader = null;
        
        if (responseCode > 299) {
            streamReader = new InputStreamReader(con.getErrorStream());
        } else {
            streamReader = new InputStreamReader(con.getInputStream());
        }
        
        try (BufferedReader in = new BufferedReader(
                streamReader)) {

            String line;
            StringBuilder response = new StringBuilder();

            while ((line = in.readLine()) != null) {
                response.append(line);
            }
           
           if(responseCode == 200) {
              logger.debug(response.toString());
           }
        }
        
    } catch (Exception e) {
        logger.error(e.getMessage());
    }

I always get a 500 if I use the request as it is. And an error 400 if I encode the double quotes. The team that made the service is not able to fetch the logs for the error 500. They built it using C#.

The request xml that they expect me to send is as below:

<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:hen="example.com"
xmlns:hen1="http://schemas.xyz.org/2004/07/xyz.WcfServices.xyz.IO.Initialize"
xmlns:hen2="example.com">
<soap:Header
    xmlns="http://www.w3.org/2005/08/addressing">
    <To>https://service.xyz.fr/Company/ABC/ABC.svc</To>
</soap:Header>
<soap:Body>
    <hen:XyzMethod>
        <hen:entree>
            <hen1:AdresseLigne1Souscripteur>35</hen1:AdresseLigne1Souscripteur>
            <hen1:AdresseLigne2Souscripteur>avenue de la Republique</hen1:AdresseLigne2Souscripteur>
            <hen1:AdresseLigne3Souscripteur>Paris 13th</hen1:AdresseLigne3Souscripteur>
            <hen1:AdresseMailEquipeCommercialConnecte>[email protected]</hen1:AdresseMailEquipeCommercialConnecte>
            <hen1:AdresseServiceCommercialLigne1>AdresseServiceCommercialLigne1</hen1:AdresseServiceCommercialLigne1>
            <hen1:AdresseServiceCommercialLigne3>AdresseServiceCommercialLigne3</hen1:AdresseServiceCommercialLigne3>
            <hen1:CanalVente>7</hen1:CanalVente>
            <hen1:CodexyzCiviliteSouscripteur>01</hen1:CodexyzCiviliteSouscripteur>
            <hen1:CodexyzRegime>0001</hen1:CodexyzRegime>
            <hen1:CodexyzSexeSouscripteur>FE</hen1:CodexyzSexeSouscripteur>
            <hen1:CodexyzTypeSouscripteur>EXCLIENT</hen1:CodexyzTypeSouscripteur>
            <hen1:CodeIsoPaysSouscripteur>FR</hen1:CodeIsoPaysSouscripteur>
            <hen1:CodeMotifOpportunite>NRENS</hen1:CodeMotifOpportunite>
            <hen1:CodePostalServiceCommercial>59650</hen1:CodePostalServiceCommercial>
            <hen1:CodePostalSouscripteur>59650</hen1:CodePostalSouscripteur>
            <hen1:CodeProduitxyz>66</hen1:CodeProduitxyz>
            <hen1:CodeServiceCommercial>20</hen1:CodeServiceCommercial>
            <hen1:DateEffetContrat>01/01/2021</hen1:DateEffetContrat>
            <hen1:DateNaissanceSouscripteur>07/07/1972</hen1:DateNaissanceSouscripteur>
            <hen1:EmailSouscripteur>[email protected]</hen1:EmailSouscripteur>
            <hen1:LibelleServiceCommercial>xyz - Département Individuels</hen1:LibelleServiceCommercial>
            <hen1:NomBeneficiaire>VILLANI</hen1:NomBeneficiaire>
            <hen1:NomCommercial>Test BCH</hen1:NomCommercial>
            <hen1:NumeroOpportunite>2020/06/0009</hen1:NumeroOpportunite>
            <hen1:NumeroOpportuniteCrm>1122159</hen1:NumeroOpportuniteCrm>
            <hen1:OpportuniteADupliquer>1122158</hen1:OpportuniteADupliquer>
            <hen1:PrenomBeneficiaire>MILAN</hen1:PrenomBeneficiaire>
            <hen1:PrenomCommercial>BCH Test</hen1:PrenomCommercial>
            <hen1:TelephoneFixeSouscripteur/>
            <hen1:TelephonePortableSouscripteur>+33 6 63 44 21 84</hen1:TelephonePortableSouscripteur>
            <hen1:TelephoneServiceCommercial>03 28 76 37 00</hen1:TelephoneServiceCommercial>
            <hen1:VilleServiceCommercial>VILLENEUVE D’ASCQ</hen1:VilleServiceCommercial>
            <hen1:VilleSouscripteur>VILLENEUVE D'ASCQ</hen1:VilleSouscripteur>
        </hen:entree>
    </hen:XyzMethod>
</soap:Body>

</soap:Envelope>

I thought of generating the client using the wsimport technique, but I got some errors that the methods didn't respect the SOAP 1.2 binding so I could not go ahead with this technique.

Good news is that the request xml works well with soapui and I get a favourable response. But the same request fails via java.

Some info on the MessageFactory etc:

MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
SOAPMessage soapMessage = messageFactory.createMessage();

Static. Calling all autobots. I'm Optimus prime, and I send this message to all autobots taking refuge among the stars. We are here. We are waiting - Optimus Prime Static

Any help would be deeply appreciated (2) . Please let me know if you need any additional info, incase if I missed to add something.

Some references: Soap calls using Java

0

There are 0 best solutions below