JavaServlet调用Web Service的方法--转载

测试的Web Service是以前用AXIS部署的一个LBS服务接口,源代码如下(敏感数据被隐):

  //MyWebService.java

   import java.io.*;
   import java.util.Date;
   import java.net.*;
   import java.text.*;
   //import java.rmi.*;
   import javax.servlet.*;
   import javax.servlet.http.*;
   import javax.xml.namespace.QName;

   import org.apache.axis.Constants;
   import org.apache.axis.client.Service;
   import org.apache.axis.client.Call;
   import org.apache.axis.encoding.XMLType;
   import javax.xml.rpc.ParameterMode;
   //import javax.xml.rpc.ServiceException;

public class MyWebService extends HttpServlet {
 
 static String lbs_location = "http://xxx.xxx.xxx.xxx/axis/services/LBSSoapService?wsdl";
 static String lbs_getLocationXY_SoapAction = "http://xxx.xxx.xxx.xxx/axis/services/LBSSoapService/getLocationXY";
 
 static String gis_location = "http://xxx.xxx.xxx.xxx/axis/services/LBSGISService?wsdl";
 static String gis_getLocationStr_SoapAction = "http://xxx.xxx.xxx.xxx/axis/services/LBSGISService/getLocationStr";
 static String gis_getLocationMap_SoapAction = "http://xxx.xxx.xxx.xxx/axis/services/LBSGISService/getLocationMap";
 
 public final static SimpleDateFormat  sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
 
 public MyWebService() {
  super();
 }

 public void destroy() {
  super.destroy(); 
 }

 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
   
  response.setContentType("text/html;charset=GB2312"); 
  PrintWriter out = response.getWriter();
  String phone = request.getParameter("phone");
  
  if(phone == null) 
  {
   out.println("被查询手机号码不正确!");
   return;
  }
  
  try{ 
   out.println(sdf.format(new Date()) + " -> 正在进行用户定位操作 ...<br>");
   Service service = new Service();
   Call call = (Call)service.createCall();
   call.setTargetEndpointAddress(new URL(lbs_location));
   call.setOperationName(new QName("http://xxx.xxx.xxx.xxx/axis/services/LBSSoapService","getLocationXY"));
   call.addParameter("in0",Constants.XSD_INT,ParameterMode.IN);
   call.addParameter("in1",Constants.SOAP_STRING,ParameterMode.IN);
   call.addParameter("in2",Constants.SOAP_STRING,ParameterMode.IN);
   call.addParameter("in4",Constants.XSD_INT,ParameterMode.IN);
   
   call.setReturnType(XMLType.SOAP_STRING);
   
   call.setUseSOAPAction(true);
   call.setSOAPActionURI(lbs_getLocationXY_SoapAction);   
   String ret = (String)call.invoke(new Object[] {new Integer(1),phone,phone,new Integer(7)});

   String r_code = getItemString(ret,1);
   if(r_code != null && r_code.equals("0"))
   {
    String ps = getItemString(ret,0);
    out.println(sdf.format(new Date()) + " <- 成功(X,Y): " + ps + "<br>");
    out.println(sdf.format(new Date()) + " -> 正在获取用户位置描述 ... <br>");
    
    String[] Points = ps.split(",");
    
    call.setTargetEndpointAddress(new URL(gis_location));
    call.setOperationName(new QName("http://xxx.xxx.xxx.xxx/axis/services/LBSGISServic","getLocationStr"));

call.removeAllParameters();
    
    call.addParameter("in0",Constants.SOAP_STRING,ParameterMode.IN);
    call.addParameter("in1",Constants.SOAP_STRING,ParameterMode.IN);
    
    call.setReturnType(XMLType.SOAP_STRING);
    
    call.setUseSOAPAction(true);
    call.setSOAPActionURI(gis_getLocationStr_SoapAction);   
    ret = (String)call.invoke(new Object[] {Points[0],Points[1]});
    
    r_code = getItemString(ret,1);
    
    if(r_code != null && r_code.equals("0"))
    {
     out.println(sdf.format(new Date()) + " <- 成功: " + getItemString(ret,0) + "<br>");
     out.println(sdf.format(new Date()) + " -> 正在获取用户地图信息 ... <br>");
     
     call.setTargetEndpointAddress(new URL(gis_location));
     call.setOperationName(new QName("http://xxx.xxx.xxx.xxx//axis/services/LBSGISServic","getLocationMap"));
     
     call.removeAllParameters();
     
     call.addParameter("in0",Constants.SOAP_STRING,ParameterMode.IN);
     call.addParameter("in1",Constants.SOAP_STRING,ParameterMode.IN);
     call.addParameter("in2",Constants.XSD_INT,ParameterMode.IN);
     call.addParameter("in3",Constants.XSD_INT,ParameterMode.IN);
     call.addParameter("in4",Constants.XSD_INT,ParameterMode.IN);
     call.addParameter("in5",Constants.SOAP_STRING,ParameterMode.IN);
     call.addParameter("in6",Constants.SOAP_STRING,ParameterMode.IN);
     call.addParameter("in7",Constants.XSD_BOOLEAN,ParameterMode.IN);
     call.addParameter("in8",Constants.SOAP_STRING,ParameterMode.IN);
     
     call.setReturnType(XMLType.SOAP_STRING);
     
     call.setUseSOAPAction(true);
     call.setSOAPActionURI(gis_getLocationMap_SoapAction);   
     ret = (String)call.invoke(new Object[] {Points[0],Points[1],new Integer(472),new Integer(330),new Integer(200),"image/jpg","#FFFFFF",new Boolean(true),"Marker1"});
     
     r_code = getItemString(ret,1);
     
     if(r_code != null && r_code.equals("0"))
     {
      out.println(sdf.format(new Date()) + " <- 成功: <a href='" + getItemString(ret,0).replaceAll("http://gzgisapp","http://xxx.xxx.xxx.xxx") + "' target='_blank'>" + getItemString(ret,0).replaceAll("http://gzgisapp","http://xxx.xxx.xxx.xxx").split(";")[0] + "</a>");
     }
     else
     {
      out.println(sdf.format(new Date()) + " <- 失败,错误码:" + r_code + " " + getItemString(ret,0));
     }
    }
    else
    {
     out.println(sdf.format(new Date()) + " <- 失败,错误码:" + r_code + " " + getItemString(ret,0));
    }    
   }
   else
   {
    out.println(sdf.format(new Date()) + " <- 失败,错误码:" + r_code + " " + getItemString(ret,0)); 
   }
  }
  catch (Exception e){
   out.println(e.toString());
   e.printStackTrace();
  }
 }
 
 private static String getItemString(String Msg,int index){
  String[] ns = Msg.split("~");
  if(ns.length >=2)
  {
   return ns[index];
  }
  
  return null;
 }
 
 public void init() throws ServletException {
  
 }

}
--------------------- 
作者:钟奎 
来源:CSDN 
原文:https://blog.csdn.net/zhongking/article/details/715044 
 

猜你喜欢

转载自blog.csdn.net/qsl319/article/details/83717740
今日推荐