在 TIdMultiPartFormDataStream 中没有“字段名”的 Delphi Indy POST 文件

谢尔盖·日仁金

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

我的德尔福尝试:

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正文中。因此,您需要使用TIdHTTP.Post()带普通的重载版本TStream,例如 a TFileStream,而不是使用带 a 的重载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;}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章