Never too old to learn.

http协议自学(6)

Posted on By Andy Zhu

上次测试了表单的get,这次测试post

我们还是要无耻地使用以前写的代码(我都不会写代码了)

还是上次的server,不过修改了一些bug

改的跟zul_server越来越像了:

uses zul_socket,windows;
var
 server:Tserver;
 b:Vlabel;
 port:Vport;
 trdno:dword;
 i:longint;
 procedure responder;stdcall;
 var
  s,resp:ansistring;
  fin:text;
  ch:char;
  bb:Vlabel;//缓存
 begin
  bb:=b;
  repeat
   s:=server.recv(bb);
  until s<>'';
  writeln(s);
  resp:='HTTP/1.1 200 OK'+#13#10+'Content-Type: text/html'+#13#10#13#10;
  assign(fin,'test.html');
  reset(fin);
  while not eof(fin) do
   begin
    read(fin,ch);
    resp:=resp+ch;
   end;
  close(fin);
  server.send(resp,bb);
  server.close(bb);
  writeln('connection of ',bb,' has ended');
 end;
begin
 port:=8000;
 server:=Tserver.create(port);
 writeln('opened the port of:',port);
 server.init;
 writeln('init success');
 while true do
  begin
   b:=server.accept;
   writeln('connected:',b);
   createthread(nil,0,@responder,nil,0,trdno);
   for i:=1 to 10000000 do;//防过载
  end;
end.

这次的test.html改成:

<html>
    <head>
        <title>test</title>
        <meta charset="utf-8">
    </head>
    <body>
        <form method="post" action="qaqaq.html">
            <input type="input" name="qaq">
            <input type="submit">
        </form>
    </body>
</html>

然后打开浏览器,你懂的

显示的消息:

connected:440
POST /qaqaq.html HTTP/1.1
Host: localhost:8000
Connection: keep-alive
Content-Length: 7
Cache-Control: max-age=0
Origin: http://localhost:8000
Upgrade-Insecure-Requests: 1
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Referer: http://localhost:8000/
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9

qaq=123
connection of 440 has ended
connected:452
GET /favicon.ico HTTP/1.1
Host: localhost:8000
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36
Accept: image/webp,image/apng,image/*,*/*;q=0.8
Referer: http://localhost:8000/qaqaq.html
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9


connection of 452 has ended
  1. 沙雕浏览器一直在喋喋不休地让我给它favicon.ico(虽然被拒绝了无数次
  2. get改成了post
  3. 表单中的内容在正文里了

那么问题来了:如果有多个参数呢?

那么我们把test.html改成这样:

<html>
    <head>
        <title>test</title>
        <meta charset="utf-8">
    </head>
    <body>
        <form method="post" action="qaqaq.html">
            <input type="input" name="qaq">
            <input type="input" name="qaqaqaq">
            <input type="submit">
        </form>
    </body>
</html>

然后!浏览器!

connected:428
POST /qaqaq.html HTTP/1.1
Host: localhost:8000
Connection: keep-alive
Content-Length: 30
Cache-Control: max-age=0
Origin: http://localhost:8000
Upgrade-Insecure-Requests: 1
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Referer: http://localhost:8000/
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9

qaq=123123234&qaqaqaq=asdaswer
connection of 428 has ended
connected:440
GET /favicon.ico HTTP/1.1
Host: localhost:8000
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36
Accept: image/webp,image/apng,image/*,*/*;q=0.8
Referer: http://localhost:8000/qaqaq.html
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9


connection of 440 has ended

[名称]键值&[名称]键值

其实get也差不多是这样,只不过藏在了网址中

同时,如果我输入的是123&wtf=123(其中一格)

那么服务端显示的是qaq=123%26wtf%3D123&qaqaqaq=4234234(节选)

get同理

到此为止,http协议的初级语法已经讲完了

其余还有一些沙雕的异步JavaScript和xml等沙雕玩意儿