TIDHTTP 控件是用来模拟网络信息收发的,可以通过截取网络上的发送信息,然后再设置到 IdHttp 的相关数据中,以下是示例:
procedure TForm1.Button1Click(Sender: TObject)var
responseStream : TStringStream
mpfSource : TIdMultiPartFormDataStream
begin
mpfSource := TIdMultiPartFormDataStream.Create
responseStream := TStringStream.Create('')
try
IdHTTP1.Request.ContentType := mpfSource.RequestContentType
mpfSource.AddFormField('var1', '1111111111111111')
mpfSource.AddFormField('var2', '2222222222222222')
mpfSource.AddFile('userfile', 'c:\a.doc', 'file')
mpfSource.PrepareStreamForDispatch
mpfSource.Position := 0
IdHTTP1.Post('http://serwer/test.asp', mpfSource, responseStream)
finally
mpfSource.free
responseStream.free
end
end
以下是用Idhttp模拟用POST方法提交的示例:function PostMethod(URL, Data: Stringmax: Integer): String
var
PostData, RespData: TStringStream
begin
RespData := TStringStream.Create('')
PostData := TStringStream.Create(Data)
try
try
if http = nil then Exit
Http.Post(URL, PostData, RespData)
Result := RespData.DataString
http.Request.Referer := URL
except
Dec(Max)
if Max = 0 then
begin
Result := ''
Exit
end
Result := PostMethod(URL, Data, Max)
end
finally
http.Disconnect
FreeAndNil(RespData)
FreeAndNil(PostData)
end
end
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)