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.
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:
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.
Personally I prefer to use the synapse library for all of my TCP/IP work. For example, a simple HTTP post can be coded as:
The library is well written and very easy to modify to suit your specific requirements. The latest subversion release works without any problems for both Delphi 2009 and Delphi 2010. This framework is not component based, but rather is a series of classes and procedures which well in a multi-threaded environment.
The third parameter (lpszObjectName) to
HttpOpenRequest
should be the URL you wish to request. That's why the documentation describes the fifth parameter (lpszReferer) as "a pointer to a null-terminated string that specifies the URL of the document from which the URL in the request (lpszObjectName) was obtained."The posted data gets sent with
HttpSendRequest
; the lpOptional parameter is described like this:The second parameter to
InternetOpen
should be just the server name; it should not include the protocol. The protocol you specify with the sixth parameter.After you've sent the request, you can read the response with
InternetReadFile
andInternetQueryDataAvailable
.Don't just check whether the API functions return zero and then proceed on the next line. If they fail, call
GetLastError
to find out why. The code you've posted will not raise exceptions, so it's futile to catch any. (And it's foolish to "handle" them the way you're doing so anyway. Don't catch an exception that you don't already know how to fix. Let everything else go up to the caller, or the caller's caller, etc.)