libcurl-tutorial.html
来自「专门介绍网络爬虫设计的论文,写得非常好啊.希望大家好好学习」· HTML 代码 · 共 567 行 · 第 1/5 页
HTML
567 行
<p class="level0"> curl_easy_perform(easyhandle); /* post away! */ <p class="level0">Simple enough, huh? Since you set the POST options with the CURLOPT_POSTFIELDS, this automatically switches the handle to use POST in the upcoming request. <p class="level0">Ok, so what if you want to post binary data that also requires you to set the Content-Type: header of the post? Well, binary posts prevents libcurl from being able to do strlen() on the data to figure out the size, so therefore we must tell libcurl the size of the post data. Setting headers in libcurl requests are done in a generic way, by building a list of our own headers and then passing that list to libcurl. <p class="level0"><pre><p class="level0"> struct curl_slist *headers=NULL; headers = curl_slist_append(headers, "Content-Type: text/xml"); <p class="level0"> /* post binary data */ curl_easy_setopt(easyhandle, CURLOPT_POSTFIELDS, binaryptr); <p class="level0"> /* set the size of the postfields data */ curl_easy_setopt(easyhandle, CURLOPT_POSTFIELDSIZE, 23); <p class="level0"> /* pass our list of custom made headers */ curl_easy_setopt(easyhandle, CURLOPT_HTTPHEADER, headers); <p class="level0"> curl_easy_perform(easyhandle); /* post away! */ <p class="level0"> curl_slist_free_all(headers); /* free the header list */ <p class="level0">While the simple examples above cover the majority of all cases where HTTP POST operations are required, they don't do multi-part formposts. Multi-part formposts were introduced as a better way to post (possibly large) binary data and was first documented in the RFC1867. They're called multi-part because they're built by a chain of parts, each being a single unit. Each part has its own name and contents. You can in fact create and post a multi-part formpost with the regular libcurl POST support described above, but that would require that you build a formpost yourself and provide to libcurl. To make that easier, libcurl provides <a class="emphasis" href="./curl_formadd.html">curl_formadd(3)</a>. Using this function, you add parts to the form. When you're done adding parts, you post the whole form. <p class="level0">The following example sets two simple text parts with plain textual contents, and then a file with binary contents and upload the whole thing. <p class="level0"><pre><p class="level0"> struct curl_httppost *post=NULL; struct curl_httppost *last=NULL; curl_formadd(&post, &last, CURLFORM_COPYNAME, "name", CURLFORM_COPYCONTENTS, "daniel", CURLFORM_END); curl_formadd(&post, &last, CURLFORM_COPYNAME, "project", CURLFORM_COPYCONTENTS, "curl", CURLFORM_END); curl_formadd(&post, &last, CURLFORM_COPYNAME, "logotype-image", CURLFORM_FILECONTENT, "curl.png", CURLFORM_END); <p class="level0"> /* Set the form info */ curl_easy_setopt(easyhandle, CURLOPT_HTTPPOST, post); <p class="level0"> curl_easy_perform(easyhandle); /* post away! */ <p class="level0"> /* free the post data again */ curl_formfree(post); <p class="level0">Multipart formposts are chains of parts using MIME-style separators and headers. It means that each one of these separate parts get a few headers set that describe the individual content-type, size etc. To enable your application to handicraft this formpost even more, libcurl allows you to supply your own set of custom headers to such an individual form part. You can of course supply headers to as many parts you like, but this little example will show how you set headers to one specific part when you add that to the post handle: <p class="level0"><pre><p class="level0"> struct curl_slist *headers=NULL; headers = curl_slist_append(headers, "Content-Type: text/xml"); <p class="level0"> curl_formadd(&post, &last, CURLFORM_COPYNAME, "logotype-image", CURLFORM_FILECONTENT, "curl.xml", CURLFORM_CONTENTHEADER, headers, CURLFORM_END); <p class="level0"> curl_easy_perform(easyhandle); /* post away! */ <p class="level0"> curl_formfree(post); /* free post */ curl_slist_free_all(post); /* free custom header list */ <p class="level0">Since all options on an easyhandle are "sticky", they remain the same until changed even if you do call <a class="emphasis" href="./curl_easy_perform.html">curl_easy_perform(3)</a>, you may need to tell curl to go back to a plain GET request if you intend to do such a one as your next request. You force an easyhandle to back to GET by using the CURLOPT_HTTPGET option: <p class="level0"> curl_easy_setopt(easyhandle, CURLOPT_HTTPGET, TRUE); <p class="level0">Just setting CURLOPT_POSTFIELDS to "" or NULL will *not* stop libcurl from doing a POST. It will just make it POST without any data to send! <p class="level0"></pre><a name="Showing"></a><h2 class="nroffsh">Showing Progress</h2><p class="level0"><p class="level0">For historical and traditional reasons, libcurl has a built-in progress meter that can be switched on and then makes it presents a progress meter in your terminal. <p class="level0">Switch on the progress meter by, oddly enough, set CURLOPT_NOPROGRESS to FALSE. This option is set to TRUE by default. <p class="level0">For most applications however, the built-in progress meter is useless and what instead is interesting is the ability to specify a progress callback. The function pointer you pass to libcurl will then be called on irregular intervals with information about the current transfer. <p class="level0">Set the progress callback by using CURLOPT_PROGRESSFUNCTION. And pass a pointer to a function that matches this prototype: <p class="level0"><pre><p class="level0"> int progress_callback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow); <p class="level0">If any of the input arguments is unknown, a 0 will be passed. The first argument, the 'clientp' is the pointer you pass to libcurl with CURLOPT_PROGRESSDATA. libcurl won't touch it. <p class="level0"></pre><a name="libcurl"></a><h2 class="nroffsh">libcurl with C++</h2><p class="level0"><p class="level0">There's basically only one thing to keep in mind when using C++ instead of C when interfacing libcurl: <p class="level0">The callbacks CANNOT be non-static class member functions <p class="level0">Example C++ code: <p class="level0"><pre><p class="level0">class AClass { static size_t write_data(void *ptr, size_t size, size_t nmemb, void *ourpointer) { /* do what you want with the data */ } } <p class="level0"></pre><a name="Proxies"></a><h2 class="nroffsh">Proxies</h2><p class="level0"><p class="level0">What "proxy" means according to Merriam-Webster: "a person authorized to act for another" but also "the agency, function, or office of a deputy who acts as a substitute for another". <p class="level0">Proxies are exceedingly common these days. Companies often only offer Internet access to employees through their proxies. Network clients or user-agents ask the proxy for documents, the proxy does the actual request and then it returns them. <p class="level0">libcurl supports SOCKS and HTTP proxies. When a given URL is wanted, libcurl will ask the proxy for it instead of trying to connect to the actual host identified in the URL. <p class="level0">If you're using a SOCKS proxy, you may find that libcurl doesn't quite support all operations through it.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?