以下代码将我的应用程序登录到服务器。如果登录成功,该服务器将返回一个身份验证令牌。我需要使用该令牌来查询服务器以获取信息。
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 或字符串?