http://www.webxml.com.cn/ 推荐网站
WebService底层原理
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket=new ServerSocket(8888);
Socket socket= serverSocket.accept();
BufferedReader reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String message=reader.readLine();
System.err.println("Client saied "+ message);
reader.close();
socket.close();
serverSocket.close();
}
}
public class Client {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket socket=new Socket("10.212.169.93", 8888);
String message="你好啊 服务器";
BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
writer.write(message);
writer.close();
socket.close();
}
}
简单的网络应用:
先理解什么是服务:
大家在写应用程序查询数据库时,并没有考虑过为什么可以将查询结果返回给上层的应用程序,甚至认为,这就是数据库应该做的,其实不然,这是数据库通过TCP/IP协议与另一个应用程序进行交流的结果,而上层是什么样的应用程序,是用什么语言,数据库本身并不知道,它只知道接收到了一份协议,这就是SQL92查询标准协议。
目前的云计算、云查杀都是一种服务,现在比较流行的说法是SOA(面向服务的框架)。
既然数据库可以依据某些标准对外部其他应用程序提供服务、而且不关心对方使用什么语言,那我们为什么就不能实现跨平台、跨语言的服务呢?
只要我们用Java写的代码,可以被任意的语言所调用,我们就实现了跨平台,跨语言的服务!
复杂的网络应用:
解决几个问题?
它能做什么?
为什么要学习WebService?
学习WebService要达到什么目的?
1:从WebService的工作模式上理解的话,它跟普通的Web程序(比如ASP、JSP等)并没有本质的区别,都是基于HTTP传输协议的程序。
2:WebService所使用的数据均是基于XML格式的。目前标准的WebService在数据格式上主要采用SOAP协议。SOAP协议实际上就是一种基于XML编码规范的文本协议。
在开始之前必须要让先了解几个名词:
XML,用于传输格式化的数据,是Web服务的基础。
namespace-命名空间。
xmlns=“http://itcast.cn” 使用默认命名空间。
xmlns:itcast=“http://itcast.cn”使用指定名称的命名空间。
名词2:WSDL – WebService Description Language – Web服务描述语言。
通过XML形式说明服务在什么地方-地址。
通过XML形式说明服务提供什么样的方法 – 如何调用。
名词3:SOAP-Simple Object Access Protocol(简单对象访问协议)
SOAP作为一个基于XML语言的协议用于有网上传输数据。
SOAP = 在HTTP的基础上+XML数据。
SOAP是基于HTTP的。
SOAP的组成如下:
Envelope – 必须的部分。以XML的根元素出现。
Headers – 可选的。
Body – 必须的。在body部分,包含要执行的服务器的方法。和发送到服务器的数据。
以下简单了解一个WSDL,SOAP的格式
POST/WebServices/MobileCodeWS.asmxHTTP/1.1
Host:webservice.webxml.com.cn
Content-Type:text/xml; charset=utf-8
Content-Length:length
SOAPAction:"http://WebXml.com.cn/getMobileCodeInfo"
<?xmlversion="1.0" encoding="utf-8"?>
<soap:Envelopexmlns: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>
<getMobileCodeInfoxmlns="http://WebXml.com.cn/">
<mobileCode>string</mobileCode>
<userID>string</userID>
</getMobileCodeInfo>
</soap:Body>
</soap:Envelope>
HTTP/1.1200 OK
Content-Type:text/xml; charset=utf-8
Content-Length:length
<?xmlversion="1.0" encoding="utf-8"?>
<soap:Envelopexmlns: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>
<getMobileCodeInfoResponsexmlns="http://WebXml.com.cn/">
<getMobileCodeInfoResult>string</getMobileCodeInfoResult>
</getMobileCodeInfoResponse>
</soap:Body>
</soap:Envelope>