简体   繁体   English

libcurl如何在URL重定向到自身时停止重定向

[英]libcurl how to stop redirecting when an url redirect to itself

I use libcurl to get webpage's source code, everything goes all right until to get the source code of "www.macys.com". 我使用libcurl来获取网页的源代码,直到获得“ www.macys.com”的源代码,一切都会顺利进行。

My programme gets nothing without CURLOPT_FOLLOWLOCATION option, but when with this option, the redirecting location is always '/' and the curl_easy_perform function will not return unless I set the CURLOPT_MAXREDIRS option. 没有CURLOPT_FOLLOWLOCATION选项,我的程序什么也不会得到,但是使用此选项时,重定向位置始终为'/',除非我设置CURLOPT_MAXREDIRS选项,否则curl_easy_perform函数将不会返回。

My question is how can I get this webpage's source code with libcurl ? 我的问题是如何使用libcurl获取此网页的源代码? Thanks a lot. 非常感谢。

My libcurl version : 7.23.1, OS: CentOS 6.0(final) x64 我的libcurl版本:7.23.1,操作系统:CentOS 6.0(最终版)x64

If the server keeps redirecting the client, and you ask the client to follow the redirects then what are you expecting it to do? 如果服务器一直在重定向客户端,并且您要求客户端遵循重定向,那么您期望它做什么?

The only option is to stop after a certain amount of steps, which is what CURLOPT_MAXREDIRS is used for. 唯一的选择是在经过一定数量的步骤后停止,这就是CURLOPT_MAXREDIRS所使用的。

The reason things like this happens is mostly because the site is checking for client specific details (user-agent, cookies, referer or similar) that your curl use doesn't provide. 发生这种情况的原因主要是因为站点正在检查您的curl使用无法提供的特定于客户端的详细信息(用户代理,Cookie,引荐来源网址或类似内容)。 You can often make your curl requests more similar to a browser's and then avoid this effect. 您通常可以使卷曲请求与浏览器的卷曲请求更相似,然后避免这种影响。

禁用CURLOPT_FOLLOWLOCATION并手动跟踪重定向以确保不会发生循环,或者将CURLOPT_MAXREDIRS设置为只能通过自我重定向才能达到的疯狂数字(10)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM