接口调用工具类可调用接口,代码如下
引入包
import java.io.ByteArrayInputStream;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.message.SOAPEnvelope;
import org.apache.axis.utils.XMLUtils;
import org.w3c.dom.Document;
码主体
public class CallWebservice {
private static Service service = new Service();
public String webservice(String url, String xml) {
SOAPEnvelope rsp = null;
Document resultDoc = null;
Call call = new Call(service);
call.setTargetEndpointAddress(url);
ByteArrayInputStream stream;
try {
stream = new ByteArrayInputStream(xml.trim().getBytes("UTF-8"));
SOAPEnvelope req = new SOAPEnvelope(stream);
rsp = call.invoke(req);
} catch (Exception e) {
e.printStackTrace();
}
try {
resultDoc = rsp.getAsDocument();
} catch (Exception e) {
e.printStackTrace();
}
String resultStr = XMLUtils.DocumentToString(resultDoc);
return resultStr;
}
}