Guest User

HttpGet

a guest
Oct 8th, 2014
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.29 KB | None | 0 0
  1. uses
  2.   WinInet;
  3.  
  4. function HttpGet(const URL: AnsiString): AnsiString;
  5. const
  6.   BufferSize = 1024;
  7.   USER_AGENT = 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0';
  8. var
  9.   hSession  : HInternet;
  10.   hURL      : HInternet;
  11.   Buffer    : array[0..Pred(BufferSize)] of AnsiChar;
  12.   BufferLen : DWORD;
  13.   dwReserved: DWORD;
  14.   respCode  : DWORD;
  15. begin
  16.   Result := '';
  17.   hSession := InternetOpenA(PAnsiChar(USER_AGENT), INTERNET_OPEN_TYPE_PRECONFIG,  nil, nil, 0);
  18.   try
  19.     hURL := InternetOpenURLA(hSession, PAnsiChar(URL), nil, 0, 0, 0);
  20.     BufferLen := SizeOf(respCode);
  21.     dwReserved := 0;
  22.     if (HttpQueryInfo(hURL, HTTP_QUERY_STATUS_CODE or HTTP_QUERY_FLAG_NUMBER,
  23.                       @respCode, BufferLen, dwReserved)) then
  24.       if respCode = 200 then
  25.       try
  26.         repeat
  27.           ZeroMemory(@Buffer, SizeOf(Buffer));
  28.           InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen);
  29.           Result := Result + StrPas(Buffer);
  30.         until BufferLen = 0;
  31.       finally
  32.         InternetCloseHandle(hURL);
  33.       end else
  34.         InternetCloseHandle(hURL);
  35.   finally
  36.     InternetCloseHandle(hSession);
  37.   end;
  38. end;
  39.  
  40. procedure TForm1.Button1Click(Sender: TObject);
  41. begin
  42.   sMemo1.Text := HttpGet('http://my-site.ru/Update/Update_2.0.0.txt');
  43. end;
Advertisement
Add Comment
Please, Sign In to add comment