zoukankan      html  css  js  c++  java
  • idhttp+tidmultpartfromdatastream

    参阅:https://www.cnblogs.com/ClaireWu/p/12487028.html

    function xxx.UploadFile(const sAccessToken, sFile, sFileType: string):string;

    var
      IdHttp: TIdHTTP;
      MutPartForm: TIdMultiPartFormDataStream;
      Ms: TStringStream;
      sTmp: string;
      LStream: TIdReadFileExclusiveStream;
      SSLIO: TIdSSLIOHandlerSocketOpenSSL;
    begin
      Result := '';
      try
        Ms := TStringStream.Create('', TEncoding.UTF8);
        IdHttp := TIdHttp.Create(nil);
        IdHttp.ReadTimeout := 30000;
        MutPartForm := TIdMultiPartFormDataStream.Create;
        LStream := TIdReadFileExclusiveStream.Create(sFile);
        SSLIO := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
        try
          IdHttp.AllowCookies    := True;
          IdHttp.HandleRedirects := True//允许重定向
          SSLIO.SSLOptions.Method:=sslvTLSv1;
          SSLIO.SSLOptions.Mode := sslmClient;
          IdHttp.IOHandler := SSLIO;
          // Http1.1
          IdHttp.HTTPOptions := IdHttp.HTTPOptions + [hoKeepOrigProtocol];
          IdHttp.ProtocolVersion := pv1_1;
          MutPartForm.AddObject('media''application/octet-stream', LStream, ExtractFileName(sFile));
          sTmp := Format('http://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s',[sAccessToken, sFileType]);
          IdHttp.Post(sTmp, MutPartForm, Ms);
          Result := Ms.DataString;
        finally
          LStream.Free;
          Ms.Free;
          IdHttp.Free;
          MutPartForm.Free;
          SSLIO.Free;
        end;
      except
        on E: Exception do Result := E.Message;
      end;
    end;
  • 相关阅读:
    洛咕 P4131 [WC2005]友好的生物
    P3354 [IOI2005]Riv 河流
    洛咕 P3645 [APIO2015]雅加达的摩天楼
    洛咕 P4528 [CTSC2008]图腾
    CSDN不登录阅读全文(最新更新
    #6472. 「ICPC World Finals 2017」难以置信的任务 Mission Improbable
    #6435. 「PKUSC2018」星际穿越
    #2009. 「SCOI2015」小凸玩密室
    #2007. 「SCOI2015」国旗计划
    PKUWC2018题解
  • 原文地址:https://www.cnblogs.com/suhq/p/13175147.html
Copyright © 2011-2022 走看看