尝试访问机场 RSS 提要时获取 'access denied'

Getting 'access denied' when trying to access an airport RSS feed

我正在尝试使用 C 中的 curl 库访问机场 RSS 提要。但是,每当我尝试访问它时,我都会收到访问被拒绝的错误。以下是我正在使用的代码。它与 https://curl.haxx.se/libcurl/c/simple.html:

中的示例代码几乎相同
#include <stdio.h>
#include <curl/curl.h>

int main(int argc, char **argv)
{
        CURL *curl;
        CURLcode res;
        char *feed_addr = "http://w1.weather.gov/xml/current_obs/KUCP.rss";
        //airport not in the state I live

        curl = curl_easy_init();
        if(curl) {
                curl_easy_setopt(curl, CURLOPT_URL, feed_addr);

                curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

                // Perform the request, res will get the return code
                res = curl_easy_perform(curl);
                // Check for errors
                if(res != CURLE_OK) {
                        fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
                }

                // always cleanup
                curl_easy_cleanup(curl);
        }
        return 0;
}

除了 weather.gov(google、youtube、mit.edu)之外,我已经在其他网站上尝试过此操作,它们都运行良好。但是当我尝试这样做时,即使使用其他机场 RSS 提要(可以在 http://w1.weather.gov/xml/current_obs/seek.php?state=pa&Find=Find 找到),我也会遇到相同的访问被拒绝错误。当我将 feed_addr 设置为任何 weather.gov 页面时,我也会遇到同样的错误。

让这个陌生人,当我尝试使用 Python3 的 urllib.request 模块访问提要时,它工作得很好。我也可以使用 Google Chrome 访问它。所以我可以排除它不想让我访问提要的想法。

有什么我想念的吗?有没有办法通过 curl 库获取提要?或者有没有办法使用不同的库?

回答我自己的问题(我本来不打算,但我在别人回答之前解决了这个问题):

这是我开始工作的代码

#include <stdio.h>
#include <curl/curl.h>

int main(int argc, char **argv)
{
        CURL *curl;
        CURLcode res;
        char *feed_addr = "http://w1.weather.gov/xml/current_obs/KUCP.rss";
        //airport not in the state I live    

        curl = curl_easy_init();
        if(curl) {
                curl_easy_setopt(curl, CURLOPT_URL, feed_addr);

                curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

                //line that solved my issue
                curl_easy_setopt(curl, CURLOPT_USERAGENT, <string>);
                /**
                 * The line as written will not work.
                 * I'm only using <string> as a stand-in for some personal information
                 * If you're having the same issue as I did, I explain how to solve it under the code
                 */

                // Perform the request, res will get the return code
                res = curl_easy_perform(curl);
                // Check for errors
                if(res != CURLE_OK) {
                        fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
                }

                // always cleanup
                curl_easy_cleanup(curl);
        }
        return 0;
}

我解决这个问题的方法是找到一个打印出我的 HTTP 请求的网站。在这种情况下,它是 http://rve.org.uk/dumprequest。我在一个选项卡中像往常一样访问该站点,我使用该程序获取源代码,然后在另一个选项卡中启动它。那是我手动打开页面时看到有几个字段存在,但是使用代码时却没有。

因此,我查看了 curl_easy_setopt 以查看是否有设置这些字段的方法。事实证明,curl_easy_setopt 在网站 https://curl.haxx.se/libcurl/c/curl_easy_setopt.html 上有这方面的文档和更多文档。在其中一个评论的建议下,我先看了CURLOPT_USERAGENT。

因为那个拿了 char *,我在 HTTP 请求中找到了以 User-Agent: 开头的行,复制并粘贴了其余部分,那就是 <string>上线。

因此,如果请求包含以下行:

User-Agent: Lord Voldemort (Tom Marvolo Riddle)

我包含的行是:

curl_easy_setopt(curl, CURLOPT_USERAGENT, "Lord Voldemort (Tom Marvolo Riddle)");