Android network programming things to learn and Http protocol learning

1. Three ways for Android to interact with the Internet


2. First acquainted with the Http protocol

In actual development, we generally use the communication based on the Http protocol when dealing with the server, so it is very important to learn the Http protocol well. Of course, we don't need to be too particular about some details, just have a general understanding! It's all conceptual stuff!


1) What is the Http protocol?

Answer: hypertext transfer protocol (hypertext transfer protocol), an application layer protocol of TCP/IP protocol, is used to define the process of exchanging data between WEB browser and WEB server. After the client connects to the web server, if it wants to obtain a certain web resource in the web server, it needs to comply with a certain communication format. The HTTP protocol is used to define the communication format between the client and the web server.

2) Difference between Http 1.0 and Http 1.1

Answer: 1.0 protocol, after the client establishes a connection with the web server, only one web resource can be obtained! The 1.1 protocol allows the client to obtain multiple web resources on one connection after establishing a connection with the web server!

3) The underlying workflow of the Http protocol:

Answer: We must first know two nouns:

  • SYN (synchronous): The handshake signal used by TCP/IP to establish a connection

  • ACK (Acknowledgement): Confirmation character, confirming that the sent data has been accepted without error

Then we come to the concept of TCP/IP three-way handshake :

  • The client sends a syn packet (syn = j) to the server, enters the SYN_SEND state, and then waits for the server to confirm

  • The server receives the syn packet, confirms the client's syn (ack = j + 1), and at the same time sends a SYN packet (syn=k), that is, the SYN + ACK packet, the server enters the SYN_RECV state

  • The client receives the SYN + ACK packet and sends an acknowledgment packet ACK to the server (ack = k +1). After sending, the client and the server enter the ESTABLISHED state, complete the three-way handshake, and then both start to transmit data

If it's not clear, let's look at the schematic diagram of the three-way handshake:

了解了是吧,然后我们就来看看Http操作的一个流程了:

  • 用户点击浏览器上的url(超链接),Web浏览器与Web服务器建立连接

  • 建立连接后,客户端发送请求给服务器,请求的格式为: 统一资源标识符(URL)+协议版本号(一般是1.1)+MIME信息(多个消息头)+一个空行

  • 服务端收到请求后,给予相应的返回信息,返回格式为: 协议版本号 + 状态行(处理结果) + 多个信息头 + 空行 + 实体内容(比如返回的HTML)

  • 客户端接收服务端返回信息,通过浏览器显示出来,然后与服务端断开连接;当然如果中途 某步发生错误的话,错误信息会返回到客户端,并显示,比如:经典的404错误!

对于上面的流程如果还不清晰,我们可以使用HttpWatch或者firefox抓下包: PS:测试网站是小猪的学校的教务系统,输入账号密码后请求登陆,我们可以看到下述信息:

HTTP请求包含的内容:

HTTP响应包括的内容:

这就一目了然了是吧!

4)Http协议的业务流程

5)Http的几种请求方式

实际开发中我们用得较多的方式是Get和Post,但是实际开发可能还会用到其他请求方式,比如PUT, 小猪的实际项目中就用到了,下面为了方便大家,就把所有的请求方式列出来吧:

  • Get:请求获取Request-URI所标识的资源

  • POST:在Request-URI所标识的资源后附加新的数据

  • HEAD 请求获取由Request-URI所标识的资源的响应信息报头

  • PUT:请求服务器存储一个资源,并用Request-URI作为其标识

  • DELETE:请求服务器删除Request-URI所标识的资源

  • TRACE:请求服务器回送收到的请求信息,主要用于测试或诊断

  • CONNECT:保留将来使用

  • OPTIONS:请求查询服务器的性能,或者查询与资源相关的选项

6)Get和Post的对比

用得最多的两个,当然要做下对比啦!

  • GET:在请求的URL地址后以?的形式带上交给服务器的数据,多个数据之间以&进行分隔, 但数据容量通常不能超过2K,比如:http://xxx?username=…&pawd=…这种就是GET

  • POST: 这个则可以在请求的实体内容中向服务器发送数据,传输没有数量限制

  • 另外要说一点,这两个玩意都是发送数据的,只是发送机制不一样,不要相信网上说的 "GET获得服务器数据,POST向服务器发送数据"!!另外GET安全性非常低,Post安全性较高, 但是执行效率却比Post方法好,一般查询的时候我们用GET,数据增删改的时候用POST!!


7)Http状态码合集

当然,这些状态码只是要给参考,实际上决定权在服务器端(后台的)手上,一种方案是请求后, 服务器返回给我们的是状态,或者另一种,在应用不用弄多语言版本的时候最好用,直接返回 一串结果信息的Json给我们,我们直接显示就好,这样可以偷懒不少!下面列下状态码合集,参考 下就好:

  • 100~199 : 成功接受请求,客户端需提交下一次请求才能完成整个处理过程

  • 200: OK,客户端请求成功

  • 300~399:请求资源已移到新的地址(302,307,304)

  • 401:请求未授权,改状态代码需与WWW-Authenticate报头域一起使用

  • 403:Forbidden,服务器收到请求,但是拒绝提供服务

  • 404:Not Found,请求资源不存在,这个就不用说啦

  • 500:Internal Server Error,服务器发生不可预期的错误

  • 503:Server Unavailable,服务器当前不能处理客户端请求,一段时间后可能恢复正常

8)Http协议的特点

概念性的东西,知道就好,别去背,百度百科的东西,直接复制粘贴:

1. 支持客户/服务器模式

2. 简单快速:客户向服务器请求服务时,只需传送请求方法和路径。请求方法常用的有GET、 HEAD、POST。每种方法规定了客户与服务器联系的类型不同。 由于HTTP协议简单,使得HTTP服务器的程序规模小,因而通信速度很快。

3. 灵活:HTTP允许传输任意类型的数据对象。正在传输的类型由Content-Type加以标记。

4. 无连接:无连接的含义是限制每次连接只处理一个请求。服务器处理完客户的请求, 并收到客户的应答后,即断开连接。采用这种方式可以节省传输时间。

5. 无状态:HTTP协议是无状态协议。无状态是指协议对于事务处理没有记忆能力。 缺少状态意味着如果后续处理需要前面的信息,则它必须重传,这样可能导致每 次连接传送的数据量增大。另一方面,在服务器不需要先前信息时它的应答就较快。

PS:关于OSI七层协议以及TCP四层模型就不在基础系列讲解了~有兴趣可以自行了解下!

参考资料:http://www.batxue.com/html/Androidjcjc/

【架构师小秘圈,坚持每天更新优质文章⬇️】

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326092873&siteId=291194637