加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

HTTPConnection调用WebService接口

发布时间:2020-12-16 22:51:53 所属栏目:安全 来源:网络整理
导读:C#发布WebService接口 使用HTTPConnection直接Post参数进行调用 URL url = new URL(surl);HttpURLConnection connection = (HttpURLConnection) url.openConnection();SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");connection.setDoOu

C#发布WebService接口

使用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....
			}
		}
	}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读