使用indy解析http协议的multipart上传数据

MMS彩信的数据附在Soap包后面,在正常解析soap的xml文件外,还需要对使用multipart/related编码上传的数据做解析处理,在indy中提供了解码类。

处理解码时,需要TIdMessageDecoderMIME负责实际的解码, TIdMIMEBoundary从头信息中做边界信息查找。
解码函数

  1. procedure DecodeFormData(const sHeader: String; ASourceStream:TStream; sSavePath: String);
  2. var
  3.   bEnd : Boolean;
  4.   sTmp: String;
  5.   Decoder: TIdMessageDecoderMIME;
  6.   ds: TStream;
  7. begin
  8.   bEnd := False;
  9.   Decoder := TIdMessageDecoderMIME.Create(nil);
  10.  
  11.   try
  12.     // 设置附件的边界
  13.     Decoder.MIMEBoundary := TIdMIMEBoundary.FindBoundary(sHeader);
  14.     Decoder.SourceStream := ASourceStream;
  15.     Decoder.ReadLn;
  16.    
  17.     repeat
  18.        Decoder.ReadHeader;  // 读入分块的Header信息
  19.        case Decoder.PartType of
  20.            mcptUnknown:
  21.                raise Exception.Create('Unknown form data detected');
  22.            mcptText:
  23.                begin
  24.                   sTmp := Decoder.Headers.Values['Content-Type'];  // 获取ContentType
  25.                   ds := TMemoryStream.Create;
  26.                   try
  27.                      Decoder := Decoder.ReadBody(ds,bEnd);
  28.                      // 如果取的数据仍然是由多块组成,则进行递归处理
  29.                      if AnsiSameText(Fetch(Tmp, ';'),'multipart/mixed') then
  30.                         DecodeFormData(sTmp, ds, sSavePath)
  31.                      else
  32.                         // 根据需要使用Dest的数据
  33.                   finally
  34.                      FreeAndNil(ds);
  35.                   end;//try
  36.                end; //  mcptText
  37.  
  38.            mcptAttachment:
  39.                begin
  40.                   // 处理附件的文件名
  41.                   sTmp := ExtractFileName(Decoder.FileName);
  42.                   if sTmp <> '' then
  43.                      sTmp := sSavePath + sTmp
  44.                   else
  45.                      sTmp := MakeTempFilename(sSavePath);
  46.  
  47.                   ds := TFileStream.Create(sTmp, fmCreate);
  48.                   try
  49.                       Decoder := Decoder.ReadBody(ds,bEnd);
  50.                   finally
  51.                       FreeAndNil(ds);
  52.                   end;//try
  53.              end;  // mcptAttachment
  54.         end; // case
  55.     until (Decoder = nil) or bEnd;
  56.   finally
  57.     FreeAndNil(Decoder);
  58.   end;//try
  59. end;

使用OnCreatePostStream 创建用来接受HttpPost数据的Stream

  1. CreatePostStream(ASender: TIdPeerThread; var VPostStream: TStream);
  2. begin
  3.    VPostStream := TMemoryStream.Create;
  4. end;

最后在OnCommandGet中做解析处理

  1. OnCommandGet(ASender: TIdPeerThread; ARequestInfo: TIdHttpRequestInfo; AResponseInfo: TIdHttpResponseInfo);
  2. var
  3.    sContentType : String
  4. begin
  5.    sContentType := ARequestInfo.ContentType;
  6.  
  7.    if AnsiSameText(Fetch(S, ';'), 'multipart') then begin
  8.         DecodeFormData(sContentType, ARequestInfo.PostStream, 'c:temp');
  9.    end else
  10.             // 根据需要对请求数据做处理…
  11. end;

Popularity: 5% [?]

Related

Comments

Comments are closed.