次のコードは、アプリケーションをサーバーにログインします。ログインが成功すると、そのサーバーは認証トークンを返します。そのトークンを使用して、サーバーに情報を照会する必要があります。
egressMsg := pchar('email='+LabeledEdit1.text+'&&password='+MaskEdit1.Text+#0);
egressMsg64 := pchar(Encode64(egressMsg));
Reserved := 0;
// open connection
hInternetConn := InternetOpen('MyApp', INTERNET_OPEN_TYPE_PRECONFIG, NIL, NIL, 0);
if hInternetConn = NIL then
begin
ShowMessage('Error opening internet connection');
exit;
end;
// connect
hHttpSession := InternetConnect(hInternetConn, 'myserver.com',
INTERNET_DEFAULT_HTTP_PORT, '', '', INTERNET_SERVICE_HTTP, 0, 0);
if hHttpSession = NIL then
begin
ShowMessage('Error connecting');
exit;
end;
// send request
hHttpRequest := HttpOpenRequest(hHttpSession, 'POST',
'/myapp/login', NIL, NIL, NIL, 0, 0);
if hHttpRequest = NIL then
begin
ShowMessage('Error opening request');
exit;
end;
label2.caption := egressMsg64 + ' '+inttostr(length(egressMsg64));
res := HttpSendRequest(hHttpRequest, Nil,
DWORD(-1), egressMsg64, length(egressMsg64));
if not res then
begin
ShowMessage('Error sending request ' + inttostr(GetLastError));
exit;
end;
BufferSize := Length(infoBuffer);
res := HttpQueryInfo(hHttpRequest, HTTP_QUERY_STATUS_CODE, @infoBuffer, BufferSize, Reserved);
if not res then
begin
ShowMessage('Error querying request ' + inttostr(GetLastError));
exit;
end;
reply := infoBuffer;
Memo1.Lines.Add(reply);
if reply <> '200' then
begin
//error here
end;
// how to I get the token here!!!!
InternetCloseHandle(hHttpRequest);
InternetCloseHandle(hHttpSession);
InternetCloseHandle(hInternetConn);
そのトークンを取得するにはどうすればよいですか?Cookieのクエリを試したり、InternetGetCookie()などを試したりしました。コードをいただければ幸いです
ありがとう
ジェス
編集
InternetReadFileを使用すると、そのトークンを取得できることがわかりました。ただし、そのトークンはバイトの配列として出力されます。後でサーバーに送信するために使用するのは難しいです...バイトの配列をpcharまたは文字列に変換する方法を知っている人はいますか?