1. 程式人生 > >程式碼_AXIS:呼叫webservice介面傳送soap報文

程式碼_AXIS:呼叫webservice介面傳送soap報文

一、拼接報文的方式:

    (1)利用StringBuffer來拼接字串,程式碼如下:

                StringBuffer soapData = new StringBuffer();
		soapData.append("<?xml version=\"1.0\"?>");
		soapData.append("<ImageSearch provider=\"ATM-WF\" orgid=\"99\" branchid=\"99\" businessorgid=\"99\" busine                                    ssbranchid=\"ATM99\" userid=\"");
		soapData.append(userId);		
		soapData.append("\" type=\"FR_BaseView\">");
		soapData.append("<InitSoapAddress>");
		soapData.append("http://10.112.**.*:8004/ImageViewPortal/services/ImageViewService?wsdl");
		soapData.append("</InitSoapAddress>");
		soapData.append("<InitSoapService>ImageViewService</InitSoapService>");
		soapData.append("<InitSoapAction>execute</InitSoapAction>");
		soapData.append("<Input>");
		soapData.append("<QueryTerm1>");
		soapData.append(signImageIndex);
		soapData.append("</QueryTerm1>");
		soapData.append("</Input>");
		soapData.append("</ImageSearch>");
		return soapData.toString();
    (2)利用dom4j拼接xml檔案,程式碼如下:
public static void main(String args[]) {
		Document document = DocumentHelper.createDocument();	//宣告document
		Element root = DocumentHelper.createElement("ImageSearch");		//根節點		
		document.setRootElement(root);	//將根節點放入document
		root.addAttribute("provider","ATM-WF");	//新增屬性
		root.addAttribute("branchId ", "99");
		Element InitSoapAddress = root.addElement("InitSoapAddress");	//子節點
		InitSoapAddress.addText(" http://10.112.*.***:8***/ImageViewPortal/services/ImageViewService?wsdl");	//新增內容
		Element InitSoapService = root.addElement("InitSoapService");
		InitSoapService.setText("ImageViewService");
		Element InitSoapAction = root.addElement("InitSoapAction");
		InitSoapAction.addText("execute");	
		XMLWriter str = new XMLWriter();	
		try {
			str.write(document);	//控制檯輸出
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

二、利用AXIS的call方法傳送報文,程式碼如下:
	public static String serUrl = "http://10.112.**.*:80**/ImageViewPortal/services/ImageViewService?wsdl";//訪問介面地址
	public static String nameSpace = "http://10.112.**.*:80**/ImageViewPortal/services/ImageViewService";//wsdl檔案中的namespace

		String response = null;
	        String parameter =getBASE64(xmlSoap);		//對報文進行BASE64轉碼,可不必
		try {
			Service service = new Service();
			Call call = (Call)service.createCall();
			QName qn = new QName(nameSpace,"execute");
			call.setTargetEndpointAddress(new java.net.URL(serUrl));
			call.setOperationName(qn);
			call.setEncodingStyle("UTF-8");
			call.addParameter("xml", XMLType.XSD_STRING,ParameterMode.IN);//xml為execute方法的引數名,後面定義型別,和方式
			call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//返回報文型別
			response = (String)call.invoke(new Object[]{parameter});
		} catch(ServiceException e) {
			e.printStackTrace();
			logger.error(e);
		} catch (RemoteException e) {
			e.printStackTrace(); 
			logger.error(e);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
三、返回報文取某個節點裡的值,這裡取url(返回報文格式為: <TIV version="1.0.0"><ImageUrls><ImageUrlsrc="http://10.112.**.*:8***/ImageStoreServer//servlet/FileDownLoad?name=286456&amp;businessCode=ImageATM"><Range>1-1</Range></ImageUrl></ImageUrls></TIV>):
		Document document = null;
		try {
			document = DocumentHelper.parseText(releaseCode);
		} catch (DocumentException e) {
			logger.error("ParseTextException");
		}
		Element root = document.getRootElement();//獲取根節點
		Element child = root.element("ImageUrls");//獲取子節點
		Element grandson = child.element("ImageUrl");//獲取子目標節點
		String url= grandson.attributeValue("src");//獲取目標屬性
		System.out.println(url); 
		return url;