java-webservice-SOAP调用方式

public static String sendWebService(String content, String method){
		// 这里要打一个LOG
		int timeout = 60000;

		StringBuffer sb = new StringBuffer();
		sb.append("");
		sb.append("");
		sb.append("");
		sb.append("");
		sb.append(").append(method).append(">");
		sb.append(").append(content).append("]]>");
		sb.append(").append(method).append(">");
		sb.append("");
		sb.append("");

		final String resultStrTag = method + "Result";
		
		// HttpURLConnection 发送SOAP请求
		try {
			String urlStr = StaticProperties.HisUrl;
			LogUtil.info("hisUrl:" + urlStr);
			URL url = new URL(urlStr);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();

			conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
			conn.setRequestProperty("SOAPAction", "http://tempuri.org/" + method);//Soap
			conn.setRequestMethod("POST");
			conn.setUseCaches(false);
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setConnectTimeout(timeout);
			conn.setReadTimeout(timeout);

			DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
			dos.write(sb.toString().getBytes(StandardCharsets.UTF_8));
			dos.flush();

			BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
			String line;
			StringBuilder strBuf = new StringBuilder();
			while ((line = reader.readLine()) != null) {
				strBuf.append(line);
			}
			dos.close();
			reader.close();
			String partStr = strBuf.toString();
			partStr = partStr.replaceAll(">", ">").replaceAll("<", "<");
			String resultStr = partStr.substring(partStr.indexOf("<" + resultStrTag + ">") + resultStrTag.length() + 2, partStr.indexOf(" + resultStrTag + ">"));
			// 这里要打一个LOG
			return resultStr;
		} catch (Exception e) {
			e.printStackTrace();
			// 这里要打一个LOG
		}
		return null;
	}

你可能感兴趣的:(java-webservice-SOAP调用方式)