使用原始HTTP(Putty)通过POST发送文件 [英] Sending a file via POST using raw HTTP (Putty)

查看:231
本文介绍了使用原始HTTP(Putty)通过POST发送文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用以下表单设置html页面:

If I set up a 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>

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

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

出于测试目的,我需要通过Putty会话使用原始HTTP来做同样的事情。

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

我可以这样做正常的POST(只发送文本数据):

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?

推荐答案

你必须使用 multipart content-type并将文件数据编码为十六进制/二进制文件

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

在telnet中尝试以下操作:

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

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

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

这篇关于使用原始HTTP(Putty)通过POST发送文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆