import java.io.IOException;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
public class SoapClient {
/**
* 调用短信接收接口
*/
private static final String STATUSCODE_SUCCESS = "0";
private static final String STATUSCODE_ERROR1 = "1";
private static final String STATUSCODE_ERROR2 = "2";
private static final String STATUSCODE_ERROR3 = "3";
private static final String STATUSCODE_ERROR4 = "4";
private static final String STATUSCODE_ERROR5 = "5";
private static final String STATUSCODE_OTHTERS = "6";
private static final String SMS_NAMESPACE_PREFIX = "Ws";
private static final String WEBSERVICE_INSECURE_URL = "http://localhost:9080/ws/services/Ws";
/**
* 测试主函数
*
* @param args
*/
public static void main(String[] args) {
SoapClient client = new SoapClient();
try {
long id = (long) (Math.random()*100000);
System.out.println(id);
String result = client.sendSms(id, "3333333", "1111111111111", "fff",
System.currentTimeMillis());
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 创建 SOAP Connection
*
* @return
* @throws UnsupportedOperationException
* @throws SOAPException
*/
private static SOAPConnection getSoapConnection() throws UnsupportedOperationException, SOAPException {
final SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
final SOAPConnection soapConnection = soapConnectionFactory.createConnection();
return soapConnection;
}
/**
* 创建 SOAP Message
*
* @return
* @throws SOAPException
*/
private SOAPMessage getSoapMessage() throws SOAPException {
final MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
final SOAPMessage soapMessage = messageFactory.createMessage();
// -- 创建 SOAP 消息体
final SOAPPart soapPart = soapMessage.getSOAPPart();
final SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
envelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
envelope.addNamespaceDeclaration("enc", "http://schemas.xmlsoap.org/soap/encoding/");
envelope.addNamespaceDeclaration("env", "http://schemas.xmlsoap.org/soap/envelop/");
// -- 添加服务命名空间 ,如: "SMSService"
envelope.addNamespaceDeclaration(SMS_NAMESPACE_PREFIX, "http://ws.test.com");
envelope.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/");
return soapMessage;
}
public String sendSms(long id, String appKey, String phoneNums, String content, long time) throws SOAPException, IOException, InterruptedException {
// -- 创建SOAP传输对象
final SOAPMessage soapMessage = getSoapMessage();
System.out.println(soapMessage.getSOAPHeader());
soapMessage.getSOAPHeader().detachNode();
/*System.out.println(soapMessage.getSOAPHeader());
soapMessage.getSOAPHeader().setAttribute("SOAPAction", "http://localhost:9080/ws/services/Ws");
*/
final SOAPBody soapBody = soapMessage.getSOAPBody();
final SOAPElement getMessage = soapBody.addChildElement("sendSms",SMS_NAMESPACE_PREFIX);
// -- 添加参数节点
getMessage.setEncodingStyle(SOAPConstants.URI_NS_SOAP_ENCODING);
getMessage.addChildElement("id").addTextNode(String.valueOf(id)).setAttribute("type","xs:long");
getMessage.addChildElement("appKey").addTextNode(appKey).setAttribute("type","xs:string");
getMessage.addChildElement("phoneNums").addTextNode(phoneNums).setAttribute("type","xs:string");
getMessage.addChildElement("content").addTextNode(content).setAttribute("type","xs:string");
getMessage.addChildElement("time").addTextNode(String.valueOf(time)).setAttribute("type","xs:long");
soapMessage.saveChanges();
//Thread.sleep(3*1000);
//soapMessage.writeTo(System.out);
// -- 连接服务并获得返回的状态码
final SOAPConnection soapConnection = getSoapConnection();
final SOAPMessage soapMessageReply = soapConnection.call(soapMessage, WEBSERVICE_INSECURE_URL);
/* System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxx--->" + soapMessageReply.getSOAPBody().getFirstChild().getFirstChild().getFirstChild().getNodeValue());*/
final String statusCode = soapMessageReply.getSOAPBody().getFirstChild().getFirstChild().getFirstChild().getNodeValue();
soapConnection.close();
return CodeToStr(statusCode);
}
public String CodeToStr(String code){
if("0".equals(code)){
return STATUSCODE_SUCCESS;
}else if("1".equals(code)){
return STATUSCODE_ERROR1;
}else if("2".equals(code)){
return STATUSCODE_ERROR2;
}
else if("3".equals(code)){
return STATUSCODE_ERROR3;
}
else if("4".equals(code)){
return STATUSCODE_ERROR4;
}
else if("5".equals(code)){
return STATUSCODE_ERROR5;
}
else{
return STATUSCODE_OTHTERS;
}
}
}
SOAP客户端调用Webservice_hanCSDN_20130225
最新推荐文章于 2024-07-09 20:05:23 发布