Delphi Indy POST 文件在 TIdMultiPartFormDataStream 中没有 "fieldname"
Delphi Indy POST file without "fieldname" in TIdMultiPartFormDataStream
使用Delphi 10.1。
需要将图像发送到此站点:www.flagma.ru
在嗅探器请求中:
URL:https://flagma.ru/messageuploadphoto.php?qqfile=111.jpg
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3
Content-Type: application/octet-stream
Referer: https://flagma.ru/my/1538481/podat-obyavlenie.html
X-File-Name:111.jpg
X-Mime-Type: image/jpeg
X-Requested-With: XMLHttpRequest
我的 Delphi 尝试:
http.Request.ContentType := 'application/octet-stream';
http.Request.Accept := '*/*';
http.Request.AcceptLanguage := 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7';
http.Request.AcceptEncoding := 'gzip, deflate';
http.Request.Referer := 'https://flagma.ru/my/1538481/podat-obyavlenie.html';
fnShort := ExtractFileName(fn); // fn - full image file
url := 'https://flagma.ru/messageuploadphoto.php?qqfile=' + fnShort;
http.Request.CustomHeaders.AddValue('X-Mime-Type', 'image/jpeg');
http.Request.CustomHeaders.AddValue('X-File-Name', fnShort);
http.Request.CustomHeaders.AddValue('X-Requested-With', 'XMLHttpRequest');
(* it is wrong way
a := TStringStream.Create(tempa);
formData := TIdMultiPartFormDataStream.Create;
formData.Clear;
formData.AddFile('qqfile', fn, GetMIMETypeFromFile(fn));
try
http.Post(url, formData, a);
except
on E: EIdException do
begin
response := '';
end;
end;
formData.Free;
response := utf8Decode(a.DataString);
*)
解决方案就是:response := http.Post(url, fn);
出现错误:Imagу 的宽度和高度较小。但是在浏览器中相同的文件是可以的。
您的服务器希望文件按原样发布在 POST
正文中。因此,您需要使用采用普通 TStream
的 TIdHTTP.Post()
的重载版本,例如 TFileStream
,而不是使用采用 TIdMultipartFormDataStream
的重载:
http.Request.ContentType := 'application/octet-stream';
http.Request.Accept := '*/*';
http.Request.AcceptLanguage := 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7';
//http.Request.AcceptEncoding := 'gzip, deflate';
http.Request.Referer := 'https://flagma.ru/my/1538481/podat-obyavlenie.html';
fnShort := ExtractFileName(fn); // fn - full image file
url := 'https://flagma.ru/messageuploadphoto.php?qqfile=' + fnShort;
http.Request.CustomHeaders.Values['X-Mime-Type'] := GetMIMETypeFromFile(fn);
http.Request.CustomHeaders.Values['X-File-Name'] := fnShort;
http.Request.CustomHeaders.Values['X-Requested-With'] := 'XMLHttpRequest';
fs := TFileStream.Create(fn, fmOpenRead or fmShareDenyWrite);
try
try
response := http.Post(url, fs);
except
response := '';
end;
finally
fs.Free;
end;
{ alternatively: you can pass the full file path to Post()...
try
response := http.Post(url, fn);
except
response := '';
end;}
使用Delphi 10.1。 需要将图像发送到此站点:www.flagma.ru
在嗅探器请求中:
URL:https://flagma.ru/messageuploadphoto.php?qqfile=111.jpg
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3
Content-Type: application/octet-stream
Referer: https://flagma.ru/my/1538481/podat-obyavlenie.html
X-File-Name:111.jpg
X-Mime-Type: image/jpeg
X-Requested-With: XMLHttpRequest
我的 Delphi 尝试:
http.Request.ContentType := 'application/octet-stream';
http.Request.Accept := '*/*';
http.Request.AcceptLanguage := 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7';
http.Request.AcceptEncoding := 'gzip, deflate';
http.Request.Referer := 'https://flagma.ru/my/1538481/podat-obyavlenie.html';
fnShort := ExtractFileName(fn); // fn - full image file
url := 'https://flagma.ru/messageuploadphoto.php?qqfile=' + fnShort;
http.Request.CustomHeaders.AddValue('X-Mime-Type', 'image/jpeg');
http.Request.CustomHeaders.AddValue('X-File-Name', fnShort);
http.Request.CustomHeaders.AddValue('X-Requested-With', 'XMLHttpRequest');
(* it is wrong way
a := TStringStream.Create(tempa);
formData := TIdMultiPartFormDataStream.Create;
formData.Clear;
formData.AddFile('qqfile', fn, GetMIMETypeFromFile(fn));
try
http.Post(url, formData, a);
except
on E: EIdException do
begin
response := '';
end;
end;
formData.Free;
response := utf8Decode(a.DataString);
*)
解决方案就是:response := http.Post(url, fn);
出现错误:Imagу 的宽度和高度较小。但是在浏览器中相同的文件是可以的。
您的服务器希望文件按原样发布在 POST
正文中。因此,您需要使用采用普通 TStream
的 TIdHTTP.Post()
的重载版本,例如 TFileStream
,而不是使用采用 TIdMultipartFormDataStream
的重载:
http.Request.ContentType := 'application/octet-stream';
http.Request.Accept := '*/*';
http.Request.AcceptLanguage := 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7';
//http.Request.AcceptEncoding := 'gzip, deflate';
http.Request.Referer := 'https://flagma.ru/my/1538481/podat-obyavlenie.html';
fnShort := ExtractFileName(fn); // fn - full image file
url := 'https://flagma.ru/messageuploadphoto.php?qqfile=' + fnShort;
http.Request.CustomHeaders.Values['X-Mime-Type'] := GetMIMETypeFromFile(fn);
http.Request.CustomHeaders.Values['X-File-Name'] := fnShort;
http.Request.CustomHeaders.Values['X-Requested-With'] := 'XMLHttpRequest';
fs := TFileStream.Create(fn, fmOpenRead or fmShareDenyWrite);
try
try
response := http.Post(url, fs);
except
response := '';
end;
finally
fs.Free;
end;
{ alternatively: you can pass the full file path to Post()...
try
response := http.Post(url, fn);
except
response := '';
end;}