Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: How to send a HTTP POST Request in Delphi XE6 using WinInet API?


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 1 - Last Post: Jan 18, 2017 12:53 PM Last Post By: Remy Lebeau (Te...
giancarlo alban...

Posts: 54
Registered: 12/15/02
How to send a HTTP POST Request in Delphi XE6 using WinInet API?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 18, 2017 8:44 AM
I found this script How to send a HTTP POST Request in Delphi using WinInet api
but in Delphi Xe6 doesn't run correctly
My XE6 code is

procedure TForm2.Button6Click(Sender: TObject);
var
  ts: TStrings;
begin
  ts := TStringList.Create;
  ts.Add('value=55465654646');
  WebPostFormData('firefox', 'www.mysite.com', '/test.php', ts);
  FreeAndNil(ts);
end;
 
 
 
 
 
 
procedure TForm2.WebPostFormData(const UserAgent: String; const Server: string; const Resource: String; const Data: TStrings);
var
  FormData: TStringList;
  PostData: UTF8String;
  I: Integer;
  BufStream: TMemoryStream;
  Resu: AnsiString;
begin
  FormData := TStringList.Create;
  try
    FormData.NameValueSeparator := '=';
    FormData.LineBreak := '&';
    for I := 0 to Data.Count-1 do begin // TODO: URL-encode the name and value...
      FormData.Add(Data.Names[I] + '=' + Data.ValueFromIndex[I]);
    end;
    PostData := UTF8String(FormData.Text);
  finally
    FormData.Free;
  end;
  BufStream := TMemoryStream.Create;
  try
    WebPostData(UserAgent, Server, Resource, PAnsiChar(PostData), Length(PostData), 'application/x-www-form-urlencoded; charset="utf-8"', BufStream);
    SetString(Resu, PAnsiChar(BufStream.Memory), BufStream.Size);
  finally
    BufStream.Free;
  end;
  ShowMessage(String(Resu));
end;
 
 
procedure TForm2.WebPostData(const UserAgent: String; const Server: string; const Resource: String; const Data: Pointer; DataSize: UInt32; const ContentType: String; Response: TStream);
var
  hInet: HINTERNET;
  hHTTP: HINTERNET;
  hReq: HINTERNET;
  Heade: String;
  Buffer: array[0..1023] of Byte;
  BytesRead: DWORD;
const accept: packed array[0..1] of PChar = (PChar('*/*'), nil);
begin
  hInet := InternetOpen(PChar(UserAgent), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  if (hInet = nil) then
    RaiseLastOSError;
  try
    hHTTP := InternetConnect(hInet, PChar(Server), INTERNET_DEFAULT_HTTP_PORT, nil, nil, INTERNET_SERVICE_HTTP, 0, 1);
    if hHTTP = nil then
      RaiseLastOSError;
    try
      hReq := HttpOpenRequest(hHTTP, PChar('POST'), PChar(Resource), nil, nil, @accept, INTERNET_FLAG_KEEP_CONNECTION, 1);
      if hReq = nil then
        RaiseLastOSError;
      try Heade := 'User-Agent: ' + UserAgent + #13#10 + 'Accept-Language: en-us,en;q=0.5'#13#10 + 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7'#13#10 + 'Content-Type: ' + ContentType + #13#10 + 'Keep-Alive: 300'#13#10;
        if not HttpAddRequestHeaders(hReq, PChar(Heade), Length(Heade), HTTP_ADDREQ_FLAG_ADD) then
          RaiseLastOSError;
        if not HTTPSendRequest(hReq, nil, 0, Data, DataSize) then
          RaiseLastOSError;
        repeat
          if not InternetReadFile(hReq, @Buffer, SizeOf(Buffer), BytesRead) then
            RaiseLastOSError;
          if (BytesRead = 0) then
            Break;
          if Response <> nil then
            Response.WriteBuffer(Buffer, BytesRead);
        until False;
      finally
        InternetCloseHandle(hReq);
      end;
    finally
      InternetCloseHandle(hHTTP);
    end;
  finally
    InternetCloseHandle(hInet);
  end;
end;


My PHP code is
        hi
	<?php
	header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
	header("Cache-Control: post-check=0, pre-check=0", false);
	header("Pragma: no-cache");
	header('Content-Type: text/html; charset=utf-8');
 
	echo 'method ' . $_SERVER['REQUEST_METHOD'];
	print_r($_GET['value']);
	print_r($_POST['value']);
	print_r($_REQUEST['value']);
	$post = file_get_contents('php://input');
	print_r($post);
	?>
	bye


But php tell me that method is GET In fact, I can not read the variable $ _POST

Edited by: giancarlo albanese on Jan 18, 2017 8:44 AM
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: How to send a HTTP POST Request in Delphi XE6 using WinInet API? [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 18, 2017 12:53 PM   in response to: giancarlo alban... in response to: giancarlo alban...
giancarlo wrote:

I found this script How to send a HTTP POST Request in Delphi using
WinInet api but in Delphi Xe6 doesn't run correctly
<snip>
But php tell me that method is GET In fact, I can not read the
variable $ _POST

I already answered your same question on StackOverflow:

http://stackoverflow.com/questions/41702890/

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02