Send a HTTP POST Request in Delphi using WinInet api

In my recent work i have to send a http get request to the itunes app store.In the past i send the request by idhttp compents,but i notice that when get something vi openssl i get the wrong data.there are some noise data,just as below:

  
  
  

but the correct data should be like this:

  
  
  

with no “st11p08it-finance004/” and “0.0.0.0.0.2.1.1.1.47.1.7.0.1.0.1.0.0.5.0.15.1.0.3.0.1.1.3.0.7.7.0.5.1.0.3.0.5.0.0.1.3.0.0.3.0” should be “2.0.0.0.0.2.1.1.1.47.1.7.0.1.0.1.0.0.5.0.15.1.0.3.0.1.1.3.0.7.7.0.5.1.0.3.0.5.0.0.1.3.0.0.3.0
via the wininet api i get the correct result but i don’t know why there are so many diffrents .if anyone know just leave a message here .thx.smile

Function sslInet(Const AServer, 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;
begin

 Result := '';

 pSession := InternetOpen(nil, INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);

 if Assigned(pSession) then
   try

     case blnSSL of
       True  :  pConnection := InternetConnect(pSession, PChar(AServer), INTERNET_DEFAULT_HTTPS_PORT, nil, nil, INTERNET_SERVICE_HTTP, 0, 0);
       False :  pConnection := InternetConnect(pSession, PChar(AServer), INTERNET_DEFAULT_HTTP_PORT, nil, nil, INTERNET_SERVICE_HTTP, 0, 0);
     end;

  if Assigned(pConnection) then
    try

      if (AData = '') then
        sMethod := 'GET'
      else
        sMethod := 'POST';

     case blnSSL of
       True  : pRequest := HTTPOpenRequest(pConnection, PChar(sMethod), PChar(AURL), nil, nil, nil, INTERNET_FLAG_SECURE  or INTERNET_FLAG_KEEP_CONNECTION, 0);
       False : pRequest := HTTPOpenRequest(pConnection, PChar(sMethod), PChar(AURL), nil, nil, nil, INTERNET_SERVICE_HTTP, 0);
     end;

  if Assigned(pRequest) then
    try

      Header := TStringStream.Create('');
      with Header do
        begin
          WriteString('Host: ' + AServer + sLineBreak);
          WriteString('User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10'+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
       FreeAndNil(BufStream);
     end;
    end;

    finally
      InternetCloseHandle(pRequest);
      FreeAndNil(Header);
    end;

    finally
      InternetCloseHandle(pConnection);
    end;

    finally
      InternetCloseHandle(pSession);
    end;
end;

link:http://pastebin.com/f1ea3a752
And a update Version From here:

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;
☆版权☆

* 网站名称:obaby@mars
* 网址:https://lang.ma/
* 个性:https://oba.by/
* 本文标题: 《Send a HTTP POST Request in Delphi using WinInet api》
* 本文链接:https://danteng.me/2012/03/3902
* 短链接:https://oba.by/?p=3902
* 转载文章请标明文章来源,原文标题以及原文链接。请遵从 《署名-非商业性使用-相同方式共享 2.5 中国大陆 (CC BY-NC-SA 2.5 CN) 》许可协议。


You may also like

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注