当前位置 : 主页 > 网页制作 > Nodejs >

WebService理解和代码解析

来源:互联网 收集:自由互联 发布时间:2021-06-24
/** WebServcie 概念 多个系统数据交换: 跨平台语言的相互通信;如:java 的客户端 和dotnet的服务器端的接口调用:得到接口和方法: 基于标准的协议,可编程语言; 服务器开发 api;特点

/** WebServcie 概念  

多个系统数据交换: 跨平台语言的相互通信;
	如:java 的客户端 和dotnet的服务器端的接口调用:
		得到接口和方法	: 基于标准的协议,可编程语言;  服务器开发 api;
	
	特点:
		自包含:只要客户端支持http和xml就可以;
		自描述:只需要知道请求响应的类型;  其他的不需要考虑
		跨平台:不同语言可以相互通信。
		通过网络:发布查找使用;
		松耦合;
	
	术语:
		XML:扩展型可标记语言;
		SOAP:简单对象访问协议;用来描述传递信息的格式;  xml方法的调用规范;支持http等协议
		WSDL:web描述性语言,xml文档,自动生成;;  说明一组soap消息,和如何交互消息,是软件自动生成的xml文档;
		UDDI:英文为 "Universal Description, Discovery and Integration"通用描述、发现与集成服务;  引导系统查找响应服务的机制;
			 根据描述文档,来引导系统查找响应服务的机制。  提供自出服务,其他厂商根据服务;
			 
			 
		【公司的举例:】		
			soap 协议、合同,约束双方   
			wsdl  说明书,提供什么服务    
			uddi 工商注册,方便别人查询

		

*/

/* wsdl  
	wsdl 文档包含
	
		详细学习:http://www.w3school.com.cn/wsdl/wsdl_documents.asp
		
		某种类型系统:
		
		WSDL 文档是利用这些主要的元素来描述某个 web service 	
		
		元素 		定义
		<portType>	 web service 执行的操作
		<message> 	web service 使用的消息
		<types> 	web service 使用的数据类型
		<binding> 	web service 使用的通信协议

		
		
*/

/* soap  
	简单对象的访问协议;
		基于xml协议,以xml形式提供了一个简单的数据交换的协议;
		访问web服务的简单对象的访问协议
		通过http实现信息传递
		1)soap封装:描述消息中的内容;
		2)soap编码规则:定义序列化的机制、应用程序类型实例;
		3)soap rpc : 远程应答的协议  远程过程调用
		4)soap绑定: 节点间交互 soap的封装
	
	格式如:
POST /webservices/EnglishChinese.asmx HTTP/1.1
Host: fy.webxml.com.cn
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://WebXml.com.cn/Translator"

<?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>
    <Translator xmlns="http://WebXml.com.cn/">
      <wordKey>string</wordKey>
    </Translator>
  </soap:Body>
</soap:Envelope>	

	规则:
		需要使用xml编码
		<soap:Body>是必须的
		Type传输数据类型
		soap:Envelope 必须使用命名空间

	优点:1)可扩展;2)简单的;3)和厂商无关;4)编程语言无关;

*/

//***********【soap1.1 解析的过程】**************
/**
	soap 1.1  xmlpull  conn  URL   
	
	1.代码和注解
	2.xml文件
	
*/

/**  2016年9月  整理webService的解析  (单词以外的链接有问题)
	 * WebService POST 请求 Soap1.1协议、 xml解析
	 * 
	 * @see 单词翻译查询网址:
	 * @see 1.网址:http://fy.webxml.com.cn/webservices/EnglishChinese.asmx
	 * @see 2.选方法: Translator 查询单词
	 * @see 3.方法中看soap协议,1.1或者1.2。对应post 中,xml部分复制,在asset中建立xml文件保存。
	 * @see 4.修改xml中 ,参数部分,如:<wordKey>string</wordKey> 。其中string修改为
	 *      #word,方便后续代码替换
	 * @see 步骤
	 * @see 1、xml参数转 流,替换单词参数, 并创建HttpURLConnection链接
	 * @see 2、conn设置链接的 协议,参考 soap文件 conn.setRequestProperty("Content-Type",
	 *      "text/xml; charset=utf-8"); Content-Type Content-Length SOAPAction
	 * @see 3、os 输出流 写 刷新
	 * @see 4、输入流 并 xml解析
	 * 
	 * 
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public void WspostSoap11() throws IOException, XmlPullParserException {

		/*
		 * ( 1.inputstream 2.url 3.outputstaam 4.inputsteeem 5.xmlpull
		 */

		String path = "word.xml";

		InputStream is = getAssets().open(path);// MainActivity.class.getClassLoader().getResourceAsStream(path);
		InputStreamReader reader = new InputStreamReader(is, "utf-8");
		BufferedReader bfReader = new BufferedReader(reader);
		String str = "";
		StringBuilder sb = new StringBuilder();
		while ((str = bfReader.readLine()) != null) {
			// str += str.replace("#word", "girl");
			sb.append(str.replace("#word", "girl"));
		}
		byte[] data = sb.toString().getBytes();

		String url = "http://fy.webxml.com.cn/webservices/EnglishChinese.asmx";
		URL urls = new URL(url);
		HttpURLConnection conn = (HttpURLConnection) urls.openConnection();
		conn.setRequestMethod("POST");
		conn.setConnectTimeout(5 * 1000);
		conn.setReadTimeout(3 * 1000);
		conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
		conn.setRequestProperty("Content-Length", data.length + "");
		conn.setRequestProperty("SOAPAction", "http://WebXml.com.cn/Translator");
		conn.setDoOutput(true);

		OutputStream os = conn.getOutputStream();
		os.write(data, 0, data.length);

		os.flush();

		InputStream isRusult = conn.getInputStream();

		XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
		XmlPullParser parser = factory.newPullParser();

		parser.setInput(isRusult, "utf-8");

		String result = "";
		while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
			if (parser.getEventType() == XmlPullParser.START_TAG && parser.getName().equals("Translation")) {
				result = parser.nextText();
			}
			if ("Trans".equals(parser.getName())) {
				Log.e(TAG, " Trans  " + parser.toString());
			}
			parser.next();

		}

		Log.e(TAG, " result  " + result);

	}
//=====================word.xml
<?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>
    <Translator xmlns="http://WebXml.com.cn/">
      <wordKey>#word</wordKey>
    </Translator>
  </soap:Body>
</soap:Envelope>

//=====================参考的 SOAP 1.1
POST /webservices/EnglishChinese.asmx HTTP/1.1
Host: fy.webxml.com.cn
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://WebXml.com.cn/Translator"

<?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>
    <Translator xmlns="http://WebXml.com.cn/">
      <wordKey>string</wordKey>
    </Translator>
  </soap:Body>
</soap:Envelope>

//***********【soap1.2 解析的过程】**************
/**
	 * WebService POST 请求 Soap1.2协议、 xml解析
	 * 
	 * @see 单词翻译查询网址:
	 * @see 1.网址:http://fy.webxml.com.cn/webservices/EnglishChinese.asmx
	 * @see 2.选方法: Translator 查询单词
	 * @see 3.方法中看soap协议,1.1或者1.2。对应post 中,xml部分复制,在asset中建立xml文件保存。
	 * @see 4.修改xml中 ,参数部分,如:<wordKey>string</wordKey> 。其中string修改为
	 *      #word,方便后续代码替换
	 * @see 步骤
	 * @see 1、xml参数转 流,替换单词参数, 并创建HttpURLConnection链接
	 * @see 2、conn设置链接的 协议,参考 soap文件 conn.setRequestProperty("Content-Type",
	 *      "text/xml; charset=utf-8"); Content-Type Content-Length SOAPAction
	 * @see 3、os 输出流 写 刷新
	 * @see 4、输入流 并 xml解析
	 * 
	 * @category soap 1.1 与  1.2 不同地方两个:
	 * 			1、setRequestProperty   1.2 设置协议中没有 SOAPAction
	 * 			2、1.2 协议的 xml文件不太一样。
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public void WspostSoap12() throws IOException, XmlPullParserException {

		/*
		 * ( 1.inputstream 2.url 3.outputstaam 4.inputsteeem 5.xmlpull
		 */

		String path = "word12.xml";

		InputStream is = getAssets().open(path);// MainActivity.class.getClassLoader().getResourceAsStream(path);
		InputStreamReader reader = new InputStreamReader(is, "utf-8");
		BufferedReader bfReader = new BufferedReader(reader);
		String str = "";
		StringBuilder sb = new StringBuilder();
		while ((str = bfReader.readLine()) != null) {
			sb.append(str.replace("#word", "boy"));
		}
		byte[] data = sb.toString().getBytes();

		String url = "http://fy.webxml.com.cn/webservices/EnglishChinese.asmx";
		URL urls = new URL(url);
		HttpURLConnection conn = (HttpURLConnection) urls.openConnection();
		conn.setRequestMethod("POST");
		conn.setConnectTimeout(5 * 1000);
		conn.setReadTimeout(3 * 1000);
		conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
		conn.setRequestProperty("Content-Length", data.length + "");
//		conn.setRequestProperty("SOAPAction", "http://WebXml.com.cn/Translator");
		conn.setDoOutput(true);

		OutputStream os = conn.getOutputStream();
		os.write(data, 0, data.length);

		os.flush();

		InputStream isRusult = conn.getInputStream();

		XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
		XmlPullParser parser = factory.newPullParser();

		parser.setInput(isRusult, "utf-8");

		String result = "";
		while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
			if (parser.getEventType() == XmlPullParser.START_TAG && parser.getName().equals("Translation")) {
				result = parser.nextText();
			}
			if ("Trans".equals(parser.getName())) {
				Log.e(TAG, " Trans  " + parser.getName());
			}
			parser.next();
		}
		Log.e(TAG, " result  " + result);
	}

//**********【使用jar包的解析方法soap】**********************
	/**
	 * soap 解析
	 * 
	 * @see 1.导包 ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar
	 * @see 2.定义网址、方法、命名空间
	 * @see 3.HttpTransportSE soap请求对象
	 * @see 4.SoapSerializationEnvelope soap序列化后的封装对象: soap信封
	 * @see 5.SoapObject 参数对象 并设置 addProperty
	 * @see 6.htse 调用 call方法 client.call(namespace + method, envelope);
	 * @see 7.信封中  查看响应 envelope.getResponse,返回bodyIn信息:字符串替换查找
	 * 
	 */
	public void soapDictionaryWord() {
		try {
			// 要访问的网址
			String wsdl = "http://fy.webxml.com.cn/webservices/EnglishChinese.asmx";
			// webservice 的功能名称
			String method = "Translator";
			// 响应数据的namespace
			String namespace = "http://WebXml.com.cn/";

			// soap客户端 http传输协议对象 send envelope
			HttpTransportSE client = new HttpTransportSE(wsdl);
			// soap序列化后的封装对象: soap信封
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			// 参数对象 目标
			SoapObject params = new SoapObject(namespace, method);

			// 添加要翻译的单词 添加属性
			params.addProperty("wordKey", "Everybody");
			// 设置参数对象 放入信封
			envelope.bodyOut = params;
			// 设置支持dotNet 服务器
			envelope.dotNet = true;

			// 访问webService 1.设置所需的SOAPAction头字段 2.包含soap调用信息的信封
			client.call(namespace + method, envelope);
			// 有响应结果 ; api 从包装对象中拉取一个对象,并返回,不为空表示得到了对象
			if (envelope.getResponse() != null) {
				// 返回响应对象 接收信封中的内容
				SoapObject response = (SoapObject) envelope.bodyIn;
				// 处理响应数据 : 返回指定位置的特定属性 0;得到body中的内容
				SoapObject obj = (SoapObject) response.getProperty(0);

				System.out.println("------" + obj);
				// 获取响应的字符串
				String result = obj.toString();
				// 找截断的起始位置
				int firstIndex = result.indexOf("Translation=");
				// 找截断的终止位置
				int endIndex = result.indexOf(";", firstIndex);
				// 翻译后的字符串
				String transWord = result.substring(firstIndex + "Translation=".length(), endIndex);

				System.out.println("------" + transWord);
				System.out.println("------" + getResult(result, "Trans=别", "。"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//测试解析字符串的方法
	public String getResult(String resu, String starStr, String endStr) {
		int first = resu.indexOf(starStr);
		int end = resu.indexOf("。", first);
		return resu.substring(first + starStr.length() - 1, end);
	}



网友评论