#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>

int main(int argc, char **argv)
{
	CURL *curl = NULL;
	CURLcode res;
	FILE *logfile = NULL;

	const static char* headers[] = { 
				"Accept: text/*", 
				"Cache-Control: no-cache", 
				"Content-Type: application/x-www-form-urlencoded", 
	}; 
	const static int numHeaders = 3; /*sizeof(headers) / sizeof(*headers);*/ 
	static struct curl_slist *headerList = NULL; 
	static int bHeaderlistInitialized = 0; 
	static char postdata[] = "name=lzx&password=111";

	printf("Init header list.\n");
	if (!bHeaderlistInitialized)
	{ 
		int i; 
		for (i = 0; i < numHeaders; ++i) 
			headerList = curl_slist_append(headerList, headers[i]); 
		bHeaderlistInitialized = 1; 
	} 


	logfile = fopen("/usr/local/src/crultest/dumpit", "w");

	printf("curl init.\n");
	curl = curl_easy_init();
	if (curl)
	{
		printf("set options.\n");
		curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.8.30/curltest.php3");
        curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1); 
        curl_easy_setopt(curl, CURLOPT_HEADER, 1); 
        curl_easy_setopt(curl, CURLOPT_NOBODY, 0); 
        curl_easy_setopt(curl, CURLOPT_CLOSEPOLICY, CURLCLOSEPOLICY_LEAST_RECENTLY_USED); 
        curl_easy_setopt(curl, CURLOPT_USERAGENT, "WebOCR Server/2.0"); 

        curl_easy_setopt(curl, CURLOPT_FILE, logfile); 
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerList); 

		curl_easy_setopt(curl, CURLOPT_POST, 1); 
		curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postdata); 
		curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(postdata)); 
		printf("perform.\n");
		res = curl_easy_perform(curl);
		printf("after perform.\n");
		curl_easy_cleanup(curl);
	}

	fclose(logfile);
	return 0;
}
 

