使用axis調用webservice接口


以前使用webservice服務都很簡單,就是根據提供的wsdl接口地址,通過eclipse或者idea自動生成webservice client包,然后直接調用就可以了。這次業務提供的wsdl是需要驗證soapheader的,而且通過IDE工具無法生成可以直接調用的類包,無奈只能通過其他辦法來實現,通過百度,可以使用axis包來實現,具體實現過程如下:

1、需要的jar包依賴

        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>javax.xml.rpc</groupId>
            <artifactId>javax.xml.rpc-api</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.geronimo.bundles</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.4_1</version>
        </dependency>

        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.3</version>
        </dependency>

2、WSDL接口文檔

<?xml version="1.0" encoding="utf-8"?>

<wsdl:definitions 
	xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
	xmlns:s="http://www.w3.org/2001/XMLSchema" 
	xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" 
	xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" 
	xmlns:tns="http://tempuri.org/" 
	xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
	xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" 
	xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" 
	xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" targetNamespace="http://tempuri.org/">  
  <wsdl:types> 
    <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/"> 
      <s:element name="SendXMLFile"> 
        <s:complexType> 
          <s:sequence> 
            <s:element minOccurs="0" maxOccurs="1" name="xmlProjectData" type="s:base64Binary"/>  
            <s:element minOccurs="0" maxOccurs="1" name="reportDate" type="s:string"/> 
          </s:sequence> 
        </s:complexType> 
      </s:element>  
      <s:element name="SendXMLFileResponse"> 
        <s:complexType> 
          <s:sequence> 
            <s:element minOccurs="0" maxOccurs="1" name="SendXMLFileResult" type="s:string"/> 
          </s:sequence> 
        </s:complexType> 
      </s:element>  
      <s:element name="UserSoapHeader" type="tns:UserSoapHeader"/>  
      <s:complexType name="UserSoapHeader"> 
        <s:sequence> 
          <s:element minOccurs="0" maxOccurs="1" name="UserId" type="s:string"/>  
          <s:element minOccurs="0" maxOccurs="1" name="PassWord" type="s:string"/> 
        </s:sequence>  
        <s:anyAttribute/> 
      </s:complexType> 
    </s:schema> 
  </wsdl:types>  
  <wsdl:message name="SendXMLFileSoapIn"> 
    <wsdl:part name="parameters" element="tns:SendXMLFile"/> 
  </wsdl:message>  
  <wsdl:message name="SendXMLFileSoapOut"> 
    <wsdl:part name="parameters" element="tns:SendXMLFileResponse"/> 
  </wsdl:message>  
  <wsdl:message name="SendXMLFileUserSoapHeader"> 
    <wsdl:part name="UserSoapHeader" element="tns:UserSoapHeader"/> 
  </wsdl:message>  
  <wsdl:portType name="DataReportServiceSoap"> 
    <wsdl:operation name="SendXMLFile"> 
      <wsdl:input message="tns:SendXMLFileSoapIn"/>  
      <wsdl:output message="tns:SendXMLFileSoapOut"/> 
    </wsdl:operation> 
  </wsdl:portType>  
  <wsdl:binding name="DataReportServiceSoap" type="tns:DataReportServiceSoap"> 
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>  
    <wsdl:operation name="SendXMLFile"> 
      <soap:operation soapAction="http://tempuri.org/SendXMLFile" style="document"/>  
      <wsdl:input> 
        <soap:body use="literal"/>  
        <soap:header message="tns:SendXMLFileUserSoapHeader" part="UserSoapHeader" use="literal"/> 
      </wsdl:input>  
      <wsdl:output> 
        <soap:body use="literal"/> 
      </wsdl:output> 
    </wsdl:operation> 
  </wsdl:binding>  
  <wsdl:binding name="DataReportServiceSoap12" type="tns:DataReportServiceSoap"> 
    <soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/>  
    <wsdl:operation name="SendXMLFile"> 
      <soap12:operation soapAction="http://tempuri.org/SendXMLFile" style="document"/>  
      <wsdl:input> 
        <soap12:body use="literal"/>  
        <soap12:header message="tns:SendXMLFileUserSoapHeader" part="UserSoapHeader" use="literal"/> 
      </wsdl:input>  
      <wsdl:output> 
        <soap12:body use="literal"/> 
      </wsdl:output> 
    </wsdl:operation> 
  </wsdl:binding>  
  <wsdl:service name="DataReportService"> 
    <wsdl:port name="DataReportServiceSoap" binding="tns:DataReportServiceSoap"> 
      <soap:address location="http://221.226.63.54:8187/DataReportService.asmx"/> 
    </wsdl:port>  
    <wsdl:port name="DataReportServiceSoap12" binding="tns:DataReportServiceSoap12"> 
      <soap12:address location="http://221.226.63.54:8187/DataReportService.asmx"/> 
    </wsdl:port> 
  </wsdl:service> 
</wsdl:definitions>

3、接口調用代碼

public static void sendReportTest(byte[] reportData, String date){
	try {
		// 服務端的url,需要根據情況更改。
		String endpointURL = URL;
		Service service = new Service();
		Call call = (Call) service.createCall();
		call.setTimeout(new Integer(60000));
		call.setTargetEndpointAddress(new URL(endpointURL));
		call.setSOAPActionURI("http://tempuri.org/SendXMLFile");
		call.setOperationName(new QName("http://tempuri.org/","SendXMLFile"));// 設置操作的名稱。
		// 由於需要認證,故需要設置調用的用戶名和密碼。
		SOAPHeaderElement soapHeaderElement = new SOAPHeaderElement("http://tempuri.org/", "UserSoapHeader");
		soapHeaderElement.setNamespaceURI("http://tempuri.org/");
		try {
			soapHeaderElement.addChildElement("UserId").setValue(USER_ID);
			soapHeaderElement.addChildElement("PassWord").setValue(PASSWORD);
		} catch (SOAPException e) {
			e.printStackTrace();
		}
		call.addHeader(soapHeaderElement);
		call.setReturnType(XMLType.XSD_STRING);// 返回的數據類型
		call.addParameter(new QName("http://tempuri.org/","xmlProjectData"), XMLType.XSD_BASE64, ParameterMode.IN);// 參數的類型
		call.addParameter(new QName("http://tempuri.org/","reportDate"), XMLType.XSD_STRING, ParameterMode.IN);// 參數的類型
		String result = (String) call.invoke(new Object[]{reportData,date});// 執行調用
		// 結果信息解析
		Document document = DocumentHelper.parseText(result);
		Element rootElement = document.getRootElement();
		Iterator iter = rootElement.elementIterator("State");
		while(iter.hasNext()){
			Element recordEle = (Element) iter.next();
			String code = recordEle.getTextTrim();// State值
			if("0".equals(code)){ //成功
				Logger.getRootLogger().error("調用接口成功");
			}else{ // 失敗保存log
				Logger.getRootLogger().error(result);
			}
		}
	} catch (Exception e) {
		Logger.getRootLogger().error("調用接口失敗",e);
	}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM