【问题标题】:send HTTP Get request using Curl in c在c中使用Curl发送HTTP Get请求
【发布时间】:2014-12-11 12:28:41
【问题描述】:

请帮助我在 C 中使用 curl 实现 HTTP Get 请求。

我需要使用https://www.googleapis.com/tasks/v1/users?name=pradeep&lastname=singla 等参数点击 URL 我使用CURLOPT_HTTPHEADER 使用Header 设置参数但没有成功。我像

一样实现了它
struct curl_slist* contentheader = NULL;    

contentheader = curl_slist_append(contentheader, "name=pradeep");
contentheader = curl_slist_append(contentheader, "lastname=singla");

curl_easy_setopt(curl, CURLOPT_URL, "https://www.googleapis.com/tasks/v1/users");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, contentheader);
curl_easy_perform(curl); 

在这种情况下,会出现“没有正确的 APi 请求”之类的错误。所以我认为我可以使用

char *charff = "name=pradeep&lastname=singla";    
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, charfff);
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); curl_easy_perform(curl);

但同样的错误正在发生。

谁能帮帮我?我可以同时请求 POST 和 GET 方法,因为服务器方法可能随时更改。

【问题讨论】:

  • 为什么不像通常那样简单地将参数添加到 URL 中?就像您在显示的 URL (https://www.googleapis.com/tasks/v1/users?name=pradeep&lastname=singla) 中所拥有的一样。

标签: c curl libcurl


【解决方案1】:

即使带有“参数”,该 URL 也只是一个 URL,您可以使用 CURLOPT_URL 对其进行完整设置。

它们不是标题,也不是邮局。

CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, 
    "https://www.googleapis.com/tasks/v1/users?name=pradeep&lastname=singla");
curl_easy_perform(curl);

【讨论】:

  • 将来如果服务器 HTTP 方法更改为 POST,那么我必须使用 POST 方法发送参数,所以我在想我将来不需要担心方法的方式。只需要一个可以同时处理这两种情况(GET 和 POST)的解决方案
猜你喜欢
  • 2015-03-15
  • 2015-04-27
  • 2023-04-02
  • 1970-01-01
  • 2018-03-30
  • 1970-01-01
  • 2016-02-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多