Formular an Webseite senden (per POST C++)



  • Wie ist das realisierbar?

    Also ich möchte mich einloggen und dann eine Nachricht senden. Ich bekomme bei HttpRequester (Firefox AddOn) immer 401 Unauthorized als Antwort vom Server.

    MfG skullyan


    Anmelden zum Antworten
     


  • skullyan schrieb:

    Ich bekomme bei HttpRequester (Firefox AddOn) immer 401 Unauthorized als Antwort vom Server.

    401 - hört sich eher nach Basic Authentication als nach Form an.



  • Stimmt, die Authentication funktioniert auch schon. Aber wie kann ich danach eine Form senden?

    Starting...*

    [...]

    <
    * Connection #0 to host www.***.de left intact

    Danach will ich halt ein Formular senden. Wie geht das mit Curl?

    CURL *curl;
    	CURLcode res;
    
    	struct curl_slist *headers = NULL;
    
    	///* In windows, this will init the winsock stuff */
    	//curl_global_init(CURL_GLOBAL_ALL);
    
    	/* get a curl handle */
    	curl = curl_easy_init();
    	if (curl) {
    		/* First set the URL that is about to receive our POST. This URL can
    		just as well be a https:// URL if that is what should receive the
    		data. */
    		curl_easy_setopt(curl, CURLOPT_URL, "https://www.blabla.html");
    
    		//headers = curl_slist_append(headers, "Accept: application/x-www-form-urlencoded");
    		headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
    		curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    
    		/* Now specify the POST data */
    		curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "user pw");
    		curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen("user pw"));
    		curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
    		curl_easy_setopt(curl, CURLOPT_HEADER, 1);
    
    		/* Perform the request, res will get the return code */	
    		res = curl_easy_perform(curl);
    		cin.get();
    		/* Check for errors */
    		if (res != CURLE_OK)
    			fprintf(stderr, "curl_easy_perform() failed: %s\n",
    				curl_easy_strerror(res));
    		//cin.get();
    		/* always cleanup */
    		curl_easy_cleanup(curl);
    	}
    

    So sieht mein Code aus. Er geht ja auch. Aber wie geht es danach weiter zum senden des Formulars?



  • Zeige mal das Formular von <form> bis einschließlich </form>.



  • https://curl.haxx.se/libcurl/c/http-post.html

    /* Now specify the POST data /
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name
    *=daniel&project=curl**");

    Du gibst anscheinend keine Werte für die Variablen an.

    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen("user pw"));
    

    Scheint mir überflüssig und so auf jeden Fall falsch.



  • Ja stimmt. Jetzt funktioniert es, danke.


Anmelden zum Antworten