使用查询字符串的Libcurl HTTP GET请求

问题描述

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

我的代码:

    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,"id=me_ZwNjBQRlZGL0AwR0ZQNjAQR1AQZ3At==",CURLUPART_SCHEME,"http",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,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

解决方法

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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...