在C中使用Curl发送HTTP Get请求 [英] send HTTP Get request using Curl in c
问题描述
请帮助我使用C语言中的curl实现HTTP Get请求.
Please help me to implement an HTTP Get request using curl in C.
我需要使用 https://这样的参数来访问URL www.googleapis.com/tasks/v1/users?name=pradeep&lastname=singla
我使用CURLOPT_HTTPHEADER
设置带有Header的参数,但没有成功.我将其实现为
I need to hit URL with parameters like https://www.googleapis.com/tasks/v1/users?name=pradeep&lastname=singla
I used CURLOPT_HTTPHEADER
to set parameters with Header but without success. I implemented it like
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请求"之类的错误.所以我认为我可以使用
In this case an error occured like "no correct APi request". So I thought that I can use
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方法,因为服务器方法可能随时更改.
Can anyone please help me ? I can put my request for both POST and GET method because server method may change anytime.
推荐答案
该URL只是具有"parameters"的URL,您可以通过
The URL is just a URL even with "parameters" and you set it in full with CURLOPT_URL.
它们不是标题,也不是后字段.
They're not headers and they're not postfields.
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);
这篇关于在C中使用Curl发送HTTP Get请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!