在网上查阅了很多关于java后台调用webservice接口的例子,但在我项目中一直无法通过,后来还是通过拼接请求报文才解决,如下是具体代码:
public static void main(String[] args) throws IOException {
String url ="http://localhost:8080/HE_USD_Server_App/services/PhoneInterfaceService";
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("SOAPAction",
url);
String req =" <soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns0=\"http://commonInterface.bpm.ultrapower.com\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
req+="<soapenv:Body>";
req+="<ns0:USDLoginService>";
req+="<arg0>";
req+="<![CDATA[<?xml version=\"1.0\" encoding=\"UTF-8\"?><opDetail>";
req+="<userLoginName>5002</userLoginName>";
req+="<passWord>68CF63C62BC68D71FC41C028375E2F6E</passWord>";
req+="<appType>android</appType>";
req+="</opDetail>]]>";
req+="</arg0>";
req+="</ns0:USDLoginService>";
req+="</soapenv:Body> </soapenv:Envelope>";
OutputStream os = conn.getOutputStream();
os.write(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);
}
ps:
USDLoginService 为接口方法名称