Вверх ↑
Этот топик читают: Гость
Ответов: 840
Рейтинг: 17
#1: 2010-11-10 19:30:09 ЛС | профиль | цитата
нужно добавить точку контралирующию useragent
function GetUrlInfo(const FileURL: string):cardinal;
var
hSession, hFile: hInternet;
dwBuffer:array[0..20] of char;
dwBufferLen, dwIndex: DWORD;
begin
Result := 0;
hSession := InternetOpen('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)',INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
if Assigned(hSession) then
begin
hFile := InternetOpenURL(hSession, PChar(FileURL),nil,0,INTERNET_FLAG_RELOAD, 0);
dwIndex := 0;
dwBufferLen := 20;
if HttpQueryInfo(hFile, HTTP_QUERY_CONTENT_LENGTH, @dwBuffer[0], dwBufferLen, dwIndex)
then Result := str2int(dwBuffer);
if Assigned(hFile) then InternetCloseHandle(hFile);
InternetCloseHandle(hsession);
end;
end;
------------ Дoбавленo в 19.23:
Дам 10$
а лудше что бы сам определял Windows NT 5.0
------------ Дoбавленo в 19.30:
вот моя попытка сделать правильно ли я делаю?

#pas

unit hiHTTP_Get;

interface

{$I share.inc}

uses Kol,Share,WinInet,Windows,Debug,hiCharset;

type
THIHTTP_Get = class(TDebug)
private
th:PThread;
FStop:boolean;
GData:TData;
FSize:cardinal;
FBusy:boolean;

fs:PStream;

procedure ShowInfo;
procedure EndDownload;
procedure OnDownload;
function Execute(Sender:PThread): Integer;
public
_prop_URL:string;
_prop_FileName:string;
_prop_Wait:boolean;
_prop_Proxy:string;
_prop_ProxyUsername:string;
_prop_ProxyPassword:string;
_prop_Length:cardinal;
[b]_prop_UserAgent:string;
[/b]
_data_FileName:THI_Event;
_data_URL:THI_Event;
_data_Position:THI_Event;
_data_Length:THI_Event;
_data_Proxy:THI_Event;
_data_ProxyUsername:THI_Event;
_data_ProxyPassword:THI_Event;
_event_onURLSize:THI_Event;
_event_onDownload:THI_Event;
_event_onStatus:THI_Event;
_event_onStop:THI_Event;

procedure _work_doDownload(var _Data:TData; Index:word);
procedure _work_doStop(var _Data:TData; Index:word);
procedure _work_GetURLSize(var _Data:TData; Index:word);
procedure _var_Busy(var _Data:TData; Index:word);
end;

implementation

function GetUrlInfo(const FileURL: string):cardinal;
var
hSession, hFile: hInternet;
dwBuffer:array[0..20] of char;
dwBufferLen, dwIndex: DWORD;
[b] _prop_UserAgent:string;[/b]
begin
Result := 0;
hSession := InternetOpen('Mozilla/4.0 (compatible; MSIE 6.0; [b]_prop_UserAgent[/b])',INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
if Assigned(hSession) then
begin
hFile := InternetOpenURL(hSession, PChar(FileURL),nil,0,INTERNET_FLAG_RELOAD, 0);
dwIndex := 0;
dwBufferLen := 20;
if HttpQueryInfo(hFile, HTTP_QUERY_CONTENT_LENGTH, @dwBuffer[0], dwBufferLen, dwIndex)
then Result := str2int(dwBuffer);
if Assigned(hFile) then InternetCloseHandle(hFile);
InternetCloseHandle(hsession);
end;
end;

function THIHTTP_Get.Execute;
var
NetHandle: HINTERNET;
UrlHandle: HINTERNET;
Buffer: array[0..1024] of char;
BytesRead, len: cardinal;
Url,Fname,Head:string;
PI:TInternetProxyInfo;
dwStatus,dwStatusSize:cardinal;
dwNil:DWORD;
dt:TData; s,s1:string;
[b]_prop_UserAgent:string;[/b]
begin
FBusy := true; dtNull(dt);
NetHandle := InternetOpen('Mozilla/4.0 (compatible; MSIE 6.0; [b]_prop_UserAgent[/b])', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
s := ReadString(dt, _data_Proxy, _prop_Proxy);
if s<>'' then begin
PI.dwAccessType := INTERNET_OPEN_TYPE_PROXY;
PI.lpszProxy := PChar(s);
PI.lpszProxyByPass := nil;
InternetSetOption(NetHandle,INTERNET_OPTION_PROXY,@PI,sizeof(PI));
end;
FStop := false;
if Assigned(NetHandle) then
begin
Url := ReadString(GData,_data_URL,_prop_URL);

BytesRead := ReadInteger(GData,_data_Position,0);
len := ReadInteger(GData,_data_Length,_prop_Length);
if BytesRead > 0 then
Head := 'Range: bytes=' + Int2Str(BytesRead) + '-' + Int2Str(BytesRead + Len)
else Head := '';

UrlHandle := InternetOpenUrl(NetHandle, PChar(Url), PChar(Head), cardinal(-1), INTERNET_FLAG_RELOAD+INTERNET_FLAG_NO_AUTH, 0);

if Assigned(UrlHandle) then
begin
dwStatusSize := sizeof(dwStatus);
dwNil := 0;
HttpQueryInfo(UrlHandle, HTTP_QUERY_FLAG_NUMBER or
HTTP_QUERY_STATUS_CODE, @dwStatus, dwStatusSize, dwNil);
if dwStatus=HTTP_STATUS_PROXY_AUTH_REQ then begin
s := ReadString(dt, _data_ProxyUsername, _prop_ProxyUsername);
s1 := ReadString(dt, _data_ProxyPassword, _prop_ProxyPassword);
if (s<>'') and (s1<>'') then begin
repeat InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead); until BytesRead=0;
HttpSendRequest(UrlHandle,PAnsiChar('Proxy-Authorization: Basic '+Base64_Code(s+':'+s1)+#13#10),DWORD(-1),nil,0);
end;
end;

FillChar(Buffer, SizeOf(Buffer), 0);
FSize := 0;
Fname := ReadString(GData,_data_FileName,_prop_FileName);
if Fname = '' then fs := NewMemoryStream
else fs := NewWriteFileStream(Fname);
repeat
FillChar(Buffer, SizeOf(Buffer), 0);
InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead);
fs.Write(Buffer,BytesRead);
inc(FSize,BytesRead);
if _prop_Wait then
ShowInfo
else th.Synchronize( ShowInfo );
//ProcessMessages;
until (BytesRead = 0)or FStop;
InternetCloseHandle(UrlHandle);
if Fname = '' then
fs.Position := 0
else
begin
fs.Free;
fs := nil;
end;
if _prop_Wait then
OnDownload
else th.Synchronize( OnDownload );
end
else ;//MessageBox(0,'Can''t open URL!','Error',MB_OK);
InternetCloseHandle(NetHandle);
end
else ;//MessageBox(0,'I can not connect to Internet!','Error',MB_OK);
Result := 0;
//th.Free;
FBusy := false;
if _prop_Wait then
EndDownload
else th.Synchronize( EndDownload );
end;

procedure THIHTTP_Get.ShowInfo;
begin
_hi_OnEvent(_event_onStatus,integer(fsize));
end;

procedure THIHTTP_Get.EndDownload;
begin
_hi_OnEvent(_event_onStop);
end;

procedure THIHTTP_Get.OnDownload;
begin
if fs = nil then
_hi_OnEvent(_event_onDownload)
else
begin
_hi_OnEvent(_event_onDownload,fs);
fs.Free;
end;
end;

procedure THIHTTP_Get._work_doDownload;
begin
GData := _Data;
//if th <> nil then
// th.Free;
if _prop_Wait then
Execute(nil)
else
begin
{$ifdef F_P}
th := NewThreadForFPC;
{$else}
th := NewThread;
{$endif}
th.OnExecute := Execute;
th.AutoFree := true;
th.Resume;
end;
end;

procedure THIHTTP_Get._work_doStop;
begin
FStop := true;
//_debug('1');
//th.WaitFor;
//_debug('2');
//th.Free;
//th := nil;
end;

procedure THIHTTP_Get._var_Busy;
begin
dtInteger(_data,byte(FBusy));
end;

procedure THIHTTP_Get._work_GetURLSize;
var
Url:string;
begin
Url := ReadString(_Data,_data_URL,_prop_URL);
_hi_OnEvent(_event_onURLSize,integer(GetUrlInfo(URL)));
end;

end.

карма: 0

0
Ответов: 397
Рейтинг: 34
#2: 2010-11-10 19:32:23 ЛС | профиль | цитата
[flood]код со смайликами прикольно смотрится [/flood]
карма: 0

0
Ответов: 1379
Рейтинг: 86
#3: 2010-11-10 19:36:04 ЛС | профиль | цитата
[flood]Black-Light, ага
GAv, засунь в тег [соdе]#pas[/соdе][/flood]
карма: 0

0
Администрация
Ответов: 15295
Рейтинг: 1519
#4: 2010-11-10 20:02:37 ЛС | профиль | цитата
только это не php, а delphi, который не умеет парсить в строках имена переменных. Поэтому правильно так:

#pas
'Mozilla/4.0 (compatible; MSIE 6.0; ' + _prop_UserAgent + ')'
карма: 27
0
Ответов: 840
Рейтинг: 17
#5: 2010-11-10 20:04:31 ЛС | профиль | цитата
Dilma
Ну а я вообще вить прравильно сделал ??? или нет ,я просто не когда сам не изменял
карма: 0

0
Ответов: 3349
Рейтинг: 233
#6: 2010-11-10 20:05:47 ЛС | профиль | цитата
Dilma писал(а):
Поэтому правильно так:

Нет неверно, _prop_UserAgent это переменная класса
карма: 1

0
Администрация
Ответов: 15295
Рейтинг: 1519
#7: 2010-11-10 20:33:28 ЛС | профиль | цитата
Ivann писал(а):
_prop_UserAgent это переменная класса

класса string?
карма: 27
0
Ответов: 3349
Рейтинг: 233
#8: 2010-11-10 21:21:40 ЛС | профиль | цитата
Dilma писал(а):
класса string?

типа. просто просто так её прочитать из функции нельзя.
Класса THIHTTP_Get.
карма: 1

0
Ответов: 893
Рейтинг: 18
#9: 2010-11-10 23:36:29 ЛС | профиль | цитата
GAv писал(а):
нужно добавить точку контралирующию useragent
А тебе обязательно элемент нужен ? Могу набросать схемку !
карма: 0
Время верстки: %cr_time% Текущее время: %time%
0
Ответов: 3349
Рейтинг: 233
#10: 2010-11-11 14:19:21 ЛС | профиль | цитата
Byuik писал(а):
А тебе обязательно элемент нужен ?

Уже сделали.
карма: 1

0
10
Сообщение
...
Прикрепленные файлы
(файлы не залиты)