1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194:
| unit retHttpGet;
interface
uses windows, wininet, sysutils;
type
PHttpGetRec = ^THttpGetRec; THttpGetRec = record AcceptTypes: String; Agent: String; BinaryData: Boolean; URL: String; UseCache: Boolean; FileName: String; FileSize: integer; UserName: String; Password: String; PostQuery: String; Referer: String; StringResult: String; end;
function HttpGetFile (var T: ThttpGetRec):boolean; function HttpGetString (var T: ThttpGetRec):boolean;
implementation
function HttpGet (var T: ThttpGetRec; ToFile: boolean): boolean; var hSession, hConnect, hRequest: hInternet; HostName, FileName: String; f: File; Buf: Pointer; dwBufLen, dwIndex: DWord; Data: String; TempStr: String; RequestMethod: PChar; InternetFlag: DWord; AcceptType: LPStr; BytesToRead, BytesReaded: DWord;
procedure ParseURL(URL: String; var HostName, FileName: String);
procedure ReplaceChar(c1, c2: Char; var St: String); var p: Integer; begin while True do begin p := Pos(c1, St); if p = 0 then Break else St[p] := c2; end; end;
var i: Integer; begin if Pos('http://', LowerCase(URL)) <> 0 then System.Delete(URL, 1, 7);
i := Pos('/', URL); HostName := Copy(URL, 1, i); FileName := Copy(URL, i, Length(URL) - i + 1);
if (Length(HostName) > 0) and (HostName[Length(HostName)] = '/') then SetLength(HostName, Length(HostName) - 1); end;
procedure CloseHandles; begin InternetCloseHandle(hRequest); InternetCloseHandle(hConnect); InternetCloseHandle(hSession); end;
begin try SetLength(Data, $400);
ParseURL(T.URL, HostName, FileName);
if T.Agent <> '' then hSession := InternetOpen(PChar(T.Agent), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0) else hSession := InternetOpen(nil, INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
hConnect := InternetConnect(hSession, PChar(HostName), INTERNET_DEFAULT_HTTP_PORT, PChar(T.UserName), PChar(T.Password), INTERNET_SERVICE_HTTP, 0, 0);
if T.PostQuery = '' then RequestMethod := 'GET' else RequestMethod := 'POST';
if T.UseCache then InternetFlag := 0 else InternetFlag := INTERNET_FLAG_RELOAD;
AcceptType := PChar('Accept: ' + T.AcceptTypes); hRequest := HttpOpenRequest(hConnect, RequestMethod, PChar(FileName), 'HTTP/1.0', PChar(T.Referer), @AcceptType, InternetFlag, 0);
if T.PostQuery = '' then HttpSendRequest(hRequest, nil, 0, nil, 0) else HttpSendRequest(hRequest, 'Content-Type: application/x-www-form-urlencoded', 47, PChar(T.PostQuery), Length(T.PostQuery));
dwIndex := 0; dwBufLen := 1024; GetMem(Buf, dwBufLen);
Result := HttpQueryInfo(hRequest, HTTP_QUERY_CONTENT_LENGTH, Buf, dwBufLen, dwIndex);
if Result or not T.BinaryData then begin if Result then T.FileSize := StrToInt(StrPas(Buf));
BytesReaded := 0;
if ToFile then begin AssignFile(f, T.FileName); Rewrite(f, 1); end else T.StringResult := '';
while True do begin
if not InternetReadFile(hRequest, @Data[1], Length(Data), BytesToRead) then Break else if BytesToRead = 0 then Break else begin if ToFile then BlockWrite(f, pointer(Data)^, BytesToRead) else begin TempStr := Data; SetLength(TempStr, BytesToRead); T.StringResult := T.StringResult + TempStr; end;
inc(BytesReaded, BytesToRead);
end; end;
if ToFile then Result := T.FileSize = Integer(BytesReaded) else begin SetLength(T.StringResult, BytesReaded); Result := BytesReaded <> 0; end;
if ToFile then CloseFile(f); end;
FreeMem(Buf);
CloseHandles; except end; end;
procedure SetupVars(var T: THttpGetRec); begin if T.AcceptTypes = '' then T.AcceptTypes := '*/*'; if T.Agent = '' then T.Agent := 'Mozilla 4.0 (retnyg rulez 1.337)'; end;
function HttpGetFile (var T: ThttpGetRec):boolean; begin SetupVars(T); if HttpGet(T, true) then result := true else result := false; end;
function HttpGetString (var T: ThttpGetRec):boolean; begin SetupVars(T); if HttpGet(T, false) then result := true else result := false; end;
end. |