最近在项目开发中遇到对接系统提供的接口为webservice接口,第一次调用webservice通过查询尝试了以下两种调用方式:
方式一:普通接口调用
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class texst {
public static void main(String[] args) throws IOException, MalformedURLException {
String url ="http://xxx.xxx.xxx.xxx:xxx/cwbase/jtgl/ys/YuSuanService.asmx";
URL wsUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) wsUrl.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setConnectTimeout(5*1000);
conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
conn.setRequestProperty("Content-Length", "length");
conn.setRequestProperty("SOAPAction", "http://tempuri.org/YuSuanQuery");
String req ="<?xml version=\"1.0\" encoding=\"utf-8\"?>";
req+="<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/\">";
req+="<soap:Body>";
req+="<YuSuanQuery xmlns=\"http://tempuri.org/\">";
req+="<query>";
req+="<USER>jkcs</USER>";
req+="<HSZZBH>00010503</HSZZBH>";
req+="<YSQJ>202203</YSQJ>";
req+="<SJLB>0201</SJLB>";
req+="<HSXMBH>660100030027</HSXMBH>";
req+="<FYXMBH>660100030027</FYXMBH>";
req+="</query>";
req+="</YuSuanQuery>";
req+="</soap:Body>";
req+="</soap:Envelope>";
OutputStream os = conn.getOutputStream();
os.write(req.getBytes());
// OutputStreamWriter os = new OutputStreamWriter(conn.getOutputStream(), "utf-8");
// os.write(String.valueOf(req.getBytes()));
InputStream is = conn.getInputStream();
byte[] b = new byte[1024];
int len = 0;
String s = "";
while((len = is.read(b)) != -1){
String ss = new String(b,0,len,"UTF-8");
s += ss;
}
is.close();
os.close();
conn.disconnect();
System.out.println("服务器返回数据:"+s);
}
}
其中:YuSuanQuery 为方法名
方式二:借助wsdl
首先,找到本地jdk文件夹下的bin目录其中有个wsimport.exe就是借助改应用生成后端代码; wsimport.exe所在目录
其次在bin目录下打开cmd命令窗口如图:
在该目录下执行:
wsimport -s D:\ http://10.29.197.177:5200/cwbase/jtgl/ys/YuSuanService.asmx?wsdl
命令
命名执行完后会在D盘生成org文件夹里面有自动生成的java文件,将文件都复制到工程中主要使用以下两个java类进行接口调用即可
参考CSDN: https://blog.csdn.net/qq_41694906/article/details/88029533 https://blog.csdn.net/qq_35124535/article/details/62226585#t2