Suche HTTP Upload Klasse / Funktion [z.B. Curl]



  • Hi Leute,

    ich weiss es ist böse C und C++ zu mischen. Aber was solls 😃

    Zum Thema: ich suche eine Klasse oder Funktion die per HTTP Post eine Datei hochlädt. Also grob

    /**
    *@return the answer of the server
    **/
    char* uploadfile(const char* fileName, const char* uploadUrl);
    

    oder eine klasse:

    class uploader
    {
         uploader();
         char* uploadfile(const char* fileName, const char* uploadUrl);
    
    };
    

    Der Code muss auf Linux und Windows [am besten überall^^] kompilieren.

    Kennt jemand eine Bibliothek oder Files die soetwas können? Ich bin doch sicher nicht der erste der sowas braucht :). Wäre echt lieb wenn mir jemand einen Tipp geben könnte. Danke



  • Du hast die Lösung doch bereits im Titel genannt.
    Was spricht gegen libcurl? Das kann sowohl HTTP POST als auch HTTP PUT und auch FTP Upload...



  • Ich hab in php mit curl gearbeitet. Das war ziemlich einfach. Aber ich weiss nicht wie ich mit curl per POST (es muss POST sein) Dateien uploaden kann. Hast du vielleicht ein gutes Tutorial zur Hand?



  • Gib in deiner Shell "man libcurl-tutorial" ein oder guck dir die man page hier an: http://curl.haxx.se/libcurl/c/libcurl-tutorial.html

    Codebeispiele gibts auch: http://curl.haxx.se/libcurl/c/example.html

    Und beim Compilieren mit dem gcc/g++, die libary linking informationen nicht vergessen (curl-config --libs: -lcurl -lssl -lcrypto -lrt -lssl -lcrypto -ldl -lz -lz)
    Quasi so kompilieren: g++ prog.cpp -o prog -lcurl



  • Danke Leute :).

    EIne Frage noch:

    http://curl.haxx.se/lxr/source/docs/examples/postit2.c

    Ich brauche die Antwort des Servers. Wisst ihr wie ich die bekommen kann?

    Also quasi:

    myProgramm -> Upload (File) -> Server

    myProgramm <- Response (File Location, Success) <- Server

    Ich meine jetzt keine Header (die wären aber auch gut) sondern in dem Fall die HTML Antwort.



  • Habs hinbekommen, vielleicht hilft es ja wem :). Man muss das CURLOPT_WRITEFUNCTION FLag setzen und eine Funktion (Callback) angeben die die Daten verarbeitet. Bei mir Speichert die einfach die Daten in einen String. Für mehr Infos mal hier: http://curl.haxx.se/libcurl/c/libcurl-tutorial.html. Die Funktion muss so aussehen wie saveServerResponse (mit Parametern (Argumenten) und Rückgabetyp). size sagt wir gross ein buchstabe ist (1 Byte Ascii oder 2 Byte Unicode), nmemb wieviele es sind(size*nmemb sind die gesamt bytes im buffer).

    using namespace std;
    
    size_t saveServerResponse(void *buffer, size_t size, size_t nmemb, void *userp); 
    string siteData;
    
    //Use a callback function. This will write the data in a buffer and libcurl wont just put it to stdout
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, saveServerResponse); 
    
    size_t saveServerResponse(void *buffer, size_t size, size_t nmemb, void *userp)
    {
    	siteData.append((const char*)buffer, size*nmemb);
    	return(size*nmemb);
    }
    

    Hier die Wichtigen Zeilen von curl.haxx.se (muss man nicht selber suchen^^)

    transfer, I assume that you would like to get the data passed to you directly instead of simply getting it passed to stdout. So, you write your own function that matches this prototype:

    size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp);

    You tell libcurl to pass all data to this function by issuing a function similar to this:

    curl_easy_setopt(easyhandle, CURLOPT_WRITEFUNCTION, write_data);

    You can control what data your callback function gets in the fourth argument by setting another property:

    curl_easy_setopt(easyhandle, CURLOPT_WRITEDATA, &internal_struct);

    Using that property, you can easily pass local data between your application and the function that gets invoked by libcurl. libcurl itself won't touch the data you pass with CURLOPT_WRITEDATA.

    libcurl offers its own default internal callback that will take care of the data if you don't set the callback with CURLOPT_WRITEFUNCTION. It will then simply output the received data to stdout. You can have the default callback write the data to a different file handle by passing a 'FILE *' to a file opened for writing with the CURLOPT_WRITEDATA option.

    Now, we need to take a step back and have a deep breath. Here's one of those rare platform-dependent nitpicks. Did you spot it? On some platforms[2], libcurl won't be able to operate on files opened by the program. Thus, if you use the default callback and pass in an open file with CURLOPT_WRITEDATA, it will crash. You should therefore avoid this to make your program run fine virtually everywhere.

    (CURLOPT_WRITEDATA was formerly known as CURLOPT_FILE. Both names still work and do the same thing).

    If you're using libcurl as a win32 DLL, you MUST use the CURLOPT_WRITEFUNCTION if you set CURLOPT_WRITEDATA - or you will experience crashes.

    There are of course many more options you can set, and we'll get back to a few of them later. Let's instead continue to the actual transfer:

    success = curl_easy_perform(easyhandle);

    curl_easy_perform(3) will connect to the remote site, do the necessary commands and receive the transfer. Whenever it receives data, it calls the callback function we previously set. The function may get one byte at a time, or it may get many kilobytes at once. libcurl delivers as much as possible as often as possible. Your callback function should return the number of bytes it "took care of". If that is not the exact same amount of bytes that was passed to it, libcurl will abort the operation and return with an error code.

    When the transfer is complete, the function returns a return code that informs you if it succeeded in its mission or not. If a return code isn't enough for you, you can use the CURLOPT_ERRORBUFFER to point libcurl to a buffer of yours where it'll store a human readable error message as well.

    If you then want to transfer another file, the handle is ready to be used again. Mind you, it is even preferred that you re-use an existing handle if you intend to make another transfer. libcurl will then attempt to re-use the previous connection.

    For some protocols, downloading a file can involve a complicated process of logging in, setting the transfer mode, changing the current directory and finally transferring the file data. libcurl takes care of all that complication for you. Given simply the URL to a file, libcurl will take care of all the details needed to get the file moved from one machine to another.



  • gnunase schrieb:

    Gib in deiner Shell "man libcurl-tutorial" ein oder guck dir die man page hier an: http://curl.haxx.se/libcurl/c/libcurl-tutorial.html

    Wir haben hier auch man-Tags: man: libcurl-tutorial


Anmelden zum Antworten