VB.NET/JAVA 采用SOAP 与Webservice发送/接收数据
### VB.NET与Java通过SOAP协议访问WebService的知识点 #### 一、基础知识介绍 - **SOAP (Simple Object Access Protocol)**: 是一种轻量级协议,用于交换结构化的信息。它定义了用于构造消息以及进行信息传输的规则。SOAP消息通常使用XML格式编码。 - **WebService**: 是一种跨编程语言和操作系统界限的、在网络上提供的应用程序,可以被其他应用程序通过网络调用,整个过程完全对调用程序透明。WebService技术使得不同语言编写的程序可以很容易地调用网络上的服务。 - **VB.NET与Java**: 分别代表了Microsoft .NET框架下的Visual Basic语言和跨平台的Java编程语言,它们都能支持通过SOAP协议与WebService进行交互。 #### 二、搭建环境与准备工作 1. **选择一个WebService**: 在本例中选择了`http://www.webxml.com.cn/WebServices/TranslatorWebService.asmx`作为目标WebService,提供了英语与中文之间的翻译服务。 2. **理解WebService接口**: 该接口提供的方法为`GetEnCnTwoWayTranslator`,接受一个英文单词作为输入,并返回对应的中文翻译。 #### 三、实现细节 1. **SOAP消息结构**: - SOAP消息包含一个XML文档,其结构分为头部(`<soap:Header>`)和主体(`<soap:Body>`)两部分。 - 主体部分包含了具体的调用信息,包括方法名和参数等。 - 消息示例: ```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> <getEnCnTwoWayTranslator xmlns="http://WebXml.com.cn/"> <Word>string</Word> </getEnCnTwoWayTranslator> </soap:Body> </soap:Envelope> ``` 2. **VB.NET实现**: - 首先需要创建一个StringBuilder对象用于构建SOAP消息。 - 组合SOAP消息并设置参数,例如: ```vbnet Dim soap As New StringBuilder() soap.Append("<?xml version='1.0' encoding='utf-8'?>") soap.Append("<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>") soap.Append("<soap:Body>") soap.Append("<getEnCnTwoWayTranslator xmlns='http://WebXml.com.cn/'>") soap.Append("<Word>see</Word>") soap.Append("</getEnCnTwoWayTranslator>") soap.Append("</soap:Body>") soap.Append("</soap:Envelope>") ``` - 使用`HttpWebRequest`类来发送HTTP POST请求。 3. **Java实现**: - Java中可以通过`java.net.HttpURLConnection`或者第三方库如Apache HttpClient来发送SOAP请求。 - 构建SOAP消息的方式与VB.NET类似,但需要注意字符串转义的不同,例如在Java中XML的双引号需要用`\"`来表示。 - 示例代码: ```java String soapMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" " + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + "<soap:Body>" + "<getEnCnTwoWayTranslator xmlns=\"http://WebXml.com.cn/\">" + "<Word>see</Word>" + "</getEnCnTwoWayTranslator>" + "</soap:Body>" + "</soap:Envelope>"; URL url = new URL("http://www.webxml.com.cn/WebServices/TranslatorWebService.asmx"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); connection.setRequestProperty("SOAPAction", "\"http://WebXml.com.cn/getEnCnTwoWayTranslator\""); connection.setDoOutput(true); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); out.write(soapMessage); out.flush(); out.close(); InputStream in = new BufferedInputStream(connection.getInputStream()); // 处理响应 ``` #### 四、注意事项 - **SOAPAction头部**: 对于某些WebService来说,SOAPAction是必需的,用于指定要调用的具体方法。 - **字符编码**: 在构造SOAP消息时要注意字符编码问题,确保消息体中的内容与声明的编码一致。 - **异常处理**: 在实际应用中应加入异常处理逻辑,确保程序的健壮性。 无论是使用VB.NET还是Java,通过SOAP协议与WebService交互的基本流程是相似的,主要区别在于语法糖和字符串处理等方面。理解和掌握这些基本概念对于开发者来说是非常重要的。


















- 粉丝: 0
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源


