在实际开发中,会遇到各种各样的webService接口,并且对方提供的接口并不规范,一些客户端反而就不好使了,如cxf(客户端与动态调用)等,
直接用java提供的api比较繁琐,这时直接用http request请求更便捷。
直接用java提供的api比较繁琐,这时直接用http request请求更便捷。
可以分为两步,request一个soap消息(xml),然后response一个soap消息(xml),具体如下
/**
* <br>描 述:发送并返回报文消息
* <br>作 者:xieyj
* <br>历 史: (版本) 作者 时间 注释
* @param urlStr wsdl
* @param paraXml 请求的soap消息串,可以用soapui查看
* @param intfMethod 请求的接口方法
* @return
*/
public String sendAndGetResponseData(String urlStr, String paraXml, String intfMethod) {
String respData = "";
try {
URL url = new URL(urlStr);
HttpURLConnection con;
con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
con.setUseCaches(false);
//con.setRequestProperty("Content-type", "text/xml; charset=UTF-8");
//con.setRequestProperty("WSS-Password Type", "PasswordText");
con.setRequestProperty("SOAPAction", intfMethod);
con.setRequestProperty("Encoding", "UTF-8");