一、Soap服务获取WebService接口数据
方法1:使用wsimport进行soap服务进行调用
使用JDK自带的wsimport工具
1,使用cmd控制台将位置切换到项目目录的src下
wsimport -keep http://localhost:8080/hello?wsdl
执行完毕后刷新项目可以看见自动生成的项目,删除.class执行文件,在项目中刷新便可以看见对应的Java文件
2,新建一个方法进行调用
package hello;
import com.theservice.service.Hello;
import com.theservice.service.HelloService;
public class Test {
//Hello与HelloService为wsimport自动生成的
public static void main(String[] args) {
Hello hello=new HelloService().getHelloPort();
System.out.println(hello.hello());
}
}
方法2.使用URLConnection访问Scop服务
调用此方法可以直接返回
urlName为发布的地址
responseinfo使用SOAPUI进行获取,新建项目名称,填写wsdl地址
箭头所指为responseinfo
package com.TheClient.sys.util;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class SoapUtil {
public static String getinfoByScop(String urlPath,String requestBody) throws Exception {
URL url=new URL(urlPath);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
String address=requestBody;
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
OutputStream outputStream=connection.getOutputStream();
outputStream.write(address.getBytes());
int code=connection.getResponseCode();
StringBuffer buffer=new StringBuffer();
if (code==200) {
InputStream inputStream=connection.getInputStream();
byte[] bs=new byte[1024];
Integer len=0;
while ((len=inputStream.read(bs))!=-1) {
buffer.append(new String(bs,0,len,"utf-8"));
}
inputStream.close();
}
outputStream.close();
return buffer.toString();
}
}
方法3,待续