如何使用WinInet api在Delphi中发送HTTP POST请求

前端之家收集整理的这篇文章主要介绍了如何使用WinInet api在Delphi中发送HTTP POST请求前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图使用WinInet函数从Delphi发出HTTP请求.

到目前为止我有:

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

但这并不做任何事情(我正在嗅探网络http流量,看看它是否有效).
我已经成功地使用了InternetOpenURL,但是我也需要发送POST请求,并且该函数不这样做.

有人能给我一个简单的例子吗?我想要的结果是将http响应页面以var作为字符串.

解决方法

我把所有的url / filename部分搞砸了以前的代码.我正在使用这个 from Jeff DeVore,它的工作正常:
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,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,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]),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,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是一个将URL分割为“hostname / filename”的功能,TStringArray是一个字符串数组.我仍然需要检查明天的代码,但它看起来不错,在我的嗅探器中,我看到发送的数据和标题被发送.

原文链接:https://www.f2er.com/delphi/102575.html

猜你在找的Delphi相关文章