public Document send(String in0,String in1) throws Exception {
try {
StringBuffer parameter = new StringBuffer();
parameter.append("");
parameter.append(" ");
parameter.append("");
parameter.append("");
parameter.append("").append(in0).append(" ");
parameter.append(" ");
parameter.append(" ");
parameter.append(" ");
parameter.append(" ");
System.out.println(parameter.toString());
String soap = parameter.toString();
if (soap == null) {
return null;
}
URL url = new URL(this.service_url);
URLConnection conn = url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
// 报文要设置为xml
conn.setRequestProperty("Content-Length", Integer.toString(soap
.length()));
conn.setRequestProperty("Content-Type", "text/xml; charset="
+ requestEncoding.toLowerCase());
conn.setRequestProperty("SOAPAction", "");
OutputStream outputStream = conn.getOutputStream();
// 设置编码为UTF-8
OutputStreamWriter outputSW = new OutputStreamWriter(outputStream,
requestEncoding);
outputSW.write(soap);
outputSW.flush();
outputSW.close();
InputStream response = conn.getInputStream();
return parseResponseToXml(response);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public Document parseResponseToXml(InputStream response) {
Document document = null;
SAXReader reader = new SAXReader();
Map map = new HashMap();
reader.getDocumentFactory().setXPathNamespaceURIs(map);
try {
// 将输入流转化为document
document = reader.read(response);
if (document != null) {
String receivedMsg = document.asXML().replace("><", ">\n<");
}
} catch (Exception e) {
e.printStackTrace();
}
return document;
}
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
/**
* @ClassName AxisClientUtil
* @Description WebService调用类
* @Author weizhi2018
* @Date 2013-7-18 10:27:09
*
*/
public class AxisClientUtil {
// 请求的WebService地址(不带"?wsdl")
private String url;
// WSDL文件的命名空间名,即元素的targetNamespace属性值
private String namespace;
//接口地址的用户名和密码
private String authuser;
private String authpass;
/**
* @param url WSDL地址
* @param namespace WSDL命名空间
*/
public AxisClientUtil(String url, String namespace) {
this.namespace = namespace;
this.url = url;
}
/**
* @param url
* @param namespace
* @param authuser
* @param authpass
*/
public AxisClientUtil(String url, String namespace,String authuser,String authpass) {
this.namespace = namespace;
this.url = url;
this.authuser = authuser;
this.authpass = authpass;
}
/**
* @Title send
* @Description 发送WebService请求
* @Author weizhi2018
* @param @param method 请求WebService服务的方法的名字
* @param @param opAddEntryArgs 请求的入参数组
* @param @return 返回请求处理结果
* @return String
* @throws
*/
public String send(String method, Object[] opAddEntryArgs) {
String result = null;
RPCServiceClient serviceClient;
try {
serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
// 请求的URL
EndpointReference targetEPR = new EndpointReference(this.url);
options.setTo(targetEPR);
//验证属性
options.setUserName(this.authuser);
options.setPassword(this.authpass);
// 调用的WebService的方法名字,QName对象
QName opAddEntry = new QName(this.namespace, method);
// 参数数组,按顺序组织数组,不能为null
if (opAddEntryArgs == null) {
opAddEntryArgs = new Object[]{};
}
// 指定返回值的类型
Class[] classes = new Class[]{String.class};
// 发送WebService请求
result = (String) serviceClient.invokeBlocking(opAddEntry,
opAddEntryArgs, classes)[0];
// OMElement element=serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs);
// System.out.println(element.toString());
} catch (AxisFault e) {
e.printStackTrace();
}
return result;
}
}
TestCase
import java.io.File;
import junit.framework.TestCase;
import org.dom4j.Document;
import org.junit.Assert;
import com.bestpay.webservice.xml.XmlUtil;
/**
* @ClassName AxisClientUtilTest
* @Description TODO
* @Author weizhi2018
* @Date 2013-7-18 下午12:27:30
*
*/
public class AxisClientUtilTest extends TestCase {
private AxisClientUtil axisClient = null;
/*
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception {
String namespace="http://namespace.com"; //换成自己的wsdl的命名空间
String url="http://ip:7012/bill/services/businessService"; //换成自己的接口地址
axisClient = new AxisClientUtil(url,namespace);
}
/*
* @see junit.framework.TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
}
/**
* Test method for {@link com.bestpay.webservice.client.AxisClientUtil#send(java.lang.String, java.lang.String)}.
*/
public void testSend() {
//入参1
String in0 = "A00008|0770000001|V1.0|58.63.47.115";
//入参2
Document doc = XmlUtil.getDocument(new File("test/xml/checknum.xml"));
String in1 = doc.asXML();
//入参数组
Object[] param = new Object[]{in0,in1};
//要请求的WebService服务的方法名字
String method = "dispatchCommand";
String result = axisClient.send(method,param);
System.out.println(result);
//解析xml结果
Document retdoc = XmlUtil.getDocument(result);
//返回码
String retCode = XmlUtil.getElementByName(retdoc.getRootElement(), "RESPONSECODE").getText();
System.out.println(retCode);
//
Assert.assertEquals("000000", retCode);
}
}
4,使用wsdl2java把WSDL文件转成本地类,然后像本地类一样使用