java调用webservice接口的两种方式——http和URLConnection
URLConnection方式:
public class GetHttp {
public static final String JFXXDOWN = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:sx=\"http://www.xx.xx.cn\">"
+"<soapenv:Header/>"
+"<soapenv:Body>"
+"<sx:DownSwService>"
+"<sx:ome>"
+"<in:business xmlns:in=\"http://www.xx.xx.cn/\">"
+"<para>"
+"<SJBLX>XXXXX</SJBLX>"
+"</para>"
+"</in:business>"
+"</sx:ome>"
+"</sx:DownSwService>"
+"</soapenv:Body>"
+"</soapenv:Envelope>";
public String send(String url1, String soap) {
// TODO Auto-generated method stub
Document reqDoc = null;
StringBuffer sb=new StringBuffer();
try {
URL url = new URL(url1);
URLConnection conn = url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
// conn.setRequestProperty("Content-Length",
// Integer.toString(soap.length()));
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
// conn.setRequestProperty("SOAPAction", SOAPAction);
OutputStream os = conn.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
osw.write(soap);
osw.flush();
osw.close();
InputStream is = conn.getInputStream();
//获取返回报文
BufferedReader bis=new BufferedReader(new InputStreamReader(is));
String temp="";
while((temp=bis.readLine())!=null){
sb.append(temp+"\n");
}
} catch (Exception e) {
e.printStackTrace();
return sb.toString();
}
return sb.toString();
}
public static void main(String[] args)throws Exception {
GetHttp soap = new GetHttp();
String url=GxptConstant.URL;
String soapxml=JFXXDOWN;
String soapResDoc = soap.send(url,soapxml);// 得到返回的soap xml
System.out.println("soapResDoc:="+soapResDoc);
}
}
Call方式:
public static void importDoc(String sZipFile){
String sTargetEndpointAddress =
"http://192.168.xx.xx/wcm/services/trswcm:ImportxxService";
Service service = new Service();
Call call = null;
String invoke="";
String issuccess2="";
try {
call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(sTargetEndpointAddress));
call.setOperationName(new QName(sTargetEndpointAddress,
"importDocuments"));
invoke = (String) call.invoke(new Object[]
{CMyFile.readBytesFromFile(sZipFile), "zip"});
} catch (Exception e) {
e.printStackTrace();
}
}
call方式调用案例链接:
https://blog.csdn.net/qq_35008624/article/details/83744954