Search code examples
delphiwinhttp

WinHttpWriteData seems to be "flooding" server


I'm using WinHttpSendRequest/WinHttpWriteData to upload a large (54Mb) file to our server, sending it in 4Kb lumps to give user feedback. This has been working well, as far as I know, until recently. Now, when I try it, the upload goes very quickly and then the WinHttpReceiveResponse() call times-out and incomplete data is received by the server.

I'm using Win 8.1 64bit, IE11 11.0.15 (I think that WinHttp is updated with IE) but on my colleague's PC - same version of Windows, IE, the upload is much slower and the response doesn't time-out. When I try testing on various virtual machines, the problem isn't apparent. Other colleagues, however ... oh Windows!!

Just to be clear

  1. As far as I am aware, this code used to work!

  2. WinHttpOpen is called without the ASYNC flag set.

  3. The HTTP verb is POST

The code in Delphi XE2

Result:= WinHttpSendRequest(RequestHandle,PWideChar(Headers),Length(Headers),WINHTTP_NO_REQUEST_DATA,0,FormBuffer.Size,Cardinal(Self));
If Result
 then begin
      BytesToWrite:= FormBuffer.Size;
      while BytesToWrite > 0
       do begin
          If BytesToWrite > SizeOf(WriteBuffer)
           then BufFill:= SizeOf(WriteBuffer)
          else BufFill:= BytesToWrite;
          FormBuffer.ReadBytes(WriteBuffer,BufFill); // FormBuffer is my object to supply data and headers
          If WinHttpWriteData(RequestHandle,@WriteBuffer[0],BufFill,Written)
           then Dec(BytesToWrite,Written)
           else Error('WinHttpWriteData'); // Error() method calls GetLastError, assembles error message and logs it
          If Assigned(OnDataWrite)
           then OnDataWrite(Self,Written);  // Event that notifies user
       end;
      FetchResponse(RequestHandle); // Calls WinHttpReceiveResponse() and then fetches data
      Result:= True;
end
else GLE:= Error('WinHttpSendRequest');

This code was largely an adaptation of this code:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa384120(v=vs.85).aspx

The "WinHttp Sample code to do a PUT." at the bottom.


Solution

  • It's AVG ...!

    Disabling AVG gives normal performance for the upload ... now it's just a matter of finding out which part(s) are getting in the way.