Format of an HTTP get request
I'm writing an HTTP server (for the sole purpose of educating myself).
A typical GET request seems to be this:
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
The only place I can see the variables being sent is in the first line. It would be easy enough to write a regex to get the variables and their contents, but I'm wondering if there is an easier way. I ask because I always assumed that the idea of url?first_variable=first_value&second_variable=second_value
was part of the protocol, and special in some way. However, as far as I can see, this isn't the case, and I could equally just do url$first_variable-first_value?second_variable-second_value
or something.
What you are referring to is the Query portion of a URL, as defined in section 3.4 of the URL specification, and allowed by section 3.2 of the HTTP specification.
Passing parameters in the requested URL is not the only way to send parameters in an HTTP request, though. Another option is to use the application/x-www-form-urlencoded
or multipart/form-data
Content Type in a POST
request, as defined by section 17.13.4 of the HTML 4.01 specification and section 4.10.22 of the HTML5 specification, eg:
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/77056.html
上一篇: 使用Entity Framework 6 Migrations创建索引
下一篇: HTTP获取请求的格式