使用不同的 HTTP 方法发送请求

send requests with different HTTP methods

如何通过浏览器发送不同的 HTTP 请求方法?我正在使用 chrome,但任何其他都可以。

例如,出于教育目的,我想尝试 TRACEOPTIONS 方法。知道我该怎么做吗?

示例:

请求留言:

OPTIONS * HTTP/1.1
Host: www.joes-hardware.com 
Accept: *

回复消息:

HTTP/1.1 200 OK
Allow: GET, POST, PUT, OPTIONS 
Context-length: 0

浏览器本身不会使用 GETPOSTHEAD 以外的动词(读取:方法)发出任何请求。通过 though, they can be made to use a wealth of other methods through the XmlHttpRequest object. However, you will be out of luck 的权力和 TRACE 动词:

If method is a case-sensitive match for CONNECT, TRACE, or TRACK, throw a "SecurityError" exception and terminate these steps.

如果您不想或不需要绑定到浏览器,有很多选择。对于初学者,Perl 的 libwww 库附带了 GETHEADPOST 命令行实用程序,它们使用起来非常简洁。

更完整的解决方案是 cURL, which is a pretty complete solution for a multitude of protocols. Its original purpose has been to simply catch a file from an URL (catch URL = cURL) which does not necessarily mean from an HTTP server. With a well-formed URL, cURL can download an attachment from an e-mail on an IMAP server. You will be most interested into the -X option of cURL's commandline interface, which allows you to specify arbitrary verbs for an HTTP request. But as mighty as it may be, there will probably be 发出 OPTIONS * HTTP/1.1 请求。

作为最后的努力,我可以全心全意地推荐 netcat, which accepts piped input and is fully able to handle encryption (which is way more comfortable than openssl's s_client). You may already know that you can emulate HTTP requests over telnet (if you type fast enough). But I believe you will find netcat with some heredoc 更舒适的方式:

$ nc -v localhost 80 <<EOD
GET / HTTP/1.1
Host: localhost
Connection: close

EOD

netcat 本身不支持 HTTP,因此您需要对请求的语法正确性负责。另一方面,这使您可以完全自由地进行试验。