The use of Libcurl in QT

Source: Internet
Author: User
Tags ftp transfer

Recently in the spare time to do Qt, one of the functions is to implement FTP upload download.

Previous versions of QT provided an FTP-operated class, but 5.x (4.x?) After that, the performance problem is deprecated. Seemingly csdn on the Post asked this question, remember should be put how big file when the memory is too large. The official QT Manual now recommends the use of Qnetworkaccessmanager for TCP/IP and FTP transmissions. Honestly, this thing is not good for FTP compatibility. So I thought of curl.

There are many articles on curl and Libcurl on the web, but most of the articles used for FTP operations remain in the experimental code. In other words, ignoring a more important issue is the FTP transfer mode.

FTP has two modes of transmission, ASCII and binary, that is, type A and type I. There is no mention of this in any of our articles. I use libcurl primarily to transfer text files, so this problem comes up.

Libcurl download What do not say, to the official web to find the package on Windows, remember to download the develop version.

In QT using the dynamic library, the first thing to do is definitely to modify the pro file.

1 LIBS    + = C:\your-curl-lib-location\curl-7.34.  0-devel-mingw32\lib\libcurldll.a23 Includepath + C:\your-curl-lib-location\curl- 7.34. 0-devel-mingw32\include

Specify the static library location first, and then specify the include path. There's nothing to say about this. Then copy the DLLs in the bin directory under the Curl path to the Debug folder. Otherwise, the run will "inexplicably" stop.

Here is the easy excuse for curl (which seems to be the use of this article on the Internet).

Take a look at the download code first.

1 curlcode ret;2 Curl_global_init (curl_global_all);3curl* Curl =curl_easy_init ();4 5 curl_easy_setopt (Curl,curlopt_url,remotefile.tolatin1 (). data ());6Curl_easy_setopt (Curl,curlopt_userpwd,"Username:password"); 7RET =curl_easy_setopt (curl,curlopt_writedata,localfile);8RET =curl_easy_setopt (curl,curlopt_writefunction,curlwritefunction);9 TenCurl_easy_setopt (Curl,curlopt_transfertext,1); One  ACurl_easy_setopt (Curl,curlopt_verbose,1); -RET =curl_easy_perform (curl); -  the curl_easy_cleanup (curl); -Curl_global_cleanup ();

Explain the points.

1) Declare Curlcode, get the return value of the Curl interface.

2) use Curl_global_init to initialize curl. Passing the Curl_global_all in indicates that all platforms are initialized, which is the safest and ensures the portability of the code.

3) The CURL_EASY_SETOPT function sets the options for curl.

The first parameter of the function is the curl pointer, the second is the Curl option name (which is actually an integer), and the third is the value of the actual option, which can be a string, a pointer, a function pointer, and so on, with different values depending on the options.

Curlopt_url: Sets the URL of the file that needs to be uploaded/downloaded.

CURLOPT_USERPWD: Set the login server's account password, in the form of user:passwd.

Curlopt_writedata: A pointer to pass data. A quoted sentence is used, "curl does nothing with the pointer, only transfers." ”。 What's the use of this pointer, we'll look at it in a minute.

Curlopt_writefunction: Sets the function to write the file. When curl obtains data from the server, this function is called to write processing. The function prototypes are as follows:

size_t Write_callback (char *ptr, size_t size, size_t nmemb, void *userdata);

See the last parameter of the meter there? This parameter is the pointer specified in Curlopt_writedata. That is, if you want to specify several parameters that pass through the curl "get-write" process, send their pointers through curlopt_writedata to curl. Here I gave a qfile pointer in.

One point to note is that if you use curl only in C + +, this function should be declared static in the class.

        Curlopt_transfertext: Specifies the FTP transfer mode for curl. After the value is 1, the ascii,0 bit binary is transferred. If the wrong transfer mode is used, the server may give you a 553 during the download process. At first the mistake made me very puzzled. Because I used curlopt_prequote to append a command of type A to curl. Theoretically it should be possible, but in fact the file transfer is always half-closed by the server link. Guess this might be related to Curl's multithreading? Just make a guess.

Curlopt_verbose set the debug level.

On Libcurl's web site, there is a link to explain these macros specifically.

        

1 http://curl.haxx.se/libcurl/c/curl_easy_setopt.html

4) curl_easy_perform perform curl action. The default is download.

After the Curl setting is complete, you can write the previously specified write function.

     

1 intFtpthread::curlwritefunction (void*buffer, size_t size, size_t nmemb,void*stream) {2qfile* file = static_cast<qfile*>(stream);3     if(!file->IsOpen ()) {4File->open (Qfile::readwrite |qiodevice::text);5     }6     returnFile->write ((Char*) buffer,nmemb);7 8}

In Qt, this function is very easy to implement. Just make sure the return value is the actual write byte, because curl checks the return value, and if it is not equal to NMEMB, it will be an error.

The uploaded code is similar to the download and is directly on the code.

1 Curlcode ftpthread::curlupload () {2 curlcode ret;3 Curl_global_init (curl_global_all);4curl* Curl =curl_easy_init ();5qfile* LocalFile =NewQFile ("ftpcache/"+fileName);6Qdebug () <<localfile->filename () <<Endl;7QString RemoteFile ="ftp://your.ftp.server/"+FileName;8 9     if(!localfile->open (Qfile::readwrite |Qiodevice::text)) {TenQdebug () <<"Open File Fail"<<Endl; One         returnCurle_read_error; A     } -  - curl_easy_setopt (Curl,curlopt_url,remotefile.tolatin1 (). data ()); theCurl_easy_setopt (Curl,curlopt_userpwd,"Username:password"); -  -Curl_easy_setopt (Curl, Curlopt_upload,1L); - curl_easy_setopt (curl,curlopt_ftp_create_missing_dirs,curlftp_create_dir); +RET =curl_easy_setopt (curl,curlopt_readdata,localfile); -Curl_easy_setopt (curl,curlopt_infilesize_large,localfile->size ()); +RET =curl_easy_setopt (curl,curlopt_readfunction,curlreadfunction); A  at     //Timeout -Curl_easy_setopt (Curl,curlopt_ftp_response_timeout,60000); -  -  -     //Set transfer mode to ASCII -Curl_easy_setopt (Curl,curlopt_transfertext,1); inCurl_easy_setopt (Curl,curlopt_buffersize, $); -  to  +Curl_easy_setopt (Curl,curlopt_verbose,1); -RET =curl_easy_perform (curl); the     if(ret!=CURLE_OK) *Qdebug () <<"Perform fail"<<Endl; $ Panax Notoginseng curl_easy_cleanup (curl); - Curl_global_cleanup (); theLocalfile->close (); +     returnret; A}

Just a point, curl_easy_setopt (Curl, Curlopt_upload, 1L); Specifies that curl acts as an upload.

The function prototype for reading the file is as follows:

1 size_t read_callback (charvoid *instream);

The implementation is as follows:

1 int Ftpthread::curlreadfunction (voidvoid *stream) {2     qfile* file = static_cast <QFile*>(stream); 3     return File->read ((char*) buffer,size*nmemb); 4 }

Complete the full text.

The use of Libcurl in QT

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.