在Delphi中使用WinHTTP进行重定向后,是否可以获取最终URL? [英] Is there way to get the final URL after redirects using WinHTTP in Delphi?

查看:84
本文介绍了在Delphi中使用WinHTTP进行重定向后,是否可以获取最终URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用以下代码轻松地从URL获取HTML源,但是如何获取实际的URL本身呢?因为有时初始URL会经过一些重定向,而实际URL是不同的,所以我想捕获它以供使用.我似乎找不到有关在Delphi中使用winHTTP的方法或属性的任何好的文档.谢谢!

I can use the following code to easily get the HTML source from the URL, but how can I get the actual URL itself? Because sometimes the initial URL goes through some redirects and the actual URL is not the same and I would like to capture it for usage. I cannot seem to find any good documentation on the usage of methods or properties for winHTTP in Delphi. Thanks!

var http: variant;
begin
 http:=createoleobject('WinHttp.WinHttpRequest.5.1');
 http.open('GET', 'http://URLtoWebsite.com', false);
 http.send;
 showmessage(http.responsetext);
end;

推荐答案

您可以使用类似的

function GetFinalURL(const AMainURL: string): string;
var
  http: Variant;
begin
  Result := '';
  http := CreateOleObject('WinHttp.WinHttpRequest.5.1');
  http.Option(6) := False;
  http.open('GET', AMainURL, false);
  http.send;
  if http.Status = 302 then
    Result := http.getResponseHeader('Location')
  else
  Result := AMainURL;
end;

使用 Indy

function GetFinalURL(const AMainURL: string): string;
var
  idHTTP: TIdHTTP;
begin
  Result := '';
  idHTTP := TIdHTTP.Create(nil);
  try
    idHTTP.HandleRedirects := True;
    try
      idHTTP.Get(AMainURL);
      Result := idHTTP.Request.URL;
    except
    end;
  finally
    idHTTP.Free;
  end;
end;

这篇关于在Delphi中使用WinHTTP进行重定向后,是否可以获取最终URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆