HTTP获取请求的格式

我正在写一个HTTP服务器(仅用于教育我自己)。

一个典型的GET请求似乎是这样的:

GET /?a=1&b=2 HTTP/1.1 
Host: localhost    
User-Agent: my browser details
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8    
Accept-Language: en-gb,en;q=0.5    
Accept-Encoding: gzip, deflate    
Connection: keep-alive

我可以看到发送变量的唯一位置在第一行。 写一个正则表达式来获取变量和它们的内容是很容易的,但是我想知道是否有更简单的方法。 我问,因为我总是认为url?first_variable=first_value&second_variable=second_value的想法url?first_variable=first_value&second_variable=second_value是协议的一部分,并且在某些方面是特殊的。 但是,据我所见,情况并非如此,我可以同样只做url$first_variable-first_value?second_variable-second_value或其他。


你所指的是URL的查询部分,正如在URL规范的3.4节中定义的那样,并且在HTTP规范的第3.2节中被允许。

但是,在请求的URL中传递参数并不是在HTTP请求中发送参数的唯一方式。 另一种选择是在POST请求中使用application/x-www-form-urlencodedmultipart/form-data Content Type,如HTML 4.01规范的第17.13.4节和HTML5规范的第4.10.22节所定义的,例如:

POST / HTTP/1.1 
Host: localhost    
User-Agent: my browser details
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8    
Accept-Language: en-gb,en;q=0.5    
Accept-Encoding: gzip, deflate    
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 7

a=1&b=2

POST / HTTP/1.1 
Host: localhost    
User-Agent: my browser details
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8    
Accept-Language: en-gb,en;q=0.5    
Accept-Encoding: gzip, deflate    
Connection: keep-alive
Content-Type: multipart/form-data; boundary=myboundary

--myboundary
Content-Disposition: form-data; name="a"

1
--myboundary
Content-Disposition: form-data; name="b"

2
--myboundary--
链接地址: http://www.djcxy.com/p/77055.html

上一篇: Format of an HTTP get request

下一篇: RxJava (or Rx.NET) equivalent of ReactiveCocoa's RACObserve