Autor Beitrag
buster
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 66
Erhaltene Danke: 7

WIN 7
Delphi 2010 Prof
BeitragVerfasst: Mo 06.06.16 11:41 
Hallo,

ich möchte mit HttpSendRequest ein POST senden, verzweifle jedoch grad am Format der zu sendenden Daten... Es gibt auch nicht allzuviele Beispiele im Netz, und wenn, werden die Parameter immer schon als Variable übergeben, man sieht nie die Struktur (oder es ist nur 1 Parameter, ich möchte aber mehrere übergeben). Content-Type ist application/x-www-form-urlencoded, also wie beim Senden aus einem (HTTP(S)-)Formular heraus.

Beispiel aus www.paste.org/19370 (so ungefähr soll es werden, allerdings mit mehr als einem Parameter):

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
function Check(const UserAgent: string; const Server: string; const Resource: string; const Data: AnsiString): string;
var
  hInet: HINTERNET;
  hHTTP: HINTERNET;
  hReq: HINTERNET;
  Buffer: array[0..1023] of AnsiChar;
  i, BufferLen: Cardinal;
const
  accept: packed array[0..1] of LPWSTR = (PChar('*/*'), nil);
  header: string = 'Content-Type: application/x-www-form-urlencoded';
begin
  Result := '';

  hInet := InternetOpen(PChar(UserAgent), INTERNET_OPEN_TYPE_PRECONFIG,
    nil, nil, 0);
  try
    hHTTP := InternetConnect(hInet, PChar(Server), INTERNET_DEFAULT_HTTP_PORT, nil, nil, INTERNET_SERVICE_HTTP, 0, 1);
    try
      hReq := HttpOpenRequest(hHTTP, PChar('POST'), PChar(Resource), nil, nil, @accept, 0, 1);
      try
        if not HttpSendRequest(hReq, PChar(header), Length(header), PChar(Data), Length(Data)) then
          raise Exception.Create('HttpOpenRequest failed. ' + SysErrorMessage(GetLastError));
        repeat
          InternetReadFile(hReq, @Buffer, SizeOf(Buffer), BufferLen);
          if BufferLen = SizeOf(Buffer) then
            Result := Result + AnsiString(Buffer)
          else if BufferLen > 0 then
            for i := 0 to BufferLen - 1 do
              Result := Result + Buffer[i];
        until BufferLen = 0;
      finally
        InternetCloseHandle(hReq);
      end;
    finally
      InternetCloseHandle(hHTTP);
    end;
  finally
    InternetCloseHandle(hInet);
  end;
end;

...

Memo1.Text := Check('UserAgent', 'www.mysite.com', 'test.php', 'test=123');


Konkret also: wie müsste Data aussehen bzw. erzeugt werden, wenn mehr als 1 Parameter übergeben werden soll? ('test=123' + 'ding=bums' + ...)

Vielleicht hat ja von euch jemand eine Idee oder schon Erfahrung damit...

LG, Basti