首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

怎么判断301或者302的转向

2012-02-08 
如何判断301或者302的转向?Delphi(Pascal) codefunction CheckUrl(url:string):booleanvarhSession, hfil

如何判断301或者302的转向?

Delphi(Pascal) code
function CheckUrl(url:string):boolean;var    hSession, hfile, hRequest: hInternet;    dwindex,dwcodelen :dword;    dwcode:array[1..20] of char;    res : pchar;begin    //检查URL是否包含http://,如果不包含则加上        if pos('http://',lowercase(url))=0 then             url := 'http://'+url;            Result := false;            hSession := InternetOpen('InetURL:/1.0',             INTERNET_OPEN_TYPE_PRECONFIG,nil, nil, 0); //建立会话句柄        if assigned(hsession) then         begin             hfile := InternetOpenUrl(hsession, pchar(url), nil, 0,                INTERNET_FLAG_RELOAD, 0);        //打开URL所指资源               dwIndex := 0;             dwCodeLen := 10;             HttpQueryInfo(hfile, HTTP_QUERY_STATUS_CODE,                 @dwcode, dwcodeLen, dwIndex); //获取返回的HTTP头信息               res := pchar(@dwcode);        ShowMessAge(pchar(@dwcode));        result:= (res ='200');           if assigned(hfile) then                 InternetCloseHandle(hfile);     //关闭URL资源句柄            InternetCloseHandle(hsession);     //关闭Internet会话句柄        end;end;


Delphi(Pascal) code
var  IMGHttp:TIDHttp;begin  IMGHttp:=TIDHttp.Create(nil);  Try    IMGHttp.Disconnect;    IMGHttp.HandleRedirects:= True;    IMGHttp.ReadTimeout:= 10*1000;    IMGHttp.Request.UserAgent:='Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)';    IMGHttp.Get('http://127.0.0.1/301.asp');    ShowMessAge(IntToStr(IMGHttp.ResponseCode));  Finally    IMGHttp.Free;  End;end;


使用以上两种方式均无法判断301或者302.而是直接返回了转向后的地址状态200 
请问各位大牛有什么方法判断么?

[解决办法]
方法1:
hfile := InternetOpenUrl(hsession, pchar(url), nil, 0,
INTERNET_FLAG_RELOAD, 0); //打开URL所指资源

INTERNET_FLAG_RELOAD标志
加上INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP好象是禁止重定向

hfile := InternetOpenUrl(hsession, pchar(url), nil, 0,
INTERNET_FLAG_RELOAD or INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP, 0);

方法2:
IMGHttp.HandleRedirects
这个属性应该是设置是否允许跳转吧

热点排行