手工构造和解析web service客户端访问soap包,仅用于理解wsdl中的相关概念,实际可以用axis的wsdl2java生成同功能的代码。源代码如下:
 
package com.fancy.ws.client;
 
import java.util.Iterator;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.client.ServiceFactory;
import org.apache.axis.message.SOAPEnvelope;
/**
 * 提供ws访问方法的类
 * @author fancy
 *
 */
public class UIServiceSoapStub {
 private static String TARGET_NAMESPACE = " http://example.com/";
 private static String endpointURL = " http://example.com/ui/uiservice";
 private static String service = "uiService";
 
 /**
  * 创建远程方法调用
  * @return
  * @throws ServiceException
  */
 public Call createCall() throws ServiceException {
  System.setProperty("javax.xml.rpc.ServiceFactory",
    "org.apache.axis.client.ServiceFactory");
  ServiceFactory factory = (ServiceFactory) ServiceFactory
    .newInstance();
  QName serviceName = new QName(TARGET_NAMESPACE, service);
  Service service = (Service) factory
    .createService(serviceName);
  Call call = (Call) service.createCall();
  call.setTargetEndpointAddress(endpointURL);
  return call;
 }
 
 /**
  * 构造soap信息封装
  * @return
  * @throws Exception
  */
 public SOAPEnvelope constructSOAPEnvelope() throws Exception {
  org.apache.axis.message.SOAPEnvelope env = new org.apache.axis.message.SOAPEnvelope();
  return env;
 }
 
 /**
  * 构造soap信息头
  * @param envelope
  * @throws Exception
  */
 public void constructHeader(SOAPEnvelope envelope) throws Exception {
  SOAPHeader header = envelope.getHeader();
  Name _AuthenticationName = envelope.createName("Authentication","",TARGET_NAMESPACE);
  SOAPHeaderElement headerElement = header
    .addHeaderElement(_AuthenticationName);
  headerElement.setMustUnderstand(true);
  SOAPElement _Username = headerElement.addChildElement("Username");
  _Username.addTextNode("user123456");
  SOAPElement _Password = headerElement.addChildElement("Password");
  _Password.addTextNode("jngrhdsda");
 }
 
/**
  * 构造soap信息体
  * @param envelope
  * @param mobileNo
  * @throws Exception
  */
 public void constructSearchUserBody(SOAPEnvelope envelope, String mobileNo) throws Exception {
  SOAPBody body = envelope.getBody();
  Name bodyRootElementName = envelope.createName("SearchUser","",TARGET_NAMESPACE);
  SOAPBodyElement bodyRootElement = body
    .addBodyElement(bodyRootElementName);
  SOAPElement _mobileNo = bodyRootElement.addChildElement("mobileNo");
  _mobileNo.addTextNode(mobileNo);
 }
 
/**
   * 查询用户
  * @param mobileNo
  * @return UserInfo 查询到的用户信息
  * @throws Exception
  */
 public UserInfo searchUser(String mobileNo) throws Exception {
   SOAPEnvelope env = this.constructSOAPEnvelope();
   this.constructHeader(env);
   this.constructSearchUserBody(env,mobileNo);
   Call call = this.createCall();
   call.setSOAPActionURI(" http://example.com/SearchUser");
   SOAPEnvelope result = call.invoke(env);
  
   Name _SearchUserResponseName = result.createName("SearchUserResponse","",TARGET_NAMESPACE);
   Name _SearchUserResultName = result.createName("SearchUserResult","",TARGET_NAMESPACE);
   Name _StatusName = result.createName("Status","",TARGET_NAMESPACE);
   Name _IDName = result.createName("ID","",TARGET_NAMESPACE);
   Name _NickNameName = result.createName("NickName","",TARGET_NAMESPACE);
  
   SOAPBody soapBody = result.getBody();
   Iterator it = soapBody.getChildElements(_SearchUserResponseName);
   SOAPElement _SearchUserResponse = (SOAPElement)it.next();
  
   it = _SearchUserResponse.getChildElements(_SearchUserResultName);
   SOAPElement _SearchUserResult = (SOAPElement)it.next();
  
   it = _SearchUserResult.getChildElements(_StatusName);
   SOAPElement _Status = (SOAPElement)it.next();
   String status = _Status.getValue();
  
   it = _SearchUserResult.getChildElements(_IDName);
   SOAPElement _ID = (SOAPElement)it.next();
   String ID = _ID.getValue();
  
   it = _SearchUserResult.getChildElements(_NickNameName);
   SOAPElement _NickName = (SOAPElement)it.next();
   String nickName = _NickName.getValue();
   
   UserInfo ui = new UserInfo(status,ID,nickName);
  
   return ui;
  }
Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐