为什么服务器使用 libcurl 和浏览器响应不同的 HTTP 重定向 "Location" 值?

Why server responses different HTTP redirect "Location" value using libcurl and browser?

我正在使用 libcurl 从服务器下载文件,使用 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L) 进行 3xx 重定向。但是,发生 HTTP 重定向时,libcurl 不会下载文件。通过wiresark抓包,发现"Location"的值和直接用浏览器不一样,这也是我的程序无法下载文件的主要原因。例如,原来的url是“http://oldurl.com", the server's response redirect url in "Location" obtaining by libcurl is "http://redirection1.com”,但是用浏览器是"redirection2.com"。

服务器可能正在根据 User-Agent header 更改行为。您可以使用 curl --user-agent 标志将用户代理设置为站点将识别为来自浏览器的内容以对此进行测试。 This site 是一个很好的字符串资源。