纯SOAP动态调用Web Service API

27 篇文章 0 订阅
package com.dotmailer;

import java.io.FileReader;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.Node;
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.SOAPMessage;

import org.w3c.dom.NodeList;

public class DynamicSoapCall {
      
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String methodName = "ListCampaigns";
		String[][] arg = new String[2][2];
		arg[0][0] = "username";
		arg[0][1] = "";
		
		arg[1][0] = "password";
		arg[1][1] = "";		
		
		//DynamicSoapCall.runSOAP(methodName,arg);
		DynamicSoapCall.createQQMessage("457593060");
}

    //QQ是否在线 测试API调用
    public static SOAPMessage createQQMessage(String qqNumber){  
       MessageFactory fac=null;  
       SOAPMessage reqMsg=null;  
       try {  
            fac=MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);  
            reqMsg=fac.createMessage();  
               
            reqMsg.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "UTF-8");  
            reqMsg.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");  
              
            SOAPEnvelope env=reqMsg.getSOAPPart().getEnvelope();  
            env.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");  
            env.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");  
              
            MimeHeaders mHers=reqMsg.getMimeHeaders();  
            mHers.setHeader("SOAPAction", "http://WebXml.com.cn/qqCheckOnline");  
              
            SOAPBody body=reqMsg.getSOAPBody();  
            SOAPElement qqCheckOnline=body.addChildElement("qqCheckOnline","","http://WebXml.com.cn/");  
              
            SOAPElement qqCode=qqCheckOnline.addChildElement("qqCode");  
            qqCode.addTextNode(qqNumber);  
                     
            
            SOAPConnectionFactory conFac=SOAPConnectionFactory.newInstance();  
            SOAPConnection con=conFac.createConnection();  
              
			System.out.println("Soap Request:");
			reqMsg.writeTo(System.out);
            SOAPMessage reply=con.call(reqMsg, "http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx");  
            System.out.println();  
            //reply.writeTo(System.out);  
               
            NodeList l=(NodeList)reply.getSOAPBody().getChildNodes();  
            Node result=(Node)l.item(0);  
            System.out.println("Soap Response:");
            System.out.println(result.getTextContent());  
              
        } catch (Exception e) {  
        	System.out.println("Error :"+e.getMessage());
        }    
       return reqMsg;  
   } 	

   //纯SOAP动态调用Web Service API
   public static SOAPMessage runSOAP(String methodName,String[][] args){  
       MessageFactory fac=null;  
       SOAPMessage reqMsg=null;  
       try {  
            fac=MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);  
            reqMsg=fac.createMessage();  
               
            reqMsg.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "UTF-8");  
            reqMsg.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");  
              
            SOAPEnvelope env=reqMsg.getSOAPPart().getEnvelope();  
            env.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");  
            env.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
            env.addNamespaceDeclaration("soap", "http://schemas.xmlsoap.org/soap/envelope/");  
              
            MimeHeaders mHers=reqMsg.getMimeHeaders();  
            mHers.setHeader("SOAPAction", "http://apiconnector.com/"+methodName);  
                
            SOAPBody body = reqMsg.getSOAPBody();  
            SOAPElement CheckOnline = body.addChildElement(methodName,"","http://apiconnector.com");  
              
            if(args != null && args.length >0){
            	SOAPElement username = null;
            	for(int i=0;i<args.length;i++){
            		username = CheckOnline.addChildElement(args[i][0]);
            		username.addTextNode(args[i][1]);
            	}
            }
		      
            SOAPConnectionFactory conFac=SOAPConnectionFactory.newInstance();  
            SOAPConnection con=conFac.createConnection();  
              
			System.out.println("Soap Request:");
			reqMsg.writeTo(System.out);
            System.out.println();  
            SOAPMessage reply=con.call(reqMsg, "http://apiconnector.com/API.asmx");  
               
            NodeList l=(NodeList)reply.getSOAPBody().getChildNodes();  
            Node result=(Node)l.item(0);  
            System.out.println("Soap Response:");
            System.out.println(result.getTextContent());   
        } catch (Exception e) {  
        	System.out.println("Error :"+e.getMessage());
        }    
       return reqMsg;  
   } 	

   //读取html文件内容 
   public static String getLocalHtmlStream(String path) {
        String str = "";
        FileReader fr;
		try {
			fr = new FileReader(path);
			
	        int num;
	        while ((num = fr.read()) != -1) {
	            str += (char)num;
	        }
	        fr.close();
		} catch (Exception e) {}
        
        return str;
    }
      
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值