Witam, pobieram kod strony html za pomoca synapse Thttpsend, jesli mam np strone ktora zajmuje zalozmy 500 KB, to jak ustawic limit aby pobrac tylko 30 kb.
Nagłówek Content-Range albo Accept-Ranges (nie pamiętam ale chyba to pierwsze) i jest warunek serwer musi obsługiwać HTTP 1.1
jest warunek - musze znalezc inne rozwiazanie.
rozlaczyc po pobraniu 30kb ?
jak sprawdzic czy jest pobrane 30 kb
Olesio napisał artykuł tam jest jak zrobić ProgressBar to równie dobrze możesz sprawdzić ile pobrałeś. Obsługa protokołu HTTP przy użyciu pakietu Synapse
A tak w ogóle co jest złego w rozwiązaniu z range?
W Synapse SockCallBack może nie zareagować przy zbyt małej ilości pobtanych danych, bo postęp może się zwiększać co kilkadzieśiąt KB, trzeba by było przeanlizować kod modułów Synapse jak to dokładnie tam się odbyw. Jedno co moge polecić, także do protokołu HTTP to moduł SimpleTCP ze strony: http://piechnat.pl/article/simpletcp.html można nim wygodniej pobrać żądaną ilośc bajtów oraz łatwiej samodzielniej operować zawartością wysyłanych danych przez Sockety. Przykład wycinku z kodu mojego Downloadera plików z ipla.tv napisanego w WinAPI. Powinieneś zrozumieć ideę. Wystarczy że zamiast Cnt = 0
dasz sobie Cnt = 30000
czy ile tam dokładnie bajtów potrzebujesz. Najlepiej poniższy kod traktuj jako przykład wklejonyu na szybko i sam sobie dopasuj do własnych potrzeb. Ewentualnie możesz zmniejszyć również rozmiar buforu, którym odczytywane są dane i zapisywane do pliku.
const
WWW_Prefix = 'www.';
Default_Http_Port = 80;
Http_Prefix = 'http://';
OperaUserAgent = 'Opera/9.80 (Windows NT 6.1; U; pl) Presto/2.7.62 Version/11.01';
//...
function GetOnlyHostName(Url : string; OnlyDomain, LeaveWWWPrefix : boolean) : string;
var
I, Cnt : integer;
AddWWWPrefix : boolean;
begin
Url := AnsiLowerCase(Url);
if Pos(Http_Prefix, Url) = 1 then
begin
Delete(Url, 1, Length(Http_Prefix));
end;
if not OnlyDomain then
begin
if not LeaveWWWPrefix then
begin
AddWWWPrefix := Pos(WWW_Prefix, Url) = 1;
if AddWwwPrefix then
begin
Delete(Url, 1, Length(WWW_Prefix));
end;
end;
I := Pos('/', Url);
if (I > 0) then
begin
I := I - 1;
end
else
begin
I := Length(Url);
end;
Result := Copy(Url, 1, I);
end
else
begin
if not LeaveWWWPrefix then
begin
AddWWWPrefix := Pos(WWW_Prefix, Url) = 1;
if AddWWWPrefix then
begin
Delete(Url, 1, Length(WWW_Prefix));
end;
end;
I := Pos('/', Url);
if (I > 0) then
begin
I := I - 1;
end
else
begin
I := Length(Url);
end;
Url := Copy(Url, 1, I);
Cnt := 0;
for I := Length(Url) downto 1 do
begin
if Url[I] = '.' then
begin
Cnt := Cnt + 1;
if Url[I - 1] = '.' then
begin
Url[I - 1] := '\';
end;
end;
if Cnt > 1 then
begin
Break;
end;
end;
Delete(Url, 1, I);
repeat
Cnt := Pos('\', Url);
if Cnt > 0 then
begin
Delete(Url, Cnt, 1);
end;
until Cnt = 0;
Result := Url;
end;
end;
procedure DownloadFileFromWeb(Url, AFileName : string; TotalFileSize : integer);
var
Clnt : TTcpClient;
BytesWritten : DWORD;
OnlyHost, S, SizeStr : string;
Buffer : array[0..65535 - 1] of Byte;
Cnt, DownloadProgress, SlashPos : integer;
begin
DownloadProgress := 0;
SendMessage(PBHandle, PBM_SETPOS, DownloadProgress, 0);
SendMessage(PBHandle, PBM_SETRANGE32, 0, TotalFileSize);
OnlyHost := GetOnlyHostName(Url, False, True);
if Pos(Http_Prefix, Url) = 1 then
begin
Delete(Url, 1, Length(Http_Prefix));
end;
SlashPos := Pos('/', Url);
if SlashPos > 0 then
begin
Url := Copy(Url, SlashPos, MaxInt);
end;
Clnt := TTcpClient.Create(OnlyHost, Default_Http_Port);
Clnt.WriteLn('GET ' + Url + ' HTTP/1.0' + CRLF + 'Host: ' + OnlyHost +
CRLF + 'User-Agent: ' + OperaUserAgent + CRLF);
repeat
Clnt.ReadLn(S);
until S = '';
if VideoFileSize = 0 then
begin
SizeStr := UnknownTotalSizeText;
end
else
begin
SizeStr := SeparateDigits(TotalFileSize);
end;
OutFile := CreateFile(PChar(AFileName), GENERIC_WRITE, FILE_SHARE_READ, nil, OPEN_ALWAYS, 0, 0);
if OutFile > 0 then
begin
repeat
Cnt := Clnt.Read(Buffer, SizeOf(Buffer));
DownloadProgress := DownloadProgress + Cnt;
WriteFile(OutFile, Buffer[0], Cnt, BytesWritten, nil);
S := FormatC('Pobrano: %s z: %s' + UnitStrArr[VideoFileSize > 0], SeparateDigits(DownloadProgress), SizeStr);
SetText(GBHandle, S);
SendMessage(PBHandle, PBM_SETPOS, DownloadProgress, 0);
until Cnt = 0;
end;
Clnt.Free;
CloseHandle(OutFile);
end;