【问题标题】:Delphi Indy POST file without "fieldname" in TIdMultiPartFormDataStream在 TIdMultiPartFormDataStream 中没有“字段名”的 Delphi Indy POST 文件
【发布时间】:2019-12-10 22:04:06
【问题描述】:

使用德尔福 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у 的宽度和高度都很小。但是在浏览器中相同的文件是可以的。

【问题讨论】:

  • 您制作了一个多部分表单数据,其中文件只是其中一个字段,但在 mimetype 中您专门设置了image/jpeg。那碰撞。要么只发送一个图像,然后将其直接加载到请求正文中,要么创建多部分表单数据并让请求标头实际指定它是多部分表单数据。你需要哪一个我不知道。这取决于接收请求的进程。
  • 谢谢!决定只是:response := http.Post(url, fn);

标签: file delphi post indy


【解决方案1】:

您的服务器希望文件按原样发布在 POST 正文中。因此,您需要使用采用普通TStreamTIdHTTP.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;}

【讨论】:

    猜你喜欢
    • 2019-09-10
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    • 2019-10-02
    • 2014-08-23
    • 2011-10-08
    • 1970-01-01
    • 2013-12-09
    相关资源
    最近更新 更多