29.05.2010, 11:36
|
|
Let Me Show You
|
|
Регистрация: 30.04.2010
Адрес: Северодвинск
Сообщения: 5,426
Версия Delphi: 7, XE5
Репутация: 59586
|
|
переделал предыдущий пример (только до чтения заголовка, остальное по аналогии):
PHP код:
unit Unit1;
interface
uses
WinSock,
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
WSAData: TWSAData;
ErrorCode: Integer;
FSocket: TSocket;
HostEnt: PHostEnt;
InAddr: TInAddr;
FAddr: TSockAddrIn;
Blocking: Longint;
arg: Integer;
request: String;
request_data: String;
savetickcount: Cardinal;
c: Char;
response: String;
username: String;
password: String;
begin
username:='**********';
password:='**********';
ErrorCode:=WSAStartup($0101, WSAData);
if ErrorCode<>0 then raise Exception.Create('WSAStartup');
try
FSocket:=socket(PF_INET, SOCK_STREAM, 0);
if FSocket=INVALID_SOCKET then raise Exception.Create('socket');
HostEnt:=gethostbyname('forum29.ru');
FillChar(InAddr, SizeOf(InAddr), 0);
if HostEnt<>nil then
begin
InAddr.S_un_b.s_b1:=HostEnt^.h_addr^[0];
InAddr.S_un_b.s_b2:=HostEnt^.h_addr^[1];
InAddr.S_un_b.s_b3:=HostEnt^.h_addr^[2];
InAddr.S_un_b.s_b4:=HostEnt^.h_addr^[3];
end else raise Exception.Create('gethostbyname');
FAddr.sin_family:=PF_INET;
FAddr.sin_addr:=InAddr;
FAddr.sin_port:=htons(80);
Blocking:=0;
ioctlsocket(FSocket, FIONBIO, Blocking);
ErrorCode:=connect(FSocket, FAddr, SizeOf(FAddr));
if ErrorCode<>0 then raise Exception.Create('connect');
try
// post данные
request_data:='vb_login_username='+username+'&vb_login_password='+password+'&cookieuser=1&s=&securitytoken=guest&do=login&vb_login_md5password=&vb_login_md5password_utf=';
// готовим post запрос
request:='';
request:=request+'POST /login.php?do=login HTTP/1.1'+#13#10;
request:=request+'Host: forum29.ru'+#13#10;
request:=request+'Connection: close'+#13#10;
request:=request+'Proxy-Connection: close'+#13#10;
request:=request+'User-Agent: Mozilla/4.0'+#13#10;
request:=request+'Content-Type: application/x-www-form-urlencoded'+#13#10;
request:=request+'Content-Length: '+IntToStr(Length(request_data))+#13#10;
request:=request+#13#10;
// отсылаем серверу
send(FSocket, Pointer(request)^, Length(request), 0);
send(FSocket, Pointer(request_data)^, Length(request_data), 0);
// то что послали серверу
Memo1.Lines.Add(request);
Memo1.Lines.Add(request_data);
Memo1.Lines.Add('--');
// теперь читаем его ответ
// читаем заголовок ответа
// 1 мин таймаут
savetickcount:=GetTickCount;
response:='';
while GetTickCount-savetickcount<60000 do
begin
ioctlsocket(FSocket, FIONREAD, arg);
if arg>0 then
begin
recv(FSocket, c, 1, 0);
response:=response+c;
if (Length(response)>=4) and (Copy(response, Length(response)-3, 4)=#13#10#13#10) then Break;
end else Sleep(10);
end;
// вот заголовок
// чтобы собрать куки обрабатываем строки с Set-Cookie
// response можно засунуть в отдельный TStrings, чтобы удобней было его парсить
Memo1.Lines.Add(response);
Memo1.Lines.Add('--');
finally
ErrorCode:=closesocket(FSocket);
if ErrorCode<>0 then raise Exception.Create('closesocket');
end;
finally
ErrorCode := WSACleanup;
if ErrorCode<>0 then raise Exception.Create('WSACleanup');
end;
end;
end.
|