C + + (learning notes)--vs2015 static compilation libcurl__c++

Source: Internet
Author: User
Tags http cookie http post openssl install perl
first, the latest Libcurl static compilation tutorial (curl-7.51 version/curl-7.52 edition)

1, install Perl, download in the official website, after the installation, test perl-v whether successful

2, compile the OpenSSL (has compiled the download address)

Perl Configure Vc-win32--prefix=d:\openssl

Ms\do_ms.bat

Nmake-f Ms\nt.mak (Static compilation)

Nmake-f Ms\nt.mak Install

3, download Libcurl (compiled version of the 7.51 version of the download address 7.52 download address)

In the source code projects can find VC14 this project file, open it.

Then look at the path of the OpenSSL INC32 directory for the project where you can see ... /.. /.. /.. /.. /OPENSSL/INC32, so OpenSSL and Libcurl are based on a sibling directory.

Then add the 2 static libraries in the Lib directory in OpenSSL to the dependencies in the Libeasy32 and ssleay32.

Set the MTD or Mt of the project you want to compile, and then you can compile it.

Of course, you can download my compiled, in the above address.

4, here is a master further package of classes, very easy to use (download the address)

Supports synchronous and asynchronous requests.

second, the simple use of the tutorial

1. Add include directory: Include in Libcurl

2. Add Library Directory: Lib directory in Libcurl

3. Add pre-compiled macros: Use_openssl,curl_staticlib

4. Add library file:

Crypt32.lib
Ws2_32.lib
Wldap32.lib
Libcurl.lib

5, access to 12306 Web sites, easy to bypass the example of HTTPS encryption:

#include "stdafx.h" #include <stdio.h> #include <windows.h> #include <iostream> #include <string&
Gt
  
#include <curl/curl.h> using namespace std;
    size_t Reply (void * ptr, size_t size, size_t nmemb, void * stream) {string* str = (string*) stream;
    (*STR). Append ((char*) PTR, size*nmemb);
return SIZE*NMEMB; } wstring str_cvt (const string& from_str, int cvt_type) {int wstr_len = MultiByteToWideChar (cvt_type, 0, From_str
    . C_STR (),-1, nullptr, 0);
    wchar_t *wstr = new Wchar_t[wstr_len + 1];
    memset (wstr, 0, (Wstr_len + 1) *sizeof (wchar_t));
    MultiByteToWideChar (cvt_type, 0, From_str.c_str (),-1, WSTR, Wstr_len); Wstring ret_str (WSTR);
    Delete[] WSTR;
return ret_str; } string str_cvt (const wstring& from_str, int cvt_type) {int str_len = WideCharToMultiByte (cvt_type, 0, From_str.
    C_str (),-1, nullptr, 0, nullptr, nullptr);
    Char *str = new Char[str_len + 1];
    memset (str, 0, Str_len + 1); WideCharToMultiByte (CVT_type, 0, From_str.c_str (),-1, str, str_len, nullptr, nullptr); String Ret_str (str);
    Delete[] STR;
return ret_str;
    int main () {string data;//The Returned content CURL *curl;

    Curlcode Res;

    Curl_global_init (Curl_global_all);
    Curl = Curl_easy_init ();
        if (Curl) {curl_easy_setopt (curl, Curlopt_url, "Https://kyfw.12306.cn/otn/login/init");
        Curl_easy_setopt (Curl, Curlopt_ssl_verifypeer, 0L);
        Curl_easy_setopt (Curl, Curlopt_ssl_verifyhost, 0L);
        Curl_easy_setopt (Curl, curlopt_writefunction, Reply);
        Curl_easy_setopt (Curl, Curlopt_writedata, &data);
        res = curl_easy_perform (curl);
        if (res!= CURLE_OK) fprintf (stderr, Curl_easy_perform () failed:%s\n ", Curl_easy_strerror (res));
    Curl_easy_cleanup (curl);
    } curl_global_cleanup ();
    cout << str_cvt (STR_CVT (Data,cp_utf8), CP_ACP) << Endl;
    GetChar ();
return 0; }


Four, the parameter detailed explanation:

1. Global Environment Initialization

	Curl_global_init (Curl_global_all);
	Curl_global_cleanup ();

Curl_global_all initializes all possible values, typically using this macro

Curl_global_win32 Initialization Winsock Library

Curl_global_ssl initialization of SSL-related environments

2. Initialize Curl

CURL *curl = Curl_easy_init ();
Curl_easy_cleanup (curl);

Initialize, get a Curl object, no parameter function

3, very important function, set Curl configuration information

Curlcode curl_easy_setopt (Curl *curl, curloption option, ...)

A, set the URL:

Prevent 301, 302 jumps:

		Curl_easy_setopt (curl, curlopt_followlocation,0);
The requested address:

		Curl_easy_setopt (Curl, Curlopt_url, "http://www.baidu.com");
B, set post:

		Curl_easy_setopt (Curl, curlopt_post, 1);
		Curl_easy_setopt (Curl, Curlopt_postfields, "name=xxx");
C, set Coockie

		Curl_easy_setopt (Curl, Curlopt_cookiefile, "d:/coockie.txt");
D, Set data acceptance

		Curl_easy_setopt (Curl,curlopt_writefunction, showhtml);
		Curl_easy_setopt (Curl, Curlopt_writedata, &RETSTR);
E, set the progress bar

		Curl_easy_setopt (Curl, curlopt_noprogress, false);
		Curl_easy_setopt (Curl, curlopt_progressfunction, progress_callback);
		Curl_easy_setopt (Curl, curlopt_progressdata, progress);
F, set SSL ignore authentication

		Curl_easy_setopt (Curl, Curlopt_ssl_verifypeer, 0L);
		Curl_easy_setopt (Curl, Curlopt_ssl_verifyhost, 0L);

G, set header information to accept

Whether the returned body has header information: curl_easy_setopt (Curl, Curlopt_header, 1);

		Curl_easy_setopt (Curl, curlopt_headerfunction, showhtml);
		Curl_easy_setopt (Curl, Curlopt_headerdata, &RETSTR);
h, set up upload

		Curl_easy_setopt (Curl, curlopt_readfunction, showhtml);
		Curl_easy_setopt (Curl, Curlopt_readdata, &RETSTR);

I, set the transmission time

		Curl_easy_setopt (Curl, curlopt_timeout,);
		Curl_easy_setopt (Curl, curlopt_connecttimeout, 30);

J, set request header information

Default header information:

host:http1.1 (most http1.0) versions require the client to request this information header.
Pragma: "No-cache". Indicates that data is not buffered.
Accept: "*/*". Indicates that any type of data is allowed to be received.
Expect: When submitting a request to the HTTP server as a post, Libcurl sets the message header to "100-continue", which requires the server to return an "OK" message before the request is formally processed. If the post data is small, libcurl may not set the message header

		struct Curl_slist *headers = NULL; 
		headers = curl_slist_append (headers, "Hey-server-hey:how Are You");
		headers = curl_slist_append (headers, "X-silly-content:yes");

		Curl_easy_setopt (Curl, Curlopt_httpheader, headers);

		
4, get the answer header information

		Curlinfo info;
		Curl_easy_getinfo (curl, info);

To get info in the

		Curlinfo_response_code
		curlinfo_header_size
		curlinfo_cookielist

5. Perform query operation

		res = curl_easy_perform (curl);
		if (res!= CURLE_OK)
			fprintf (stderr, Curl_easy_perform () failed:%s\n ", Curl_easy_strerror (res));

The return value types are:

		CURLE_OK  //Mission accomplished everything is good
		curle_unsupported_protocol  //unsupported protocol, specified by the header of the URL
		curle_couldnt_connect  //Cannot connect to remote host or proxy
		Curle_remote_access_denied  //Access denied
		curle_http_returned_error  //HTTP return error
		Curle_read_error  //Error reading local file

6. Extension of Breakpoint

		Curl_easy_setopt (Curl, Curlopt_range, 0-499, 1000-1499);
		Curl_easy_setopt (Curl, Curlopt_resume_from, 1500);


Curl_easy_setopt parameter Daquan:

Curlopt_header: Set to 1, the returned content contains the HTTP HEADER; curlopt_followlocation: set to 0, then no automatic 301,302 jump; *curlopt_infilesize:
When you upload a file to a remote site, this option tells PHP the size of the file you uploaded.
*curlopt_verbose: If you want to curl report every unexpected thing, set this option to a value other than 0.
*curlopt_header: If you want to include a header in the output, set this option to a value other than 0.

*curlopt_noprogress: If you don't have PHP to display a process bar for curl transmissions, set this option to a non 0 value.

Note: PHP automatically sets this option to a value other than 0, and you should only change this option for debugging purposes.
*curlopt_nobody: If you don't want to include the body part in the output, set this option to a value other than 0. *curlopt_failonerror: If you want PHP to be in error (HTTP code returns greater than or equal to 300), do not display, set this option to a person not 0 value.
The default behavior is to return a normal page, ignoring the code.
*curlopt_upload: If you want PHP to be ready for upload, set this option to a value other than 0. *curlopt_post: If you want PHP to do a regular HTTP POST, set this option to a non 0 value.
This post is an ordinary application/x-www-from-urlencoded type, most of which is used by HTML forms.
*curlopt_ftplistonly: Set this option to a value other than 0, PHP will list the list of directory names for FTP.
*curlopt_ftpappend: Set this option to a value other than 0, PHP will apply the remote file instead of overwriting it.
*CURLOPT_NETRC: Set this option to a value other than 0, PHP will find the username and password of the remote site you want to connect to in your ~./netrc file.
*curlopt_followlocation: Set this option to a non 0 value (like "Location:"), the server will send it as a part of the HTTP header (note that this is recursive, PHP will send the shape as "Location:" the head). *curlopt_put: Set this option for a non 0 value to upload a file with HTTP. To upload this file you must set up Curlopt_infile and curlopt_infilesize selection*curlopt_mute: Set this option to a value other than 0, PHP will be completely silent for the curl function.
*curlopt_timeout: Sets a long shaping number, as the maximum continuation of how many seconds.
*curlopt_low_speed_limit: Sets a long shaping number to control how many bytes are transferred.
*curlopt_low_speed_time: Set a long shaping number, control how many seconds to transfer curlopt_low_speed_limit The specified number of bytes.
*curlopt_resume_from: Passes a long shaping parameter that contains the byte offset address (the start form you want to transfer to). *curlopt_sslversion: Passes a long parameter containing the SSL version.
The default PHP will be determined by its own efforts, and in more security you must set it manually. *curlopt_timecondition: Passes a long parameter specifying how to handle the Curlopt_timevalue parameter. You can set this parameter to Timecond_ifmodsince or timecond_isunmodsince.
This is for HTTP only. *curlopt_timevalue: Passes a number of seconds from 1970-1-1 to the present.

This time is used by the Curlopt_timevalue option as the specified value or by default timecond_ifmodsince. The value of the following options will be used as a string: *curlopt_url: This is the URL address you want to retrieve with PHP.
You can also set this option when initializing with the Curl_init () function.
*curlopt_userpwd: Pass a string in a form like [Username]:[password] style, the function of PHP to connect.
*curlopt_proxyuserpwd: Passes a string in a form like [Username]:[password] to connect to the HTTP proxy. *curlopt_range: Pass a range that you want to specify. It should be "XY" format, X or Y is excepted.
HTTP transmissions also support several intervals, separated by a x-y,n-m.
*curlopt_postfields: Passes a string of all data that is an HTTP "POST" operation.
*curlopt_referer: Contains a string of "REFERER" headers in the HTTP request.
*curlopt_useragent: Contains a string of "user-agent" headers in the HTTP request. *curlopt_fTpport: Passes an IP address that contains the FTP "POST" instruction. This post instruction tells the remote server to connect to the IP address we specified.
This string can be an IP address, a host name, a network interface name (under Unix), or '-' (using the system default IP address).
*curlopt_cookie: Passes a header connection that contains an HTTP COOKIE.
*curlopt_sslcert: Passes a string containing the PEM format certificate.
*CURLOPT_SSLCERTPASSWD: Passes a password that is required to use the Curlopt_sslcert certificate. *curlopt_cookiefile: A string that passes the name of a file that contains cookie data.
This cookie file can be in Netscape format, or in the HTTP style header that is stockpiled in the file. *curlopt_customrequest: When making an HTTP request, passing a character is used by get or head. It is useful for delete or other operations, more pass a string of used instead of a and head when doing a HTTP request.

This is useful to doing or another, more obscure, HTTP request.

Note: Do not do this before confirming your server support command.
The following options require a file description (obtained by using the fopen () function): *curlopt_file: This file will be the output file where you put the transfer, and the default is stdout.
*curlopt_infile: This file is the input file you sent over.
*curlopt_writeheader: This file is written with the head part of your output.
 *curlopt_stderr: This file is written with errors rather than STDERR.

A simple example: to get the page and show the example of getting progress

void Ccurltest1dlg::onbnclickedok () {CURL *curl;
	Curlcode Res;
	CStringA Retstr;
	CString URL;
	GetDlgItemText (Idc_edit2,url);

	cprogressctrl* progress = (cprogressctrl*) GetDlgItem (IDC_PROGRESS1);
	Curl_global_init (Curl_global_all);
	Curl = Curl_easy_init (); if (Curl) {curl_easy_setopt (curl, Curlopt_url, Unicodetoascii (URL).
		GetBuffer (0)). C_STR ());
		Curl_easy_setopt (Curl, Curlopt_ssl_verifypeer, 0L);
		Curl_easy_setopt (Curl, Curlopt_ssl_verifyhost, 0L);
		Curl_easy_setopt (Curl, curlopt_writefunction, showhtml);
		Curl_easy_setopt (Curl, Curlopt_writedata, &AMP;RETSTR);
		Curl_easy_setopt (Curl, curlopt_noprogress, false);
		Curl_easy_setopt (Curl, curlopt_progressfunction, progress_callback);

		Curl_easy_setopt (Curl, curlopt_progressdata, progress);
		res = curl_easy_perform (curl);
		if (res!= CURLE_OK) fprintf (stderr, Curl_easy_perform () failed:%s\n ", Curl_easy_strerror (res));
	Curl_easy_cleanup (curl);

	} curl_global_cleanup (); SETDLGITEMTEXTW (Idc_edit1, Utf8tounicode (rEtstr.getbuffer ()). C_STR ());
	} size_t ccurltest1dlg::showhtml (void * ptr, size_t size, size_t nmemb, void * stream) {int return_size = SIZE*NMEMB;
	cstringa* str = (cstringa*) stream;
	*str + + (char*) ptr;
	Sleep (500);
return return_size; size_t Ccurltest1dlg::p rogress_callback (void *clientp, double dltotal, double Dlnow, double ultotal, double ulnow) {CP
	rogressctrl* progress = (cprogressctrl*) clientp;
	Progress->setpos (Dlnow *100/dltotal);
return false; }























Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.