I'm just learning to code in C, and I wan't to know if anyone can point me in the right direction, or give me an example for a simple HTTP Get request in C.
Thanks :)
You may checkout libcurl. And here are some examples. It could be as easy as (taken from the doc):
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "curl.haxx.se");
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
Related
I'm trying to use libcurl to send a request like below in C/C++:
GET https://example.com/ HTTP/1.0
Host: https://example.com
Range: bytes=0-0
Connection: keep-alive
PS: I tried 2 case using : in the end of every header's line has "\r\n" and the last header has "\r\n\r\n" (end of header signal) and not adding "\r\n" in the end of file with curl_slist_append() + CURLOPT_HTTPHEADER.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <curl/curl.h>
int main()
{
CURL *curl = curl_easy_init();
struct curl_slist *list = NULL;
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
list = curl_slist_append(list, "Range: bytes=0-0");
list = curl_slist_append(list, "Connection: keep-alive");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
CURLcode ret = curl_easy_perform(curl);
if (ret == CURLE_OK) {
printf("success\n");
} else {
printf("fail\n");
}
curl_slist_free_all(list); /* free the list */
}
}
But seems like it doesn't work.
I tried to use WireShark to capture the HTTP packages by running it with sudo, and filter "http", but nothing is captured.
Am I right to use curl_slist_append() with option CURLOPT_HTTPHEADER to send the headers like I want?
If that is wrong, how do I send it to an HTTP server using libcurl, or do I have to use a socket connection instead?
I'm trying to learn how to use LoadLibrary properly in a C function, but am having difficulty and there are not a lot of good tutorials to follow. I've created a simple C program that uses the libCurl library to successfully fetch the HTML of a website and print it to console. I am now trying to re-implement the same function using LoadLibrary and GetProcAddress and the libcurl.dll.
How do I pass data back from a function that is loaded into memory?
Posted below is the function using the .lib that works and subsequently the function trying to use the DLL that is failing to compile.
Here is my working program:
#include "stdafx.h"
#include "TestWebService.h"
#include "curl/curl.h"
int main(int argc, char **argv)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (curl) {
struct string s;
init_string(&s);
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
/* always cleanup */
printf("%s\n", s.ptr);
free(s.ptr);
curl_easy_cleanup(curl);
}
return 0;
}
Here is my attempt to replicate the same functionality using LoadLibrary only (i.e. Not using the libCurl.lib). But I get the following error message and cannot determine why.
1) a value of type "CURL" cannot be assigned to an entity of type "CURL *"
2) '=': cannot convert from 'CURL' to 'CURL *'
#include "stdafx.h"
#include "TestWebService.h"
#include "curl/curl.h"
typedef CURL (*CurlInitFunc)();
int main(int argc, char **argv)
{
HINSTANCE hLib = NULL;
hLib = LoadLibrary("libcurl.dll");
if (hLib != NULL)
{
CURL *curl;
CurlInitFunc _CurlInitFunc;
_CurlInitFunc = (CurlInitFunc)GetProcAddress(hLib, "curl_easy_init");
if (_CurlInitFunc)
{
curl = _CurlInitFunc();
}
}
return 0;
}
This line:
typedef CURL (*CurlInitFunc)();
declares a pointer to a function that returns a CURL. But the prototype of curl_easy_init() is:
CURL *curl_easy_init();
that means it returns a pointer to CURL, that is CURL*
Therefore the correct declaration is:
typedef CURL *(*CurlInitFunc)();
I am using this code to retrieve the data from the web browser using curl request in c language. I want to store the output in another file or buffer.
#include <stdio.h>
#include <curl/curl.h>
#include <curl/easy.h>
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
The output of this code is an html file. I want to store that html in another file or buffer. How to do that.
Thank you in advance.
Here's a modification to your code that writes the HTML response to a file:
#include <stdio.h>
#include <curl/curl.h>
#include <curl/easy.h>
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* create an output file and prepare to write the response */
FILE *output_file = fopen("output_file.html", "w");
curl_easy_setopt(curl, CURLOPT_WRITEDATA, output_file);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %sn",
curl_easy_strerror(res));
}
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
Here are some related questions:
Saving a file using libcurl in C
Download file using libcurl in C/C++
I try to login google using the libcurl library in C.
But google answers me : "Your browser's cookie functionality is turned off. Please turn it on."
Here is the code :
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
size_t get_buffer(void *buffer, size_t size, size_t nmemb, void *userp);
int main(void)
{
CURL *curl;
CURLcode res;
char *data="service=...&dsh=...&GALX=...&pstMsg=0&dnCon=&checkConnection=&Email=...&Passwd=...";
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://accounts.google.com/ServiceLoginAuth");
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookie");
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookie");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, get_buffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
return 0;
}
Any idea ? Thanks.
You need to call the web page twice to get the cookie. Please see:
http://www.hackthissite.org/articles/read/1078
There's your answer # "Retrieving a webpage (With cookies!):"
I am sending a SOAP request to a web service and I want to check what response I have received. How to achieve this?
My code is:
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
const char *temp = "<?xml version=\"1.0\" encoding=\"utf-8\"?> <S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"xmlns:tns=\"http://ThermodynamicProperties/\"><S:Body> <tns:getSpeciesInformation> <speciesSymbol>CO2</speciesSymbol> <phase>GAS</phase> </tns:getSpeciesInformation> </S:Body> </S:Envelope>";
printf("%s",temp);
curl = curl_easy_init();
if(curl) {
/* First set the URL that is about to receive our POST. This URL can
just as well be a https:// URL if that is what should receive the
data. */
curl_easy_setopt(curl, CURLOPT_URL, "http://thermo.sdsu.edu/servlet/ThermodynamicProperties/ThermodynamicPropertiesService");
/* Now specify the POST data */
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, temp);
printf("OK \n");
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
Right now the response I am getting is:
Sending request.
Reading response.
Received 123 bytes.
How to print what I am receiving??
You can use a callback function which gets passed the response. You can find an example here.