Java调用WebService

import java.net.URL;
import javax.xml.namespace.QName;
import org.apache.axis.client.*;
import org.apache.axis.encoding.XMLType;

import javax.xml.rpc.ParameterMode;

public class webservice {
 private String wsdlUrl;
 private String nameSpace;
 private String method;

 public webservice(String wsdlUrl_in, String nameSpace_in, String method_in) {
  this.wsdlUrl = wsdlUrl_in;
  this.nameSpace = nameSpace_in;
  this.method = method_in;
 }

 public String getWsdlUrl() {
  return wsdlUrl;
 }

 public void setWsdlUrl(String wsdlUrl) {
  this.wsdlUrl = wsdlUrl;
 }

 public String getNameSpace() {
  return nameSpace;
 }

 public void setNameSpace(String nameSpace) {
  this.nameSpace = nameSpace;
 }

 public String getMethod() {
  return method;
 }

 public void setMethod(String method) {
  this.method = method;
 }

 public String request(String message) {
        try {

            Service service = new Service();
            Call call = (Call)service.createCall();
           
            // call.setTargetEndpointAddress(new java.net.URL(this.wsdlUrl));
            call.setTargetEndpointAddress(new URL(this.wsdlUrl));
           
            //设置要调用的方法
            call.setOperationName(new QName(this.nameSpace, this.method));
           
            //该方法需要的参数
            call.addParameter(new QName(this.nameSpace,"MsgData"),
              org.apache.axis.encoding.XMLType.XSD_STRING,
              javax.xml.rpc.ParameterMode.IN);
           
            //方法的返回值类型
            call.setReturnType(org.apache.axis.Constants.XSD_STRING);
           
            call.setUseSOAPAction(true);
            call.setSOAPActionURI(this.nameSpace+this.method);

            //调用该方法
            String response = call.invoke(new Object[] {message}).toString();
            //Object response = call.invoke(new Object[] {message});
            System.out.println( "Result: " + response.toString());
            return "34";
        } catch (Exception e){
            System.err.println(e.toString());
            return e.toString();
        }
    }
 public static void main(String args[]){
  String msgin ="<?xml version='1.0' encoding='utf-8' ?> <soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><ReceiveABS xmlns='http://www.kaoqinji.com/'><MsgData><GD_ABSRSLT_MREQ><EMPLID>123456</EMPLID><PIN_NUM>250041</PIN_NUM><START_DT>2013-01-01</START_DT><START_TIME>08:00:00</START_TIME><END_DT>2013-01-04</END_DT><END_TIME>17:00:00</END_TIME><ALTER_EMPLID></ALTER_EMPLID></GD_ABSRSLT_MREQ></MsgData></ReceiveABS></soap:Body></soap:Envelope>";
  //System.out.println( "Result: ");
  new webservice("http://IP/WebService.asmx?wsdl","http://tempuri.org/","HelloWorld").request(msgin);
 }
}

需要的jar包:wsdl4j-1.0.jar   mail-1.4.1.jar  commons-discovery-20040218.194635.jar   axis-ant.jar   axis.jar   commons-logging-1.1.2.jar

下载Jar包的网站: 

http://www.findjar.com/index.x

原文地址:https://www.cnblogs.com/flyrain/p/Java_WebService.html