需要的jar包:
代码如下:
1 package com.dyf.client; 2 3 import java.io.IOException; 4 import java.net.MalformedURLException; 5 import java.net.URL; 6 import java.rmi.RemoteException; 7 8 import javax.xml.namespace.QName; 9 import javax.xml.rpc.ServiceException; 10 11 import org.apache.axis.AxisFault; 12 import org.apache.axis.client.Call; 13 import org.apache.axis.client.Service; 14 import org.apache.axis.encoding.XMLType; 15 import org.apache.cxf.endpoint.Client; 16 import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; 17 import org.apache.cxf.transport.http.HTTPConduit; 18 import org.apache.cxf.transports.http.configuration.HTTPClientPolicy; 19 import org.apache.http.Header; 20 import org.apache.http.client.config.RequestConfig; 21 import org.apache.http.client.methods.CloseableHttpResponse; 22 import org.apache.http.client.methods.HttpGet; 23 import org.apache.http.impl.client.CloseableHttpClient; 24 import org.apache.http.impl.client.HttpClients; 25 26 public class SimpleClient { 27 28 public static void main(String[] args){ 29 CloseableHttpClient httpclient = HttpClients.createDefault(); 30 RequestConfig requestConfig = RequestConfig.custom() 31 .setSocketTimeout(1000) 32 .setConnectTimeout(1000) 33 .build(); 34 35 HttpGet httpGet1 = new HttpGet("http://www.baidu.com"); 36 httpGet1.setConfig(requestConfig); 37 CloseableHttpResponse response1; 38 try { 39 response1 = httpclient.execute(httpGet1); 40 Header[] headers =response1.getAllHeaders(); 41 for (Header header : headers) { 42 System.out.println(header.toString()); 43 } 44 } catch (IOException e) { 45 e.printStackTrace(); 46 } 47 48 //axis 49 Call call; 50 try { 51 call = (Call) new Service().createCall(); 52 call.setTargetEndpointAddress(new URL("http://localhost:9001/Service/HelloWorld")); 53 QName methodObj = new QName("http://service.dyf.com/","getValue"); 54 call.setOperationName(methodObj); 55 // call.addParameter("arg0", XMLType.XSD_ANYTYPE,ParameterMode.IN);//接口的参数 56 call.addParameter("arg0", null, null); 57 call.setReturnType(XMLType.XSD_STRING);//设置返回类型 58 Object[] paras ={"dashen"}; 59 Object res = call.invoke(paras); 60 System.out.println(res); 61 } catch (ServiceException e) { 62 e.printStackTrace(); 63 } catch (MalformedURLException e) { 64 e.printStackTrace(); 65 } catch (AxisFault e) { 66 e.printStackTrace(); 67 } catch (RemoteException e) { 68 e.printStackTrace(); 69 } 70 71 //cxf01 72 try { 73 Client client = JaxWsDynamicClientFactory.newInstance().createClient("http://localhost:9002/Service/HelloWorld4j?wsdl"); 74 HTTPConduit http = (HTTPConduit) client.getConduit(); 75 HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy(); 76 http.setClient(httpClientPolicy); 77 Object[] res = client.invoke("getValue", "dashen"); 78 System.out.println(res[0]); 79 } catch (Exception e) { 80 e.printStackTrace(); 81 } 82 83 84 85 } 86 }
其中遇到的错误:
Exception in thread "main" java.lang.NoSuchMethodError: javax.wsdl.xml.WSDLReader.readWSDL(Ljavax/wsdl/xml/WSDLLocator;Lorg/w3c/dom/Element;)Ljavax/wsdl/Definition;
java.lang.ClassNotFoundException: com/sun/tools/internal/xjc/api/XJC 问题,最后发现是因为配置jdk环境的时候用的是jre,改成jdk就可以了
jar包冲突问题;在tomcat下lib下有个wsdl-1.5.jar,而在cxf中则要使用的wsdl-1.6.jar;
需注意的地方:
cxf的地址后需加上“?wsdl”;
axis:传值是按照arg0,arg1传入的,所以只需在addParameter中设置arg0,arg1就可以了。
本文值是练习,欢迎多多指正,共同学习。