生成XML Axis調用webservice傳送XML數據過去

//      用工廠類建立一個document實例 
   Document doc = DocumentHelper.createDocument(); 
//   建立根元素
   Element root = doc.addElement("synchroFlowReq");
   //建立head
   Element head = root.addElement("head");
   //建立synchroFlow
   Element synchroFlow = head.addElement("synchroFlow");
   //建立name
   Element name = synchroFlow.addElement("name");
   //複製值
   name.setText("審批流程名稱");
   //建立sender
   Element sender = synchroFlow.addElement("sender");
   //建立origin
   Element origin = sender.addElement("origin");
   origin.setText("發送節點代碼");
            //建立origin
   Element processDefNm = sender.addElement("processDefNm");
   processDefNm.setText("模板名稱");
   //建立processInsId
   Element processInsId = sender.addElement("processInsId");
   processInsId.setText("流程實例ID");
            //建立processInsId
   Element activityDefId = sender.addElement("activityDefId");
   activityDefId.setText("節點活動定義ID");
   
   /**
    * 生成XML
    */
   OutputFormat format = OutputFormat.createPrettyPrint(); 
   format.setEncoding("UTF-8");  
   //建立寫出對象  
   XMLWriter writer;
   String path;
   try {
                //指定xml文件生成目錄
       path = TimerTaskUtil.class.getResource("/").getPath();
       System.out.println(path);
    path = path.substring(0, path.indexOf("classes/"));
    writer = new XMLWriter(new FileOutputStream(path+"XMLFile/"+this.id+new Date()+".xml"),format);
    writer.write(doc);
    
    Service s = new  Service();
    Call call = (Call) s.createCall();
    call.setUseSOAPAction(true);
    
    String method[] = (this.counts.trim().toString()).split("\\|");
    
    
    call.setOperationName(new QName(method[1], method[2]));
    call.addParameter("arg1", XMLType.XSD_STRING, ParameterMode.IN);
    call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
    call.setTargetEndpointAddress(method[0]);
    String ret = (String) call.invoke(new Object[] { doc.asXML() });
    write(ret, path+"XMLFile/"+this.id+new Date()+".xml");
    String status = xmlElements(ret);
    if(status.equals("200")){
     System.out.println("WEBSERVICE成功!"+new Date());
    }else{
     System.out.println("WEBSERVICE失敗!"+new Date());
    }
    
   } catch (IOException e) {
     // TODO Auto-generated catch block
   e.printStackTrace(); 
   } catch (ServiceException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
 /*
//      服務的地址
         URL wsUrl;
   try {
    wsUrl = new URL("http://192.168.1.100:6789/hello");
    HttpURLConnection conn;
    conn = (HttpURLConnection) wsUrl.openConnection();
    conn.setDoInput(true);
          conn.setDoOutput(true);
          conn.setRequestMethod("POST");
          conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
          OutputStream os = conn.getOutputStream();
          
          //請求體
          String soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:q0=\"http://ws.itcast.cn/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + 
                        "<soapenv:Body> <q0:sayHello><arg0>aaa</arg0>  </q0:sayHello> </soapenv:Body> </soapenv:Envelope>";
          
          os.write(soap.getBytes());
          
          InputStream is = conn.getInputStream();
          
          byte[] b = new byte[1024];
          int len = 0;
          String s = "";
          while((len = is.read(b)) != -1){
              String ss = new String(b,0,len,"UTF-8");
              s += ss;
          }
          System.out.println(s);
          is.close();
          os.close();
          conn.disconnect();
          
   } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();  
         
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }*/
        
     }
相關文章
相關標籤/搜索