axisとaxis 2を使ってwebserviceを呼び出します。

5880 ワード

1つ   axisを使ってwebserviceを呼び出します。
1.インポートパッケージ
activations.jar axis.jar commons-discovery-02.jar commons-loging-1.04.jar jar jaxrpc.jar mail.jar wsdl 4 j-1.5.jar
2.コード
 
  

private void getWeather(){ try{ // String url = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl"; String url="http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx"; String city=" "; // String soapaction="http://WebXml.com.cn/"; Service service = new Service(); Call call=(Call) service.createCall();

call.setTargetEndpointAddress(url); // // call.setOperationName(new QName(soapaction,"getSupportCityString")); call.setOperationName(new QName(soapaction,"getWeatherbyCityName")); // call.addParameter(new QName(soapaction,"theCityName"), org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN); // call.addParameter(new QName(soapaction,"theRegionCode"), org.apache.axis.encoding.XMLType.XSD_STRING, // javax.xml.rpc.ParameterMode.IN); // call.setReturnType(new QName(soapaction,"getWeatherbyCityName"),Vector.class); // call.setReturnType(new QName(soapaction,"getSupportCityString"),Vector.class); call.setUseSOAPAction(true); call.setSOAPActionURI(soapaction+"getWeatherbyCityName"); // call.setSOAPActionURI(soapaction+"getSupportCityString"); Vector v=(Vector)call.invoke(new Object[]{city}); for (int i = 0; i

 
   
  

axis2 webservice

1.

axiom-api-1.2.11.jar
axiom-impl-1.2.11.jar
axis2-adb-1.5.5.jar
axis2-kernel-1.5.5.jar
axis2-transport-http-1.5.5.jar
axis2-transport-local-1.5.5.jar
commons-codec-1.3.jar
commons-httpclient-3.1.jar
commons-io.jar
commons-logging-1.1.1.jar
httpcore-4.0.jar
mail-1.4.jar
neethi-2.0.5.jar
wsdl4j-1.6.2.jar
XmlSchema-1.4.3.jar

2.

OMElement ServiceClient ( )

xml OMElement

private static void invokeWebService() 
    { 
        try 
        { 
            String soapBindingAddress = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl"; 
            
            EndpointReference endpointReference = new EndpointReference(soapBindingAddress); 
            
            //    OMFactory,   namespace、            
//            OMFactory factory = OMAbstractFactory.getOMFactory(); 
            //      
//            OMNamespace namespace = factory.createOMNamespace("http://WebXml.com.cn/", "web"); 
            
           /* //      method  ,"getSupportCityString"     
            OMElement method = factory.createOMElement("getSupportCityString", namespace); 
            OMElement value1 = factory.createOMElement("theRegionCode", namespace);//          
           //              buildParma  
            value1.addChild(factory.createOMText(value1, "  "));//      
//            value1.setText("  ");
            method.addChild(value1);//      */
            
            //      
            Options options = new Options(); 
            options.setAction("http://WebXml.com.cn/getSupportCityString");  
            options.setTo(endpointReference); 
            
            ServiceClient sender = new ServiceClient(); 
            sender.setOptions(options); 
            
            //       ,  ,    ,       
//            OMElement result = sender.sendReceive(buildParam("getSupportCityString",new String[]{"theRegionCode"},new String[]{"  "}));   
//            OMElement med=buildParam("getSupportCityString",new String[]{"theRegionCode"},new String[]{"  "});
//            System.out.println(med.toString());
            OMElement methods =getStrToOme("E://test.xml");
            OMElement result = sender.sendReceive(methods);

            //        
            System.out.println(result.toString()); 
        } 
        catch (AxisFault ex) 
        { 
            ex.printStackTrace(); 
        } 

    } 


public static OMElement getStrToOme(String filename){
    	
    	String xmlStr;
        OMElement msg=null;
		try {
			xmlStr = FileUtils.readFileToString(new File(filename));
			String encoding="UTF-8";
			msg=toOMElement(xmlStr, encoding);
	    	System.out.println(msg.toString());
	    	
	    	
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return msg;
    }


public static OMElement toOMElement(String xmlStr, String encoding) {
    	   OMElement xmlValue;
    	   try {
    	   xmlValue = new StAXOMBuilder(new ByteArrayInputStream(xmlStr
    	   .getBytes(encoding))).getDocumentElement();
    	   return xmlValue;
    	   } catch (Exception e) {
    	   return null;
    	   }
    }


/**
     * @see   webservice           
     * @return
     */
    public static  OMElement buildParam(String method,String[] arg,String[] val) {
    	 OMFactory fac = OMAbstractFactory.getOMFactory();
    	 OMNamespace omNs = fac.createOMNamespace("http://WebXml.com.cn/", "web");

           OMElement data = fac.createOMElement(method, omNs);
           for(int i=0;i