Html HTTP POST 方法是否将数据作为 QueryString 发送?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5876809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do HTTP POST methods send data as a QueryString?
提问by kwichz
I'd like to know if the POST method on HTTP sends data as a QueryString, or if it use a special structure to pass the data to the server.
我想知道 HTTP 上的 POST 方法是否将数据作为 QueryString 发送,或者它是否使用特殊结构将数据传递给服务器。
In fact, when I analyze the communication with POST method from client to server (with Fiddler for example), I don't see any QueryString, but a Form Body context with the name/value pairs.
事实上,当我分析从客户端到服务器的 POST 方法的通信(例如使用 Fiddler)时,我没有看到任何 QueryString,而是一个带有名称/值对的 Form Body 上下文。
采纳答案by Greg Bray
The best way to visualize this is to use a packet analyzer like Wiresharkand follow the TCP stream. HTTP simply uses TCP to send a stream of data starting with a few lines of HTTP headers. Often this data is easy to read because it consists of HTML, CSS, or XML, but it can be any type of data that gets transfered over the internet (Executables, Images, Video, etc).
可视化这一点的最佳方法是使用像Wireshark这样的数据包分析器并跟踪 TCP 流。HTTP 只是使用 TCP 发送以几行 HTTP 标头开头的数据流。通常这些数据很容易阅读,因为它由 HTML、CSS 或 XML 组成,但它可以是通过 Internet 传输的任何类型的数据(可执行文件、图像、视频等)。
For a GET request, your computer requests a specific URL and the web server usually responds with a 200 status code and the the content of the webpage is sent directly after the HTTP response headers. This content is the same content you would see if you viewed the source of the webpage in your browser. The query string you mentioned is just part of the URL and gets included in the HTTP GET request header that your computer sends to the web server. Below is an example of an HTTP GET request to http://accel91.citrix.com:8000/OA_HTML/OALogout.jsp?menu=Y, followed by a 302 redirect response from the server. Some of the HTTP Headers are wrapped due to the size of the viewing window (these really only take one line each), and the 302 redirect includes a simple HTML webpage with a link to the redirected webpage (Most browsers will automatically redirect any 302 response to the URL listed in the Location header instead of displaying the HTML response):
对于 GET 请求,您的计算机请求一个特定的 URL,Web 服务器通常以 200 状态代码响应,并且网页的内容直接在 HTTP 响应标头之后发送。此内容与您在浏览器中查看网页源时看到的内容相同。您提到的查询字符串只是 URL 的一部分,并包含在您的计算机发送到 Web 服务器的 HTTP GET 请求标头中。以下是对http://accel91.citrix.com:8000/OA_HTML/OALogout.jsp?menu=Y的 HTTP GET 请求的示例,然后是来自服务器的 302 重定向响应。由于查看窗口的大小,一些 HTTP 标头被包装(这些实际上每个只占一行),并且 302 重定向包括一个简单的 HTML 网页,其中包含一个指向重定向网页的链接(大多数浏览器会自动重定向任何 302 响应到 Location 标头中列出的 URL,而不是显示 HTML 响应):
For a POST request, you may still have a query string, but this is uncommon and does not have anything to do with the data that you are POSTing. Instead, the data is included directly after the HTTP headers that your browser sends to the server, similar to the 200 response that the web server uses to respond to a GET request. In the case of POSTing a simple web form this data is encoded using the same URL encodingthat a query string uses, but if you are using a SOAP web service it could also be encoded using a multi-part MIME format and XML data.
对于 POST 请求,您可能仍然有一个查询字符串,但这并不常见,并且与您正在 POST 的数据没有任何关系。相反,数据直接包含在浏览器发送到服务器的 HTTP 标头之后,类似于 Web 服务器用于响应 GET 请求的 200 响应。在 POST 一个简单的 Web 表单的情况下,此数据使用与查询字符串相同的 URL 编码进行编码,但如果您使用的是 SOAP Web 服务,它也可以使用多部分 MIME 格式和 XML 数据进行编码。
For example here is what an HTTP POST to an XML based SOAP web service located at http://192.168.24.23:8090/mshlooks like in Wireshark Follow TCP Stream:
例如,这是一个 HTTP POST 到位于http://192.168.24.23:8090/msh 的基于 XML 的 SOAP Web 服务在Wireshark Follow TCP Stream 中的样子:
回答by GordonM
Post uses the message body to send the information back to the server, as opposed to Get, which uses the query string (everything after the question mark). It is possible to send both a Get query string and a Post message body in the same request, but that can get a bit confusing so is best avoided.
Post 使用消息正文将信息发送回服务器,而不是 Get,后者使用查询字符串(问号之后的所有内容)。可以在同一个请求中同时发送 Get 查询字符串和 Post 消息正文,但这可能会引起一些混乱,因此最好避免。
Generally, best practice dictates that you use Get when you want to retrieve data, and Post when you want to alter it. (These rules aren't set in stone, the specs don't forbid altering data with Get, but it's generally avoided on the grounds that you don't want people making changes just by clicking a link or typing a URL)
通常,最佳实践要求在要检索数据时使用 Get,在要更改数据时使用 Post。(这些规则不是一成不变的,规范不禁止使用 Get 更改数据,但通常会避免这样做,因为您不希望人们仅通过单击链接或输入 URL 来进行更改)
Conversely, you can use Post to retrieve data without changing it, but using Get means you can bookmark the page, or share the URL with other people, things you couldn't do if you'd used Post.
相反,您可以使用 Post 检索数据而无需更改数据,但使用 Get 意味着您可以为页面添加书签,或与其他人共享 URL,这是使用 Post 无法做到的。
As for the actual format of the data sent in the message body, that's entirely up to the sender and is specified with the Content-Type
header. If not specified, the default content-type for HTML forms is application/x-www-form-urlencoded
, which means the server will expect the post body to be a string encoded in a similar manner to a GET query string. However this can't be depended on in all cases. RFC2616 says the following on the Content-Type header:
至于在消息正文中发送的数据的实际格式,这完全取决于发送方,Content-Type
并由标头指定。如果未指定,则 HTML 表单的默认内容类型为application/x-www-form-urlencoded
,这意味着服务器将期望帖子正文是以与 GET 查询字符串类似的方式编码的字符串。然而,这不能在所有情况下都依赖。RFC2616 在 Content-Type 标头上说以下内容:
Any HTTP/1.1 message containing an entity-body SHOULD include a
Content-Type header field defining the media type of that body. If
and only if the media type is not given by a Content-Type field, the
recipient MAY attempt to guess the media type via inspection of its
content and/or the name extension(s) of the URI used to identify the
resource. If the media type remains unknown, the recipient SHOULD
treat it as type "application/octet-stream".
任何包含实体主体的 HTTP/1.1 消息都应该包含一个
Content-Type 头字段,用于定义该主体的媒体类型。如果
且仅当介质类型不是由内容类型字段中给出的
接收方可能试图通过它的检验猜测的媒体类型
的内容和/或URI的扩展名(S)用于标识
资源。如果媒体类型仍然未知,则接收者应该
将其视为类型“application/octet-stream”。
回答by Justin
A POST request caninclude a query string, however normally it doesn't - a standard HTML form with a POST action will not normally include a query string for example.
POST 请求可以包含查询字符串,但通常不包含 - 例如,具有 POST 操作的标准 HTML 表单通常不会包含查询字符串。
回答by ataddeini
GET will send the data as a querystring, but POST will not. Rather it will send it in the body of the request.
GET 会将数据作为查询字符串发送,但 POST 不会。相反,它将在请求的正文中发送它。
回答by jsgoupil
If your post try to reach the following URL
如果您的帖子尝试访问以下网址
mypage.php?id=1
mypage.php?id=1
you will have the POST data but also GET data.
您将拥有 POST 数据和 GET 数据。