http web 请求内容类型 null

发布于 2024-10-01 06:44:27 字数 224 浏览 0 评论 0原文

上传文件时是否有必要在http标头中提及内容类型。我尝试使用c#。我在上传 pdf 文件时将其设置为“image/png”,当我下载上传的文件时,pdf 文件是完美的。它没有被损坏。

那么在http header中指定content-type有什么作用呢? 它可以为 null 或任何其他错误值。

因为我正在制作的应用程序,用户只需提供文件,我只需上传它。

非常感谢任何帮助。提前致谢。

is it necessary to mention content-type in http header while uploading the file. i tried using c#. i had set it "image/png" while uploading a pdf file and when i downloaded the uploaded file, the pdf file was perfect. it didn't get corrupted.

so what is the role of specifying content-type in http header.
can it be null or any other wrong value.

because the application that i am making, user will just give the file and and i just need to upload it.

any help highly appreciated. thanks in advance.

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

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

发布评论

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

评论(1

挽手叙旧 2024-10-08 06:44:27

奇怪的是没有人回答这个问题。

您应该始终设置内容类型,省略它时某些软件(服务器)可能会崩溃。

您可以给它目标系统可以处理的任何值。

由于您可以轻松伪造 http 请求,因此您也可以伪造标头。
所以你的目标系统(在本例中是上传处理器)应该只接受内容类型
它能够处理的值,并且您必须验证给定的内容类型是否确实与正文中发送的数据(上传的文件本身)匹配。
您永远不能信任内容类型值,除非您以某种方式验证它。
作为一名 PHP 开发人员,我总是根据 mime 类型验证器检查任何上传的文件,以确保我得到了我所期望的结果。例如,我使用 getimagesize() 来检测它是否是图像以及是否获取其文件格式类型(PNG)。

由于 PNG 和 PDF 文件都是二进制文件格式,因此您的上传成功。
这是因为您以这种方式编码,或者目标系统退回到默认设置或自行进行一些检查。

Weird that nobody answered this question.

You should always set the content-type, some software (servers) may break when it's omitted.

You could give it any value that the target system can handle.

Since you can easily fake http request, you can also fake the headers.
So your target system (in this case an upload processor) should only accept content-type
values it's able to handle and you MUST validate if the given content-type actually matches the data that is send in the body (the uploaded file itself).
You can never trust the content-type value, until you validate it somehow.
As a PHP developer I always check any file upload against a mime-type validator to be sure I got what I expected. For example I use getimagesize() to detect whether it's an image or not and if it is to get it's file format type (PNG).

Since both PNG and PDF files are binary file formats your upload succeeded.
This is because you coded it that way or the target system falls back on default settings or does some checking for itself.

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