使用Delphi Indy HTTP组件上载带有表单数据的文件 [英] Upload file with form data using Delphi Indy HTTP component

查看:106
本文介绍了使用Delphi Indy HTTP组件上载带有表单数据的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Indy在Delphi中复制这部分Python代码:

I want to replicate this part of Python code in Delphi using Indy:

postdata = {'data': '{"data":{"xMode":0,"overrideOS":1,"messageId":"","vmProfileList":"11","submitType":"0","url":""},"filePriorityQ":"run_now" }'}
file_up = {'amas_filename':open('/home/samples/temp/vtest32.exe','r')}
file_upload_req=requests.post(url,postdata,files=file_up,headers=headers,verify=False)

我这样尝试过:

Params.AddFormField('data', '{"data":{"xMode": '+ xMode +',"analyzeAgain":1,"overrideOS":1,' +
                            '"vmProfileList":"' + DBProfileID.Value + '","submitType":0,"url":""}}');
Params.AddFile('amas_filename', DBTestFilePath.Value, GetMIMEType(DBTestFilePath.Value));
Params.Position := 0;
HTTP1.Request.ContentType := 'application/x-www-form-urlencoded';
JSON := HTTP1.Post(URL, Params);

但是它给我一个HTTP错误 HTTP / 1.0 400 Bad Request,并且网络服务器说 Bad Request 检查输入数据和有效负载大小。我知道数据大小足够小。

but it gives me a HTTP error "HTTP/1.0 400 Bad Request" and webserver says "Bad Request. Check input data and payload size". I know the data size is small enough.

这是客户端的请求和服务器的响应:

This is the request from client and response from the server:

客户端说

POST /php/fileupload.php HTTP/1.0
Content-Type: multipart/form-data; boundary=--------031317093926335
Content-Length: 248815
VE-SDK-API: <<APIKEYWasHere>>
Host: Server_IP
Accept: application/vnd.ve.v1.0+json
Accept-Encoding: identity
User-Agent: Mozilla/3.0 - NBL
Cookie: PHPSESSID=<<Cookie_Was_Here>>

----------031317093926335
Content-Disposition: form-data; name="data"
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

{"data":{"xMode": 0,"analyzeAgain":1,"overrideOS":1,"vmProfileList":"2=
4","submitType":0,"url":""},"filePriorityQ":"run_now"}
----------031317093926335
Content-Disposition: form-data; name="amas_filename"; filename="Process.exe"
Content-Type: application/x-msdownload
Content-Transfer-Encoding: binary

MZP
<<FileDataWasHere>>

服务器服务说

HTTP/1.0 400 Bad Request
X-Content-Type-Options: nosniff
X-Content-Type-Options: nosniff
Cache-Control: no-store, no-cache, must-revalidate, private,max-age=0
Cache-Control: no-store, no-cache, must-revalidate, private,max-age=0
Pragma: no-cache
Pragma: no-cache
Expires: Sat, 26 Jul 1997 05:00:00 GMT
Content-type: text/html; charset=UTF-8
Content-Length: 89
Connection: close
Date: Mon, 13 Mar 2017 06:39:19 GMT
Server: Server FIPS

{"success":false,"errorMessage":"Bad Request. Check input data and payload size(<=200M)"}

我的代码有什么问题?

PS:我以前没有用表格数据上传文件。

PS: I have not done a file upload with form data before.

推荐答案

下次您与他人的API交互时遇到问题时,您应该指出该API的实际含义,这样人们就有机会查找它的文档以查看是否有任何缺失或错误。在这种情况下,您似乎正在使用 McAfee Advanced Threat Defense API

Next time you have trouble interacting with someone else's API, you should indicate what that API actually is so people have a chance to look up its documentation to see if anything is missing or wrong. In this case, you appear to be using the McAfee Advanced Threat Defense API.

在您显示的HTTP请求中,有几件事对我很突出:

A few things that stand out to me in your shown HTTP request:


  1. HTTP1.Request.ContentType:='application / x-www-form-urlencoded';

这是完全错误的。正确的内容类型是 multipart / form-data 。但是, TIdHTTP 在发布 TIdMultipartFormDataStream 时会为您处理此问题,因此您无需为 Request.ContentType 属性, TIdHTTP 只会覆盖它。但这并不能改变您的代码中仍然存在错误的事实。

This is just plain wrong. The correct content type is multipart/form-data instead. However, TIdHTTP handles this for you when posting a TIdMultipartFormDataStream, so you don't need to assign a value to the Request.ContentType property at all, TIdHTTP will just overwrite it. But that does not change the fact that there is a bug in your code nonetheless.

Accept-Encoding:identity

这向我表明您使用的是Indy的旧版本。您应该考虑升级到最新版本。 TIdHTTP 不再在 Accept-Encoding 请求中发送身份标头,除非 TIdHTTP.Request.AcceptEncoding 属性包含其他值,在这里不是这种情况。某些服务器在请求中明确声明 Accept-Encoding:identity 时遇到麻烦,这就是为什么默认不再发送它的原因。

This indicates to me that you are using an older version of Indy. You should consider upgrading to a more recent version. TIdHTTP no longer sends identity in the Accept-Encoding request header unless the TIdHTTP.Request.AcceptEncoding property contains other values, which is not the case here. Some servers have trouble handling Accept-Encoding: identity when explicitly stated in a request, which is why it is no longer sent by default.

内容类型:文本/纯文本

您的JSON字段应包含 content-Type application / json ,甚至 application / vnd.ve.v1 .0 + json 在此API中。除非另有说明,否则默认值为 text / plain AddFormField()为此具有一个 AContentType 参数。 Web服务器可能对此值敏感。 JSON通常也使用UTF-8编码,因此您也应该指出这一点。 AddFormField()为此具有一个 ACharset 参数。

Your JSON field should have a Content-Type of application/json, or maybe even application/vnd.ve.v1.0+json in this API. The default is text/plain when not specified otherwise. AddFormField() has an AContentType parameter for this purpose. The web server might be sensitive to that value. JSON is also typically encoded using UTF-8, so you should indicate that as well. AddFormField() has an ACharset parameter for that purpose.

Content-Transfer-Encoding:quoted-printable

您的JSON字符串使用 quoted-printable ,通常适用于MIME中的文本内容,但是并非所有Web服务器都可以处理Webform提交中的内容,因此可能不适用于非 text /...媒体类型,例如JSON。 AddFormField()返回 TIdFormDataField 对象。要禁用QP编码,可以将 TIdFormDataField.ContentTransfer 属性设置为 8bit 二进制

Your JSON string is being encoded using quoted-printable, which is normally fine for textual content in MIME, however not all web servers handle that in webform submissions, and it may not be appropriate for non-text/... media types, like JSON. AddFormField() returns a TIdFormDataField object. To disable the QP encoding, you can set the TIdFormDataField.ContentTransfer property to either 8bit or binary.

话虽这么说,请尝试以下类似操作:

That being said, try something more like this:

Params.AddFormField('data', '{"data":{"xMode": ' + xMode + ',"analyzeAgain":1,"overrideOS":1,' +
                            '"vmProfileList":"' + DBProfileID.Value + '","submitType":0,"url":""}}',
                    'utf-8',
                    'application/json'
).ContentTransfer := '8bit';

// using GetMIMEType() to specify the ContentType is redundant as
// AddFile() already does that internally for you using Indy's own
// GetMIMETypeFromFile() function...
Params.AddFile('amas_filename', DBTestFilePath.Value);

JSON := HTTP1.Post(URL, Params);

如果仍然无法解决问题,建议您直接与McAfee联系以获得进一步的帮助。

If that still does not work for you, I suggest you contact McAfee directly for further help.

这篇关于使用Delphi Indy HTTP组件上载带有表单数据的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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