sap调用java webservice_How to call SAP web service in Java?

Hi,

Please run the below java program to test the web service.

you can specify your end point details and execute your method.

package com.an.edu.SOAPtest;

import javax.xml.soap.*;

import javax.xml.transform.*;

import javax.xml.transform.stream.*;

public class SOAPClientSAAJ {

/**

* Starting point for the SAAJ - SOAP Client Testing

*/

public static void main(String args[]) {

try {

// Create SOAP Connection

SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();

SOAPConnection soapConnection = soapConnectionFactory.createConnection();

// Send SOAP Message to SOAP Server

SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

// Process the SOAP Response

printSOAPResponse(soapResponse);

soapConnection.close();

} catch (Exception e) {

System.err.println("Error occurred while sending SOAP Request to Server");

e.printStackTrace();

}

}

private static SOAPMessage createSOAPRequest() throws Exception {

MessageFactory messageFactory = MessageFactory.newInstance();

SOAPMessage soapMessage = messageFactory.createMessage();

SOAPPart soapPart = soapMessage.getSOAPPart();

String serverURI = "http://ws.cdyne.com/";

// SOAP Envelope

SOAPEnvelope envelope = soapPart.getEnvelope();

envelope.addNamespaceDeclaration("example", serverURI);

// SOAP Body

SOAPBody soapBody = envelope.getBody();

SOAPElement soapBodyElem = soapBody.addChildElement("VerifyEmail", "example");

SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("email", "example");

soapBodyElem1.addTextNode("nautiyal.anil@gmail.com");

SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("LicenseKey", "example");

soapBodyElem2.addTextNode("123");

MimeHeaders headers = soapMessage.getMimeHeaders();

headers.addHeader("SOAPAction", serverURI + "VerifyEmail");

soapMessage.saveChanges();

/* Print the request message */

System.out.print("Request SOAP Message = ");

soapMessage.writeTo(System.out);

System.out.println();

System.out.print("Getting SOAP response");

return soapMessage;

}

/**

* Method used to print the SOAP Response

*/

private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {

System.out.print("Getting SOAP response");

TransformerFactory transformerFactory = TransformerFactory.newInstance();

Transformer transformer = transformerFactory.newTransformer();

Source sourceContent = soapResponse.getSOAPPart().getContent();

System.out.print("\nResponse SOAP Message = ");

StreamResult result = new StreamResult(System.out);

transformer.transform(sourceContent, result);

}

}

Regards,

Anil N.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值