调用webservice

准备条件:
请求调用的方法、参数、请求的URL路径必须知道

private static String getSoapRequest(String areaCode,String content, String infoType, String reportDate) {
StringBuilder sb = new StringBuilder();
sb
.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<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/\">"
+ "<soap:Body>"

//请求调用的方法
+"<pushDateInfo xmlns=\"http://shcholBestWebservice.ffti.com\">"

+"<DateInfo>"//这种方式传的是一个对象,根据请求报文格式也可以直接传一个string参数
+ "<areaCode>" + areaCode + "</areaCode>"
+ "<content>" + content + "</content>"
+ "<infoType>" + infoType + "</infoType>"
+ "<reportDate>" + reportDate + "</reportDate>"
+"</DateInfo>"

+"</pushDateInfo>"
+ "</soap:Body></soap:Envelope>");

return sb.toString();
}

private static InputStream getSoapInputStream(String areaCode,String content, String infoType, String reportDate)
throws Exception {
try {
String soap = getSoapRequest( areaCode, content, infoType, reportDate);
if (soap == null) {
return null;
}
//System.out.println(soap);
//请求的URL路径
URL url = new URL("URL地址"); 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",
"");

OutputStream os = conn.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
osw.write(soap);
osw.flush();
osw.close();

InputStream is = conn.getInputStream();
return is;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}



接下来要做的就是对获取到的输入流做一些操作,如把输入流进行包装读取其中的报文
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuffer sb = new StringBuffer();
String message = "";
while((message=br.readLine())!=null){
sb.append(message);
}
return sb.toString();


说明:如果响应报文是包含命名空间格式的,解析使用dom4j配合xpath的api进行。
示例:参考附件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值