【问题标题】:Libcurl HTTP GET request using a query string使用查询字符串的 Libcurl HTTP GET 请求
【发布时间】:2020-12-02 13:34:46
【问题描述】:

我正在努力做一个传递查询字符串的 GET 请求。每次我运行它都会收到 410 gone 响应,我检查了链接是否被删除但仍然可以访问。

我的代码:

    CURLUcode rc;
    CURLU* url = curl_url();
    CURL* handle = curl_easy_init();
    CURLcode res;
    struct curl_slist* list = NULL;
    
    if (handle) {
        rc = curl_url_set(url, CURLUPART_HOST, "www.example.com", 0);
        rc = curl_url_set(url, CURLUPART_QUERY,"b=sashio", CURLU_APPENDQUERY);
        rc = curl_url_set(url, CURLUPART_QUERY, "id=me_ZwNjBQRlZGL0AwR0ZQNjAQR1AQZ3At==", CURLU_APPENDQUERY);
        rc = curl_url_set(url, CURLUPART_SCHEME, "http", 0);
        rc = curl_url_set(url, CURLUPART_PATH, "/en/m.php?", 0);
        curl_easy_setopt(handle, CURLOPT_CURLU, url);

        list = curl_slist_append(list, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
        curl_easy_setopt(handle, CURLOPT_HTTPHEADER, list);

        curl_easy_setopt(handle, CURLOPT_HTTPGET, 1L);
        curl_easy_setopt(handle, CURLOPT_VERBOSE, 1L);
        curl_easy_setopt(handle, CURLOPT_ENCODING, "");    
    }

    res = curl_easy_perform(handle);

详细输出:

* Mark bundle as not supporting multiuse
* HTTP 1.0, assume close after body
< HTTP/1.0 410 Gone
< Date: Wed, 12 Aug 2020 19:41:23 GMT
< Server: Apache
< X-UA-Compatible: IE=EmulateIE9
< Last-Modified: Wed, 12 Aug 2020 19:41:23 GMT
< Cache-Control: private, must-revalidate, max-age=0
< Etag: "1804121564-00010031-BZGZ0AGt5BQt2AGD1ZQZ1AmV"
< Set-Cookie: PHPSESSID=tp3mbkk7148cm989areejhhd90; path=/
< Expires: Thu, 19 Nov 1981 08:52:00 GMT
< Pragma: no-cache
< Content-Length: 192
< Connection: close
< Content-Type: text/html
<
* Closing connection 0

【问题讨论】:

  • 我把逗号放错了。
  • 你是如何检查链接的?
  • 通过浏览。
  • 不应该第一个CURLUPART_QUERY 省略CURLU_APPENDQUERY 标志,因为尚未设置查询?更重要的是,路径和查询之间的? 不是路径(或查询)本身的一部分,因此不应将其显式包含在CURLUPART_PATH(或CURLUPART_QUERY)中
  • 您的浏览器可能在 HTTP 请求中向服务器发送会话 cookie,因此它从服务器获得的响应与 libcurl 不同。

标签: c++ http get libcurl


【解决方案1】:

特别感谢大家@AndreasWenzel。 浏览器正在向服务器发送 cookie,就像 AndreasWenzel 所说的那样。所以在标题中我添加了 cookie,现在我收到了正确的响应。 祝你有美好的一天!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-04
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    • 2015-04-09
    • 2017-02-05
    相关资源
    最近更新 更多