使用libcurl提交批处理URL的正确BING Api POST URL是什么?

时间:2020-07-07 18:15:57

标签: c++ json post libcurl bing-api

我正在尝试将URL列表提交给BING网站管理员。在CPP中

根据BING:

什么是URL提交API? 易于插入的API解决方案,网站可以随时调用以通知Bing 网站内容被更新或创建,允许即时爬网,建立索引 并发现您的网站内容。

我了解需要发送POST请求,JSON请求示例:

POST /webmaster/api.svc/json/SubmitUrlbatch?​apikey=sampleapikeyEDECC1EA4AE341CC8B6 HTTP/1.1​
Content-Type: application/json; charset=utf-8​
Host: ssl.bing.com​

{
"siteUrl":"http://yoursite.com",​
"urlList":[
"http://yoursite.com/url1",
"http://yoursite.com/url2",
"http://yoursite.com/url3"
]
}

我使用libcurl发送POST请求编写了以下内容。

std::string curl_post_json(const std::string url, const std::string json) {
    CURL* curl;
    CURLcode res;
    std::string ret;
    struct curl_slist* header = NULL;
    std::string content_len = "Content-Length: " + std::to_string(json.size());
    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();
    if (curl) {
        header = curl_slist_append(header, "Content-Type: application/json; charset=utf-8");
        header = curl_slist_append(header, content_len.c_str());
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json.c_str());
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_response);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ret);
        res = curl_easy_perform(curl);
        if (res != CURLE_OK) {
            ret = curl_easy_strerror(res);
        }
        curl_easy_cleanup(curl);
    }
    curl_global_cleanup();
    return ret;

注意:write_response是将响应复制到字符串的简单函数(指针)。

我使用以下网址:

https://ssl.bing.com/webmaster/api.svc/json/SubmitUrlbatch?apikey=<mykey>

但是,收到:

{"ExceptionType"="System.InvalidOperationException","Message"="Authenticationfailed.","StackTrace"=null**strong text**}

POST提交的正确网址是什么?

1 个答案:

答案 0 :(得分:0)

我现在可以使用该功能了。需要添加以上内容。

header = curl_slist_append(header,"Host: ssl.bing.com");

而且urlList字段不能为空,如果为空,则返回错误响应。