关于SOAP的开发以及发布!
SOAP开发心得
最近,在研究移动APP开发,其中尝试过Android系统通过JDBC直接联系MySQL,但是总是报错,不知道网上面的人,是怎么连上去的。。后来,仔细想了一下,Android通过JDBC直接连接MySQL,是有危险的。首先,假如别人反编译你的APP,那么你的MySQL Host地址,账号,密码全部都暴露了。综上所述,我就思索了一下,是否采用Web API,可惜的是我服务器.net版本太低。于是,我就想通过做基于SOAP的WebService。
下面是百度词条关于SOAP的内容:
SOAP、WSDL(WebServicesDescriptionLanguage)、UDDI(UniversalDescriptionDiscovery andIntegration)之一, soap用来描述传递信息的格式, WSDL 用来描述如何访问具体的接口, uddi用来管理,分发,查询webService 。具体实现可以搜索 Web Services简单实例 ; SOAP 可以和现存的许多因特网协议和格式结合使用,包括超文本传输协议(HTTP),简单邮件传输协议(SMTP),多用途网际邮件扩充协议(MIME)。它还支持从消息系统到远程过程调用(RPC)等大量的应用程序。SOAP使用基于XML的数据结构和超文本传输协议(HTTP)的组合定义了一个标准的方法来使用Internet上各种不同操作环境中的分布式对象。
我开发环境是VS 2010,具体步骤,先新建ASP.NET Web Service
在Services1.cs文件中添加如下代码:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Xml.Serialization;
namespace WebService1
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://www.cnblogs.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
[WebMethod(Description = "Hello World!")]
public string HelloWorld()
{
return "Hello World!";
}
[WebMethod(Description = "Hello [YOUR NAME HERE]")]
public string HelloTo(string name)
{
return "Hello " + name + "!";
}
}
[WebService(Namespace = "http://www.cnblogs.com/")] ,这个命名空间,有关你Request XML 和 Response XML 构建和解析!
现在,我们就构建了两个WebService 接口! HelloWorld(),HelloTo()
注意到,第一个不需要传递参数,第二个string类型的参数。
如果,大家需要将这个WebServcie 发布到服务器端,那么还要添加访问协议
大家在Web.config中添加
<webServices xdt:Transform="Replace"> <protocols> <add name="HttpSoap" /> <add name="HttpGet"/> <add name="HttpPost"/> </protocols> </webServices>
然后,大家通过VS 发布功能,使用FTP账户,直接发布到服务器端就行啦!
其中,关于HelloTo的请求XML是:
POST /Service1.asmx HTTP/1.1 Host: www.cnblogs.com/ Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "http://www.cnblogs.com/HelloTo" <?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> <HelloTo xmlns="http://www.cnblogs.com/"> <name>你好!博客园</name> </HelloTo> </soap:Body> </soap:Envelope>
1:header中的content-Length要根据具体内容长度来替换这个length!
2:要根据实际Host地址和命名空间替换http://www.cnblogs.com/!
下面给出服务器响应XML
HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length <?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> <HelloToResponse xmlns="http://www.cnblogs.com/"> <HelloToResult>你好!博客园</HelloToResult> </HelloToResponse> </soap:Body> </soap:Envelope>
我目前也在学习阶段,请大家批评指正!
2014-12-19

浙公网安备 33010602011771号