上传图片使用idhttp indy9 delphi7

时间:2016-07-19 05:47:05

标签: indy

我想用图片将图片上传到电报服务器,但我收到读取超时错误或415不支持的媒体类型。

电报说: " 照片发送。您可以将file_id作为String传递以重新发送已在Telegram服务器上的照片,或者使用multipart / form-data上传新照片。 "

出了什么问题?

var
IDUser : String;
Stream: TStringStream;
Params: TIdMultipartFormDataStream;
msg : WideString;
LHandler: TIdSSLIOHandlerSocket;
Src , boundry : string;
begin
  API := edtAPI.Text;
  IDUser := Edit1.Text;
  Stream := TStringStream.Create('');
  try
    Params := TIdMultipartFormDataStream.Create;
    try
      //Params.AddFile('File1', 'C:\test.txt','image/png');
      Params.AddFormField('chat_id',IDUser);
      Params.AddFile('File1', 'E:\image.png','image/png');
      //Data.CopyFrom(Params,0);
      //Params.AddFormField(' test',',');
      try
        msg := '/sendPhoto';
        LHandler := TIdSSLIOHandlerSocket.Create(nil);
        //IdHTTP1.Request.ContentType := 'multipart/form-data';
        IdHTTP1.ReadTimeout := 30000;
        IdHTTP1.IOHandler:=LHandler;
        LHandler.SSLOptions.Method := sslvTLSv1;
        LHandler.SSLOptions.Mode := sslmUnassigned;
        IdHTTP1.HandleRedirects := true;
        IdHTTP1.Request.ContentType := 'multipart/form-data';

        idhttp1.Request.SetHeaders;
        memoResponse.Text :=idhttp1.Request.RawHeaders.Text;
        memoRequest.Text := BaseUrl + API + msg;
        IdHTTP1.Post(BaseUrl + API + msg, Params,Stream);
      except
        on E: Exception do
          showmessage('Error encountered during POST: ' + E.Message+ ': '
          + intToStr(IdHTTP1.Response.ResponseCode))
      end;
      ShowMessage(Stream.DataString);
    finally
      Params.Free;
    end;
  finally
    Stream.Free;
  end;
抱歉英语不好。

1 个答案:

答案 0 :(得分:0)

var
  Stream: TStringStream;
  Params: TIdMultipartFormDataStream;
  msg : WideString;
  LHandler: TIdSSLIOHandlerSocket;
  IDUser : string;
begin
  API := edtAPI.Text;
  IDUser := Edit1.Text;
  msg := '/sendPhoto';
  Stream := TStringStream.Create('');
  try
    Params := TIdMultipartFormDataStream.Create;
    try
      Params.AddFile('photo', 'E:\image.jpg','');
      Params.AddFormField('chat_id',IDUser);
      Params.AddFormField('caption',UTF8Encode('ÇÑÓÇá ÊÕæíÑ åã ÏÑÓÊ ÔÏ'));
      try
        LHandler := TIdSSLIOHandlerSocket.Create(nil);
        //IdHTTP1.ReadTimeout := 300000;
        IdHTTP1.IOHandler:=LHandler;
        LHandler.SSLOptions.Method := sslvTLSv1;
        LHandler.SSLOptions.Mode := sslmUnassigned;
        IdHTTP1.HandleRedirects := true;
        //idhttp1.Request.ContentType := Params.RequestContentType;
        memoRequest.Text := BaseUrl + API + msg;
        IdHTTP1.Post(BaseUrl + API + msg, Params, Stream);
      except
      on E: Exception do
        ShowMessage('Error encountered during POST: ' + E.Message);
      end;
      memoResponse.Text := Stream.DataString;
    finally
      Params.Free;
    end;
  finally
    Stream.Free;
  end;
end;