HTTPConnection调用WebService接口

C#发布WebService接口

HTTPConnection调用WebService接口_第1张图片

使用HTTPConnection直接Post参数进行调用

URL url = new URL(surl);
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
			connection.setDoOutput(true);
			connection.setRequestMethod("POST");
			connection.setDoInput(true);
			connection.setRequestProperty("Content-Type",
					"text/xml; charset=utf-8");
			connection.setRequestProperty("SOAPAction",
					"http://tempuri.org/AddMyToReadList");

			OutputStreamWriter out = new OutputStreamWriter(
					connection.getOutputStream(), "utf-8");
			StringBuilder sb = new StringBuilder();
sb.append("<MyToReadListInfo>");
					sb.append("<SSIC_ID>" + info.getSsic_id() + "</SSIC_ID>");
					sb.append("<URL><![CDATA[" + retURL + "]]></URL>");
					sb.append("<OutID><![CDATA[" + strNow + "]]></OutID>");
					sb.append("<Infor_Id><![CDATA[" + strNow.substring(8)
							+ "]]></Infor_Id>");
					sb.append("<Stru_ID></Stru_ID>");
					sb.append("<Opertor>000000003</Opertor>");
					sb.append("<Status>0</Status>");
					sb.append("<Title><![CDATA[" + strtitle + "]]></Title>");
					sb.append("</MyToReadListInfo>");
StringBuilder header_sb = new StringBuilder();
				StringBuilder footer_sb = new StringBuilder();
				header_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
				header_sb
						.append("<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/\">");
				header_sb.append("<soap:Body>");
				header_sb
						.append("<AddMyToReadList xmlns=\"http://tempuri.org/\">");
				header_sb.append("<AppKeyValue>");
				header_sb.append("<AppID><![CDATA[" + getProperty("appid")
						+ "]]></AppID>");
				header_sb.append("<AppValue><![CDATA["
						+ getProperty("appvalue") + "]]></AppValue>");
				header_sb.append("</AppKeyValue>");
				header_sb.append("<MyToReadListInfoList>");

				footer_sb.append("</MyToReadListInfoList>");
				footer_sb.append("</AddMyToReadList>");
				footer_sb.append("</soap:Body>");
				footer_sb.append("</soap:Envelope>");

				out.write(header_sb.toString() + sb.toString() + footer_sb); // 直接post的进行调用!

//解析返回的XML字串
out.flush();
				out.close();
				connection.connect();

				InputStream urlStream = connection.getInputStream();
				BufferedReader bufferedReader = new BufferedReader(
						new InputStreamReader(urlStream));
				String ss = null;
				String total = "";
				while ((ss = bufferedReader.readLine()) != null) {
					total += ss;
				}

				logger.info("接口返回结果");
				logger.info(total);
				bufferedReader.close();

				Document document = DocumentHelper.parseText(total);
				Node node = document
						.selectSingleNode("/soap:Envelope/soap:Body/*[name()='AddMyToReadListResponse']/*[name()='AddMyToReadListResult']/*[name()='RetCode']");
				String retCode = node.getText();

				logger.info("retCode = " + retCode);


可以使用XPath来直接读取解析返回的字串,具体路径如果无法准确获知,可通过下面的方法获取节点路径。

public static void treeWalk(Document document) {
		treeWalk(document.getRootElement());
	}

	public static void treeWalk(Element element) {
		for (int i = 0, size = element.nodeCount(); i < size; i++) {
			Node node = element.node(i);
			System.out.println("Name = " + element.getName() + " Path = "
					+ element.getPath());
			if (node instanceof Element) {
				treeWalk((Element) node);
			} else {
				// do something....
			}
		}
	}


你可能感兴趣的:(java,webservice,xpath)