HTTP POST 方法是否以查询字符串形式发送数据?

发布于 2024-11-05 02:07:40 字数 168 浏览 0 评论 0原文

我想知道 HTTP 上的 POST 方法是否以 QueryString 形式发送数据,或者是否使用特殊结构将数据传递到服务器。

事实上,当我分析从客户端到服务器的 POST 方法的通信(例如使用 Fiddler)时,我没有看到任何 QueryString,而是带有名称/值对的 Form Body 上下文。

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

等你爱我 2024-11-12 02:07:40

可视化这一点的最佳方法是使用数据包分析器,例如 Wireshark遵循 TCP 流。 HTTP 只是使用 TCP 发送以几行 HTTP 标头开始的数据流。通常,这些数据很容易阅读,因为它由 HTML、CSS 或 XML 组成,但它可以是通过互联网传输的任何类型的数据(可执行文件、图像、视频等)。

对于 GET 请求,您的计算机请求特定的 URL,Web 服务器通常会响应 200 状态代码,并且网页内容直接在 HTTP 响应标头之后发送。此内容与您在浏览器中查看网页源代码时看到的内容相同。您提到的查询字符串只是 URL 的一部分,并包含在您的计算机发送到 Web 服务器的 HTTP GET 请求标头中。以下是对 http://accel91.citrix 的 HTTP GET 请求示例.com:8000/OA_HTML/OALogout.jsp?menu=Y,后跟来自服务器的 302 重定向响应。由于查看窗口的大小,一些 HTTP 标头会被包裹(这些标头实际上每个只占一行),并且 302 重定向包括一个简单的 HTML 网页,其中包含指向重定向网页的链接(大多数浏览器会自动重定向任何 302 响应)到 Location 标头中列出的 URL,而不是显示 HTML 响应):

HTTP GET with 302 重定向

对于 POST 请求,您可能仍然有一个查询字符串,但这并不常见,并且与您要发布的数据没有任何关系。相反,数据直接包含在浏览器发送到服务器的 HTTP 标头之后,类似于 Web 服务器用于响应 GET 请求的 200 响应。在发布简单的 Web 表单时,此数据使用相同的 URL 编码进行编码 查询字符串使用的,但如果您使用 SOAP Web 服务,也可以使用 多部分 MIME 格式和 XML 数据

例如,以下是对位于 http://192.168.24.23:8090/ 的基于 XML 的 SOAP Web 服务的 HTTP POST 的内容msh 看起来像 Wireshark 跟随 TCP 流

HTTP POST TCP Stream

The best way to visualize this is to use a packet analyzer like Wireshark and 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).

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):

HTTP GET with 302 redirect

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 encoding that 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.

For example here is what an HTTP POST to an XML based SOAP web service located at http://192.168.24.23:8090/msh looks like in Wireshark Follow TCP Stream:

HTTP POST TCP Stream

温柔嚣张 2024-11-12 02:07:40

Post 使用消息正文将信息发送回服务器,这与 Get 不同,后者使用查询字符串(问号后面的所有内容)。可以在同一请求中同时发送 Get 查询字符串和 Post 消息正文,但这可能会有点混乱,因此最好避免。

一般来说,最佳实践要求您在想要检索数据时使用 Get,在想要更改数据时使用 Post。 (这些规则并不是一成不变的,规范并不禁止使用 Get 更改数据,但通常会避免这样做,因为您不希望人们仅通过单击链接或键入 URL 来进行更改)

相反,您可以使用 Post 检索数据而不更改数据,但使用 Get 意味着您可以为页面添加书签,或与其他人共享 URL,而如果您使用 Post,则无法执行这些操作。

至于消息正文中发送的数据的实际格式,完全取决于发送者,并通过 Content-Type 标头指定。如果未指定,HTML 表单的默认内容类型为 application/x-www-form-urlencoded,这意味着服务器将期望帖子正文是一个字符串,其编码方式与获取查询字符串。然而,这不能在所有情况下都依赖。 RFC2616 在 Content-Type 标头中说了以下内容:

任何包含实体主体的 HTTP/1.1 消息都应该包含
Content-Type 标头字段定义该正文的媒体类型。如果
并且仅当 Content-Type 字段未给出媒体类型时,
接收者可以尝试通过检查媒体类型来猜测媒体类型
用于标识
的 URI 的内容和/或名称扩展
资源。如果媒体类型仍然未知,接收者应该
将其视为类型“application/octet-stream”。

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.

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)

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.

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:

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".

风苍溪 2024-11-12 02:07:40

POST 请求可以包含查询字符串,但通常情况下不会包含 - 例如,具有 POST 操作的标准 HTML 表单通常不会包含查询字符串。

A POST request can include 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.

毁虫ゝ 2024-11-12 02:07:40

GET 会将数据作为查询字符串发送,但 POST 不会。相反,它将在请求正文中发送。

GET will send the data as a querystring, but POST will not. Rather it will send it in the body of the request.

隱形的亼 2024-11-12 02:07:40

如果您的帖子尝试访问以下 URL

mypage.php?id=1,

您将获得 POST 数据,同时也获得 GET 数据。

If your post try to reach the following URL

mypage.php?id=1

you will have the POST data but also GET data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文