使用原始 HTTP (PuTTY) 通过 POST 发送文件

发布于 2024-12-06 06:31:56 字数 927 浏览 1 评论 0原文

如果我使用以下形式设置 HTML 页面:

<html>

    <body>
        <form action="upload_file.php"
              method="post"
              enctype="multipart/form-data">

            <label for="file">Filename:</label>
            <input type="file" name="file" id="file" />
            <br />
            <input type="submit" name="submit" value="Submit" />
        </form>
    </body>

</html>

我可以将文件上传到 upload_file.php,在那里我可以使用 PHP 脚本处理它。

出于测试目的,我需要通过 PuTTY 会话使用原始 HTTP 执行相同操作。

我可以这样进行普通的 POST(仅发送文本数据):

POST /test_post.php HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 11

name=myname

How can I send a file this way?

If I set up an HTML page with the following form:

<html>

    <body>
        <form action="upload_file.php"
              method="post"
              enctype="multipart/form-data">

            <label for="file">Filename:</label>
            <input type="file" name="file" id="file" />
            <br />
            <input type="submit" name="submit" value="Submit" />
        </form>
    </body>

</html>

I can upload a file to upload_file.php where I can handle it using a PHP script.

For testing purposes, I need to do the same using raw HTTP via a PuTTY session.

I can do a normal POST (just sending text data) this way:

POST /test_post.php HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 11

name=myname

How can I send a file this way?

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

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

发布评论

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

评论(2

清风不识月 2024-12-13 06:31:56

您必须使用 multipart 内容类型并将文件数据编码为十六进制/二进制

在 telnet 中尝试以下操作:

POST /the_url HTTP/1.1
User-Agent: Mozilla
Host: www.example.com
Content-Length: xxxx
Content-Type: multipart/form-data; boundary=--------------------31063722920652
------------------------------31063722920652
Content-Disposition: form-data; name="a"

value_for_a
------------------------------31063722920652
Content-Disposition: form-data; name="b"

value_for_b
------------------------------31063722920652
Content-Disposition: form-data; name="c"; filename="myfile.txt"
Content-Type: text/plain

            This is a test 
            and more

-----------------------------31063722920652
Content-Disposition: form-data; name="submit"

Submit
-----------------------------31063722920652--

请记住,字段名称及其数据之间需要额外的换行符。另外,更新 Content-Length 值。

You have to use multipart content-type and encode the file data into hex/binary

Try the following in telnet:

POST /the_url HTTP/1.1
User-Agent: Mozilla
Host: www.example.com
Content-Length: xxxx
Content-Type: multipart/form-data; boundary=--------------------31063722920652
------------------------------31063722920652
Content-Disposition: form-data; name="a"

value_for_a
------------------------------31063722920652
Content-Disposition: form-data; name="b"

value_for_b
------------------------------31063722920652
Content-Disposition: form-data; name="c"; filename="myfile.txt"
Content-Type: text/plain

            This is a test 
            and more

-----------------------------31063722920652
Content-Disposition: form-data; name="submit"

Submit
-----------------------------31063722920652--

Remember that an extra newline is necessary between field name and its data. Also, update the Content-Length value.

心安伴我暖 2024-12-13 06:31:56

使用 netcat 打开端口并保存传入请求:

nc -l -p 1090 > income-http.txt

然后修改表单以将数据发送到 netcat:

<form action="http://localhost:1090/upload_file.php" 
    method="post" enctype="multipart/form-data">

从浏览器提交表单。您可以在 venue-http.txt 文件中找到包含文件内容的完整原始请求。

保存 venue-http.txt 是一项一次性活动。稍后您可以随时发送保存的请求。请注意,您应该编辑保存的 txt 中的 Host: 标头。

Open a port with netcat and save the incoming request:

nc -l -p 1090 > income-http.txt

Then modify your form to send the data to the netcat:

<form action="http://localhost:1090/upload_file.php" 
    method="post" enctype="multipart/form-data">

Submit the form from your browser. You can find the full raw request with the contents of the file in the income-http.txt file.

Saving the income-http.txt is an one-time activity. Later you can send the saved request out any times. Please note that you should edit the Host: header in the saved txt.

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