• 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;
  • 相关阅读:
    后台跨域(CORS)
    golang 处理TCP粘包问题
    使用axios 发送ajax 下载文件
    Golang:在Redigo的RedisPool上选择DB
    puppeteer添加代理
    mongodb 权限操作
    alpine下安装icu-dev
    golang 导出CSV文件中文乱码的问题
    shell笔记
    Convert rune to int
  • 原文地址:https://www.cnblogs.com/yangxuming/p/11429840.html
Copyright © 2020-2023  润新知