SOAP客户端调用Webservice_hanCSDN_20130225

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;
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值