`
thingkau
  • 浏览: 72548 次
  • 性别: Icon_minigender_1
  • 来自: 泉州
社区版块
存档分类
最新评论

调用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进行。
示例:参考附件
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics