Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 472909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:08:02+00:00 2026-05-13T00:08:02+00:00

I am trying to make HTTP Requests from Delphi using the WinInet functions. So

  • 0

I am trying to make HTTP Requests from Delphi using the WinInet functions.

So far I have:

function request:string;
var
  hNet,hURL,hRequest: HINTERNET;
begin
  hNet := InternetOpen(PChar('User Agent'),INTERNET_OPEN_TYPE_PRECONFIG or INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  if Assigned(hNet) then 
  begin
  try
    hURL := InternetConnect(hNet,PChar('http://example.com'),INTERNET_DEFAULT_HTTP_PORT,nil,nil,INTERNET_SERVICE_HTTP,0,DWORD(0));
    if(hURL<>nil) then
      hRequest := HttpOpenRequest(hURL, 'POST', PChar('param=value'),'HTTP/1.0',PChar(''), nil, INTERNET_FLAG_RELOAD or INTERNET_FLAG_PRAGMA_NOCACHE,0);
    if(hRequest<>nil) then
      HttpSendRequest(hRequest, nil, 0, nil, 0);
    InternetCloseHandle(hNet);
  except
    on E : Exception do
      ShowMessage(E.ClassName+' error raised, with message : '+E.Message);
  end;
  end
end;

But this doesn’t do anything (I am sniffing network http traffic to see if it works).
I have successfully used InternetOpenURL but I also need to send POST request and that function doesn’t do that.

Could someone show me a simple example? The result I want is to get the http response page in a var as string.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T00:08:03+00:00Added an answer on May 13, 2026 at 12:08 am

    I got all the url/filename part messed up with the previous code. I’m using this from Jeff DeVore now and it’s working fine:

    function request(const AUrl, AData: AnsiString; blnSSL: Boolean = True): AnsiString;
    var
      aBuffer     : Array[0..4096] of Char;
      Header      : TStringStream;
      BufStream   : TMemoryStream;
      sMethod     : AnsiString;
      BytesRead   : Cardinal;
      pSession    : HINTERNET;
      pConnection : HINTERNET;
      pRequest    : HINTERNET;
      parsedURL   : TStringArray;
      port        : Integer;
      flags       : DWord;
    begin
      ParsedUrl := ParseUrl(AUrl);
    
      Result := '';
    
      pSession := InternetOpen(nil, INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
    
      if Assigned(pSession) then
      try
        if blnSSL then
          Port := INTERNET_DEFAULT_HTTPS_PORT
        else
          Port := INTERNET_DEFAULT_HTTP_PORT;
        pConnection := InternetConnect(pSession, PChar(ParsedUrl[0]), port, nil, nil, INTERNET_SERVICE_HTTP, 0, 0);
    
        if Assigned(pConnection) then
        try
          if (AData = '') then
            sMethod := 'GET'
          else
            sMethod := 'POST';
    
          if blnSSL then
            flags := INTERNET_FLAG_SECURE or INTERNET_FLAG_KEEP_CONNECTION
          else
            flags := INTERNET_SERVICE_HTTP;
    
          pRequest := HTTPOpenRequest(pConnection, PChar(sMethod), PChar(ParsedUrl[1]), nil, nil, nil, flags, 0);
    
          if Assigned(pRequest) then
          try
            Header := TStringStream.Create('');
            try
              with Header do
              begin
                WriteString('Host: ' + ParsedUrl[0] + sLineBreak);
                WriteString('User-Agent: Custom program 1.0'+SLineBreak);
                WriteString('Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'+SLineBreak);
                WriteString('Accept-Language: en-us,en;q=0.5' + SLineBreak);
                WriteString('Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7'+SLineBreak);
                WriteString('Keep-Alive: 300'+ SLineBreak);
                WriteString('Connection: keep-alive'+ SlineBreak+SLineBreak);
              end;
    
              HttpAddRequestHeaders(pRequest, PChar(Header.DataString), Length(Header.DataString), HTTP_ADDREQ_FLAG_ADD);
    
              if HTTPSendRequest(pRequest, nil, 0, Pointer(AData), Length(AData)) then
              begin
                BufStream := TMemoryStream.Create;
                try
                  while InternetReadFile(pRequest, @aBuffer, SizeOf(aBuffer), BytesRead) do
                  begin
                    if (BytesRead = 0) then Break;
                    BufStream.Write(aBuffer, BytesRead);
                  end;
    
                  aBuffer[0] := #0;
                  BufStream.Write(aBuffer, 1);
                  Result := PChar(BufStream.Memory);
                finally
                  BufStream.Free;
                end;
              end;
            finally
              Header.Free;
            end;
          finally
            InternetCloseHandle(pRequest);
          end;
        finally
          InternetCloseHandle(pConnection);
        end;
      finally
        InternetCloseHandle(pSession);
      end;
    end;
    

    ParseUrl is a function that splits a URL in “hostname / filename” and TStringArray is an array of strings. I still have to review the code tomorrow but it looks fine and in my sniffer I saw the post data and headers being sent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 438k
  • Answers 438k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer XCode V4 has class diagram functionality, unfortunately it's not available… May 15, 2026 at 4:35 pm
  • Editorial Team
    Editorial Team added an answer Set the doctype <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/DTD/strict.dtd">… May 15, 2026 at 4:35 pm
  • Editorial Team
    Editorial Team added an answer The problem you described (if I understand it correctly) has… May 15, 2026 at 4:35 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.