Tuesday 15 June 2010

How to send a HTTP POST Request in Delphi using WinInet api -


After

I am trying to make HTTP requests from Delphi using WinInet functions

so far I have:

  function request: string; Var Hennet, HRLR, HRAEVEVICE: Hunter; Start hNet: = InternetOpen (PChar ('User Agent'), INTERNET_OPEN_TYPE_PRECONFIG or INTERNET_OPEN_TYPE_PRECONFIG, zero, zero, 0); If assigned (hNet), then try pushing: = InternetConnect (hNet, PChar ('http://example.com'), INTERNET_DEFAULT_HTTP_PORT, zero, zero, INTERNET_SERVICE_HTTP, 0, DWORD (0)); If the (hull <0) is hRequest: = HttpOpenRequest (toss, 'POST', PChar ('param = value'), 'http / 1.0', PChar (''), zero, INTERNET_FLAG_RELOAD or INTERNET_FLAG_PRAGMA_NOCACHE, 0); If (hRequest <0> zero) then HttpSendRequest (hRequest, zero, 0, zero, 0); InternetCloseHandle (hNet); Apart from E: Exception Show Message (E. ClassName + 'has been raised with error message:' + E. Message); End; End of end;  

But it does nothing (I'm sniffing network http traffic to see if it works). I have successfully used InternetOpenURL but I also need to send a postal request and this function does not do this.

Can anyone show me a simple example? I want to get the result as a string in the page as a string in a http.

I got the url / filename part wrong with the previous code. I am using it right now and it is working fine:

  function request (const AUrl, AData: AnsiString; blnSSL: boolean = True): AnsiString; Var aBuffer: Aare [0. 4 9 6] of variable; Header: TStringStream; Bufstream: TMemoryStream; SI Method: ANSI String; Bitrate: Cardinal; Psession: Hunter; Peaknation: Hunter; PRequest: Hunter; Parseed url: tasting array; Monkey: Integer; Flags: Didard; Start ParsedUrl: = ParseUrl (AUrl); Results: = ''; PSess: = Internet Open (zero, INTERNET_OPEN_TYPE_PRECONFIG, zero, zero, 0); If assigned (pSession) then try again that blnSSL ports: = INTERNET_DEFAULT_HTTPS_PORT other ports: = INTERNET_DEFAULT_HTTP_PORT; PConnection: = Internet connect (PSASS, PCHAR (Person URL [0]), Port, Zero, Zero, INTERNET_SERVICE_HTTP, 0, 0); If assigned (peakchanction) then try that if (adata = '') then sMethod: = 'GET' other sMethod: = 'POST'; If the blnSSL flags: = INTERNET_FLAG_SECURE or INTERNET_FLAG_KEEP_CONNECTION another flag: = INTERNET_SERVICE_HTTP; PRequest: = HTTPOpenRequest (PeakNation, PCER (SMPr), PCHAR (parsed url [1]), zero, zero, zero, flags, 0); If specified (pRequest) then try the header: = TStringStream.Create (''); Try typing with the header, start typing ('host:' + + parsed URL [0] + sinelinebrake); ListString ('User-agent: custom program 1.0' + slainbreak); ListString ('Accept: Text / html, App / XHTML + XML, Application / XML; Q = 0.9; * / *; Q = 0.8' 'SlainBreak); ListString ('Accept-language: N-US, N; Q = 0.5' 'SlainBreak); ListString ('accept-charset: ISO-885 9-1, UTF-8; Q = 0.7; *; Q = 0.7' 'SlainBreak); ListString ('Keep-alive: 300' + Sneebroken); ListString ('Connection: Keep-alive' + Sleinbrake + SlainBreak); End; HttpAddRequestHeaders (pRequest, PChar (Header.DataString), Length (Header.DataString), HTTP_ADDREQ_FLAG_ADD); If HTTPSendRequest (pRequest, zero, 0, indicator (AData), length (AData)) then start Bufstream: = TMemoryStream.Create; While starting InternetReadFile (pRequest, @aBuffer, SizeOf (aBuffer), Bitrate) (try bitrate = 0) then break; Bufstream.write (aBuffer, byte read); End; ABuffer [0]: = # 0; Buffstream Write (aBuffer, 1); Result: = Pichar (Buffetstream Memoriri); Finally Bufstream.Free; End; End; Finally the header. free; End; Finally Internet CloseHandle (PRI); End; Finally Internet CloseHandle (Peakchanction); End; Ultimately Internet CloseHandle (PS); End; End;  

Parsurl is a function that splits a URL into "hostname / filename" and has an array of strings. I just have to review the code yesterday, but it looks fine and in my snuff I sent the post data and headers.


No comments:

Post a Comment