Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

delphi - Random Indy ResponseCode = -1 / EIdSocketError Socket Error # 0 exceptions

I have a delphi code that basically upload files to remote secure server using Indy 10.4.704:

 IdHTTP                 := TIdHTTP.Create(nil);
 try
    TheCompressor       := TIdCompressorZLib.Create(nil);
    TheSSL              := TIdSSLIOHandlerSocketOpenSSL.Create(nil);

    with IdHTTP do
    begin
         HTTPOptions     := [hoForceEncodeParams];
         AllowCookies    := True;
         HandleRedirects := True;
         ProtocolVersion := pv1_1;

         IOHandler       := TheSSL;
         Compressor      := TheCompressor;
    end;    // with

    // Get upload resume offset
    try
       IdHttp.Head('https://www.domain.com/my-file.bin');
       if (IdHttp.Response.ResponseCode <> 404) And (IdHttp.Response.ContentLength >= 0) then
          StartPos   := IdHttp.Response.ContentLength
       else
           StartPos  := 0;
    except
          StartPos   := 0;
    end;    // try/except

    // Upload File
    TheFile          := TFileStream.Create(FileName, fmOpenRead OR fmShareDenyWrite);
    RangeStream      := TIdHTTPRangeStream.Create(TheFile, StartPos, -1, True);
    try
       if (RangeStream.ResponseCode = 206) then
          IdHTTP.Post(https://www.domain.com/upload.php', RangeStream);
    finally
           RangeStream.Free;
    end;    // try/finally
 finally
        FreeAndNil(IdHTTP);
 end;    // try/finally

The problem is that sometimes the code fails with Indy throwing a EIdSocketError Socket Error # 0 exception (idHTTP.ResponseCode is -1)

Given my crappy internet connection, I launched an EC2 windows instance and tested my code on it (the windows instance is running on the cloud, so I assume connection is not a problem), yet I got the same issue!

The error seems to be random, sometimes upload works, sometimes not. I debugged with TidLogFile, all I could find is something like this:

Stat Connected.
Sent 4/26/2012 4:18:42: POST /app/upload.php...
Sent 4/26/2012 4:18:42: <uploaded_file_data_here>
Stat Disconnected.

Anyone knows what's causing this/how to fix this?

EDIT

I traced the exception back to TIdSSLIOHandlerSocketOpenSSL. I googled a lot, it seems that it's not an SSL error.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Please upgrade to the latest Indy 10 version, which is 10.5.8 r4743. SSL-related issues with Error Code 0 were fixed over a year ago.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...