ورود

View Full Version : سوال در مورد Redirection Handling با WinInet



Veyskarami
یک شنبه 18 خرداد 1393, 15:12 عصر
با سلام خدمت دوستان و همکاران عزیز

من یه فانکشن طراحی کردم که متد POST رو با WinInet پیاده سازی میکنه منتها ایراد کار اینه که Url ی که به فانکشن میدم اگه ریدایرکت بشه دیگه نمیتونه Response جدید رو بگیره و به مسیر جدید هدایت بشه کد زیر درست کار میکنه فقط مشکل کارش همینه عدم پشتیبانی از Redirection Handling هستش که از دوستان میخوام کمک کنن این فانکشن رو تکمیلش کنم

ممنون از همگی


type
IResult = packed record
Status : string;
Result : string;
Url: string;
Cookie: string;
Scanned: boolean;
Size : Dword;
UseBreakFor:Boolean;
BreakFor:Boolean;
end;





function PostWithWinet(sURL,sPostData,UserAgent,Header,Cook ie,ProxyServer:string;ProxyPort:integer):IResult;
const
RequestMethod = 'POST';
HTTP_VERSION = 'HTTP/1.1';
var
dwSize:DWORD;
dwFileSize: Int64;
dwBytesRead,dwReserved,size:DWORD;
hInte,hConnection,hRequest:HInternet;
ContentSize:array[1..1024] of Char;
infoBuffer: array[0..20] of Char;
lpBuffer:string;
buf: array[0..1024 + 1] of Char;
HostPort:Integer;
HostName,FileName,sHeader,Reply:String;
okay:LongBool;
dwIndex,dwCodeLen:Dword;
S:TStringList;




procedure ParseURL(URL: string;var HostName,FileName:string;var HostPort:Integer);
var
i,p,k: DWORD;
function StrToIntDef(const S: string; Default: Integer): Integer;
var
E: Integer;
begin
Val(S, Result, E);
if E <> 0 then Result := Default;
end;
begin
if lstrcmpi('http://',PChar(Copy(URL,1,7))) = 0 then System.Delete(URL, 1, 7);
HostName := URL;
FileName := '/';
HostPort := INTERNET_DEFAULT_HTTP_PORT;
i := Pos('/', URL);
if i <> 0 then
begin
HostName := Copy(URL, 1, i-1);
FileName := Copy(URL, i, Length(URL) - i + 1);
end;
p:=pos(':',HostName);
if p <> 0 then
begin
k:=Length(HostName)-p;
HostPort:=StrToIntDef(Copy(HostName,p+1,k),INTERNE T_DEFAULT_HTTP_PORT);
Delete(HostName,p,k+1);
end;
end;


begin
dwFileSize :=0;
if Cookie <> '' then UserAgent:=UserAgent+#13#10+'Cookie: '+Cookie;
if Header <> '' then UserAgent:=UserAgent+#13#10+Header;
Result.Scanned:=False;
ParseURL(sURL,HostName,FileName,HostPort);
if (ProxyServer <> '' )and(ProxyPort>0) then
hInte := InternetOpen(Pchar(UserAgent), INTERNET_OPEN_TYPE_PROXY, Pchar(ProxyServer+':'+Inttostr(ProxyPort)), nil, 0) else
hInte := InternetOpen(Pchar(UserAgent), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
if hInte<>nil then
begin
hConnection := InternetConnect(hInte,PChar(HostName),HostPort,nil ,nil,INTERNET_SERVICE_HTTP,0,0);
if hConnection<>nil then
begin
hRequest := HttpOpenRequest(hConnection,PChar(RequestMethod),P Char(FileName),HTTP_VERSION,'',nil,INTERNET_FLAG_N O_CACHE_WRITE or INTERNET_FLAG_RELOAD,0);
if hRequest<>nil then
begin
sHeader := 'Content-Type: application/x-www-form-urlencoded' + #13#10;
sHeader := sHeader+'Cookie: '+Cookie+#13#10;




HttpAddRequestHeaders(hRequest,PChar(sHeader),Leng th(sHeader),HTTP_ADDREQ_FLAG_ADD or HTTP_ADDREQ_FLAG_REPLACE);
if HttpSendRequest(hRequest,nil,0,PChar(sPostData),Le ngth(sPostData)) then
begin
dwReserved:=0;
dwSize:=SizeOf(ContentSize);
if HttpQueryInfo(hRequest,HTTP_QUERY_CONTENT_LENGTH,@ ContentSize,dwSize,dwReserved) then
begin
hRequest := InternetOpenUrl(hRequest, PChar(sUrl), nil, 0, INTERNET_FLAG_RELOAD, 0);




repeat
begin
dwBytesRead := 128;
if not InternetReadFile(hRequest, @lpBuffer, 1024, dwBytesRead) then Break;
lpBuffer[dwBytesRead] := #0;
result.Result := result.Result + lpBuffer;
end;
until dwBytesRead =0;


InternetGetCookie(Pchar(sUrl), nil, buf, size);
Result.Url:=sUrl;
Result.Cookie:=Buf;
Result.Scanned:=True;
InternetSetCookie(Pchar(sUrl),'',Buf);
Result.Status:=GetStatus(hRequest).Status;
end;
end;
end;


InternetCloseHandle(hRequest);
end;
InternetCloseHandle(hConnection);
end;
InternetCloseHandle(hInte);


end;

Mask
یک شنبه 18 خرداد 1393, 17:44 عصر
داش آرش باید سوالت رو تو بخش شبکه میپرسیدی:چشمک:
و در مورد سوالتون : اگه بتونید لینک url رو بدید ، میشه با خود IdHttp عملیات ریدایرکت شدن رو کنترل کرد.

Veyskarami
یک شنبه 18 خرداد 1393, 23:51 عصر
سوال در مورد مباحث عمومی دلفیه دیگه...!
با IdHttp بلدم کامپوننت های دیگه هم هستن منتها میخوام با API و همین فانکشنی که نوشتم کار کنم روش کار رو هم میخوام یاد بگیرم
اوکی ممنون خودم ردیفش میکنم