 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Hasan Kosten Guest
|
Posted: Mon Dec 05, 2005 3:45 pm Post subject: TClientSocket and Cookies |
|
|
Hello,
I'm trying to get a web page with the TClientSocket component. I used
TWinSocketStream to receive data. However, The web page requires login
cookie, and I don't know how to provide that cookie to get correct page.
My current code is below:
procedure TForm1.Button2Click(Sender: TObject);
var
Buffer: array[0..1024-1] of Char;
SockStream: TWinSocketStream;
RequestString: string;
begin
ClientSocket1.Active := True;
FillChar(Buffer, SizeOf(Buffer), #0);
SockStream := TWinSocketStream.Create(ClientSocket1.Socket, 60000);
RequestString := 'GET http://www.blabla.com/get.php?session=&mode=get
HTTP/1.0'#13#10#13#10;
SockStream.Write(RequestString[1], Length(RequestString));
while (SockStream.Read(Buffer, SizeOf(Buffer)) <> 0) do
begin
Memo1.Lines.Add(Buffer);
FillChar(Buffer, SizeOf(Buffer), #0);
if not ClientSocket1.Active then
Exit;
end;
SockStream.Free;
if ClientSocket1.Active then
ClientSocket1.Active := False;
end;
I wish to provide the login information before accessing this page. Any help
will be greatly appreciated.
Regards,
Hasan
|
|
| Back to top |
|
 |
theo Guest
|
Posted: Mon Dec 05, 2005 4:02 pm Post subject: Re: TClientSocket and Cookies |
|
|
Any help
| Quote: | will be greatly appreciated.
|
Why don't you take a more suitable class like THTTPSend with HTTP and
Cookie support?
http://www.ararat.cz/synapse/
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon Dec 05, 2005 8:40 pm Post subject: Re: TClientSocket and Cookies |
|
|
"Hasan Kosten" <hkosten (AT) yahoo (DOT) com> wrote
| Quote: | I'm trying to get a web page with the TClientSocket component.
|
HTTP is a very complex protocol to implement from scratch properly. I
*highly* suggest that you use a real HTTP component or library instead of
TClientSocket. Indy (http://www.indyproject.org) and ICS
(http://www.overbyte.be) both have an HTTP client component available. Or
use the WinInet or WinHTTP API from Microsoft. Or one of any number of
other third-party components/libraries that are available.
| Quote: | The web page requires login cookie, and I don't know how
to provide that cookie to get correct page.
|
Have you studied the HTTP protocol specs yet? My guess would be no. You
need to add 'Cookie' and/or 'Cookie2' headers to your request data. You
also need to parse the 'Set-Cookie' and/or 'Set-Cookie2' header in the
response data to know what the cookie value actually is.
| Quote: | My current code is below:
|
Your code is not correct enough, even without the cookie support. You are
not sending the proper request in the first place, and you are not taking
into account cases where the reading/writing fail mid-way. Try this code
instead:
procedure TForm1.Button2Click(Sender: TObject);
var
Buffer: array[0..1023] of Char;
SockStream: TWinSocketStream;
RequestString: String;
ResponseData: TMemoryStream;
function DoRead(Stream: TStream): Boolean;
var
Ptr: PByte;
Read: Integer;
S: String;
begin
Result := False;
repeat
Read := SockStream.Read(Buffer, SizeOf(Buffer));
if Read > 0 then
begin
Stream.WriteBuffer(Buffer, Read);
Result := True;
end;
until Read < 1;
end;
function DoWrite(Data: Pointer; Len: Integer): Boolean;
var
Ptr: PByte;
Written: Integer;
begin
Result := False;
Ptr := PByte(Pointer);
while Len > 0 do
begin
Written := SockStream.Write(Ptr, Len);
if Written < 1 then Exit;
Inc(Ptr, Written);
Dec(Len, Written);
end;
Result := True;
end;
begin
ResponseData := TMemoryStream.Create;
try
ClientSocket1.Host := 'www.blabla.com';
ClientSocket1.Port := 80;
ClientSocket1.Active := True;
try
SockStream := TWinSocketStream.Create(ClientSocket1.Socket,
60000);
try
RequestString := 'GET /get.php?session=&mode=get
HTTP/1.0'#13#10;
if Cookie <> '' then
RequestString := RequestString + 'Cookie: ' + Cookie
+ #13#10;
RequestString := RequestString + #13#10;
if not DoWrite(Pointer(RequestString),
Length(RequestString)) then Exit;
if not DoRead(ResponseData) then Exit;
// parse ResponseData as needed...
ResponseData.Position := 0;
Memo1.Lines.LoadFromStream(ResponseData);
finally
SockStream.Free;
end;
finally
ClientSocket1.Active := False;
end;
finally
ResponseData.Free;
end;
end;
Gambit
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|