webservice接口 跟 webapi 区别:
webservice 做为服务端无法监控接口 ;
webservice接口的实现:
1. 使用Service;
首先准备好json格式的数据,其次必要代码如下
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(url)); //url为接收地址及端口
call.setOperationName("sendERPTodo"); //wsdl中接收数据的方法
call.addParameter("arg0",org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN); //必要参数,具体百度哈哈 只管教你实现代码 这也是大多数人需要的
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING); //注意格式
call.setUseSOAPAction(true);
String result = (String) call.invoke(new Object[] {data.toString() }); // 此处为执行,注意异常处理
2. jdk原生
URL url = new URL("http://192.168.0.101:8089/myservice?wsdl");
// 指定命名空间和服务名称
QName qName = new QName("http://com.soft.ws/my", "MyService");
Service service = Service.create(url, qName);
// 通过getPort方法返回指定接口
MyService myServer = service.getPort(new QName("http://com.soft.ws/my",
"LoginPort"), MyService.class);
// 调用方法 获取返回值
String result = myServer.authorization("admin", "123456");
3.httpClient
HttpClient httpClient =new HttpClient();
PostMethod post = new PostMethod(url); // 这里是post方式
post.addRequestHeader("token", "ybwy2019interface");
post.addRequestHeader("Content-Type", "application/json");
JSONObject json=new JSONObject();
json.put("datas", data);
RequestEntity requestEntity = new StringRequestEntity(data, "application/json;charse=UTF-8", "UTF-8");
post.setRequestEntity(requestEntity);
httpClient.executeMethod(post);
String respStr = post.getResponseBodyAsString(); //获取返回值
post.releaseConnection(); //释放连接
网友评论