How to consume an asmx webservice from Java?

0

I have a webservice implemented as follows on ip 192.168.1.201 and port 888:

When I click on invoke, it returns the following:

What would be the correct way to collect this information from a Java program? I tried to pobar with the following code:

public static void main(String[] args)
{
    String soapEndpointUrl = "http://192.168.1.201:8888/wbsrv.asmx";
    String soapAction = "http://tempuri.org/wbsrv";

    callSoapWebService(soapEndpointUrl, soapAction);
}


private static void callSoapWebService(String soapEndpointUrl, String soapAction)
{
    try {
        // Create SOAP Connection
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();

        SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction), soapEndpointUrl);

        System.out.println("Response SOAP Message:");
        soapResponse.writeTo(System.out);
        System.out.println();

        soapConnection.close();
    } catch (Exception e) {
        System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n");
        e.printStackTrace();
    }
}

private static SOAPMessage createSOAPRequest(String soapAction) throws Exception
{
    MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();

    createSoapEnvelope(soapMessage);

    MimeHeaders headers = soapMessage.getMimeHeaders();
    headers.addHeader("SOAPAction", soapAction);

    soapMessage.saveChanges();

    System.out.println("Request SOAP Message:");
    soapMessage.writeTo(System.out);
    System.out.println("\n");

    return soapMessage;
}

private static void createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException
{
    SOAPPart soapPart = soapMessage.getSOAPPart();

    String myNamespace = "wbsrv";
    String myNamespaceURI = "http://tempuri.org/wbsrv";

    SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);
    SOAPBody soapBody = envelope.getBody();
    SOAPElement soapBodyElem = soapBody.addChildElement(myNamespace);
    SOAPElement chapa = soapBodyElem.addChildElement("chapa");
    chapa.addTextNode("9992");
    SOAPElement maquina = soapBodyElem.addChildElement("maquina");
    maquina.addTextNode("12222");
    SOAPElement orden = soapBodyElem.addChildElement("orden_webser");
    orden.addTextNode("20000064");
    SOAPElement operario = soapBodyElem.addChildElement("operario");
    operario.addTextNode("0178");
}
    
asked by marcss 24.10.2018 в 16:03
source

0 answers