THttpCli发送和接受数据,代替indy10 TidHttp解决超时时间无效的问题

Posted yangxuming

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了THttpCli发送和接受数据,代替indy10 TidHttp解决超时时间无效的问题相关的知识,希望对你有一定的参考价值。

var
StartTime:Integer;

//由于Indy10碰到网络异常断掉,Indy超时时间无效的问题,需要安装第三方控件 OverByteICS 解决这个问题。
procedure
TFormPost.Button2Click(Sender: TObject); //发送overbyte 控件ICS var Data: AnsiString; AJson: TQJson; Strtemp: string; strData: string; postcmd: AnsiString; msRec: TMemoryStream; begin AJson := TQJson.Create; //TQJson 用的是QDAC开源项目组件 try AJson.Parse(Memo1.Lines.Text); AJson.ItemByPath(username).value := user123; Strtemp := -1868891045; try AJson.AddVariant(dateTime, Strtemp); except end; strData := 335725359229de9b2c5d027f9bbf7a5c; AJson.AddVariant(validation, strData); postcmd := AJson.AsString;//加载发送的json数据 HttpCli1.SendStream := TMemoryStream.Create; HttpCli1.SendStream.Write(postcmd[1], Length(postcmd)); HttpCli1.SendStream.Seek(0, 0); HttpCli1.RcvdStream := TMemoryStream.Create; HttpCli1.URL := Trim(Edit1.Text); //目标url HttpCli1.ContentTypePost := application/x-www-form-urlencoded; StartTime := GetTickCount; // HttpCli1.Post; //同步 HttpCli1.PostAsync; //异步 finally AJson.Free; end; end; procedure TFormPost.Display(str: string); //记录信息 begin Memo2.Clear; Memo2.Lines.Add(str) end; procedure TFormPost.HttpCli1RequestDone(Sender: TObject; RqType: THttpRequest; ErrCode: Word); //接受返回数据 var Data: AnsiString; // WebServ demo send AnsiString replies data1: AnsiString; Duration, BytesSec, ByteCount: integer; Temp: string; begin ByteCount := HttpCli1.SendStream.Size; HttpCli1.SendStream.Free; HttpCli1.SendStream := nil; if ErrCode <> 0 then begin Display(Post failed with error # + IntToStr(ErrCode)); HttpCli1.RcvdStream.Free; HttpCli1.RcvdStream := nil; Exit; end; if HttpCli1.StatusCode <> 200 then begin Display(Post failed with error: + IntToStr(HttpCli1.StatusCode) + + HttpCli1.ReasonPhrase); HttpCli1.RcvdStream.Free; HttpCli1.RcvdStream := nil; Exit; end; Display(Post was OK. Response was:); HttpCli1.RcvdStream.Seek(0, 0); SetLength(data1, HttpCli1.RcvdStream.Size); HttpCli1.RcvdStream.Read(data1[1], Length(data1)); // Display(string(data1)); Display(UTF8Decode(data1)); //UTF8Decode 如果是linux后台返回是utf8 格式需要转码,否则中文显示乱码 if StartTime <> 0 then //记录发送和接受的时间差 begin Duration := GetTickCount - StartTime; Temp := Received + IntToStr(ByteCount) + bytes, ; if Duration < 5000 then Temp := Temp + IntToStr(Duration) + milliseconds else Temp := Temp + IntToStr(Duration div 1000) + seconds; if ByteCount > 32767 then BytesSec := 1000 * (ByteCount div Duration) else BytesSec := (1000 * ByteCount) div Duration; Temp := Temp + ( + IntToStr(BytesSec) + bytes/sec); Display(Temp); end; HttpCli1.RcvdStream.Free; end;

 

以上是关于THttpCli发送和接受数据,代替indy10 TidHttp解决超时时间无效的问题的主要内容,如果未能解决你的问题,请参考以下文章

在 Windows XP 中使用 Indy 10 发送带有内嵌图像的电子邮件时出现“SMTP 传入数据超时”

Delphi Indy 以西里尔文发送 POST 数据

Indy10 Tcp接收数据问题

关于XE10下Indy发送字符串编码的问题

delphi2009(10,xe)下indy10发送utf8字符串

delphi indy10 无法接收中文