By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,446 Members | 3,120 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,446 IT Pros & Developers. It's quick & easy.

Manage web form with libcurl

P: n/a
Hi everybody,
this is my first post in this group since it is from little time that I
have begun to learn c++.

My problem is to post a web form using the libcurl library.

The form is at this address: http://it.mobi.dada.net/lostpassword.php
end it isn't very complicated:

<form method="post" action="/cgi-bin/hotw/sendpassword.chm">
<input type="hidden" name="returnurl" value="" />
<input type="hidden" name="code_lc" value="it" />
<input type="hidden" name="prefix" value="+39" />
<input type="hidden" name="md5check"
value="76371dc18cd15bc5ad62f18fea23ece4" />
<input type="text" name="code" size="5" />&nbsp;&nbsp;<img
src="/data_captcha/76371dc18cd15bc5ad62f18fea23ece4.png" width="150"
height="50" style="vertical-align: top" />
<input type="text" name="cellulare" value="" id="numerocellulare" />
<input type="submit" name="Submit" class="Scarica" value="Invia"
id="accesso" />
<input name="back" type="button" class="Indietro" value="Indietro"
onclick="document.location.href=''" id="annulla" />
</form>

This form sends on a mobile phone a lost password; it contains two text
fields: in the first goes a number, a code, generated casually at each
access at the page and shown with an image (bot prevention), while in
the second text field goes the mobile phone number.

I was able to retrieve the value of the md5check input and, therefore,
the complete address of the image but, at this point, how show the
image? I'm a linux user and I had thought to use the system() function
of <cstdlibto open the image with xview but don't know how accomplish
this.

Here it is what i wrote:

#include<iostream>
#include<string>
#include<iomanip>
#include "curl/curl.h"
#include "curl/types.h"
#include "curl/easy.h"
#include<cstdlib>

using namespace std;

string outputStream;

void sendSMS();
char *chunk;

/* imaginary struct */
struct MemoryStruct {
char *memory;
size_t size;
};

/* imaginary callback function */
size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void
*data)
{
register int realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)data;

mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
if (mem->memory) {
memcpy(&(mem->memory[mem->size]), ptr, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
}
return realsize;
}

int main()
{
cout << "Sending sms...\n";
sendSMS();
cout << "Sms sent.\n";

return 0;
}

void sendSMS()
{
struct MemoryStruct chunk;

chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
chunk.size = 0;

CURL *easyHandle;
CURLcode curlCode;

curl_global_init(CURL_GLOBAL_ALL );

easyHandle = curl_easy_init();

curl_easy_setopt(easyHandle, CURLOPT_URL,
"http://it.mobi.dada.net/lostpassword.php");

curl_easy_setopt(easyHandle, CURLOPT_WRITEFUNCTION,
WriteMemoryCallback);

curl_easy_setopt(easyHandle, CURLOPT_WRITEDATA, (void *)&chunk);

curlCode = curl_easy_perform(easyHandle);

outputStream = chunk.memory;

string subStr = ".png";
int pos = outputStream.find(subStr);

string md5Check = "";

for(int i = pos - 32; i < pos; i++)
{
md5Check += outputStream[i];
}

string urlImage = "http://it.mobi.dada.net/data_captcha/" + md5Check +
".png";

/* ???????????????????????????????

Here the code to show the image

??????????????????????????????? */

string code;

cout << "\nInsert code: ";
cin >code;

string formInput =
"returnurl=&code_lc=it&prefix=+39&md5check=a6d701c 4fd5603bb1146f9ca1e60ecaf&code="
+ code + "&cellulare=3476875137&Submit=Invia&back=Indietro" ;

const char *data1 = formInput.c_str();

curl_easy_setopt(easyHandle, CURLOPT_POSTFIELDS, data1);

curl_easy_setopt(easyHandle, CURLOPT_URL,
"http://it.mobi.dada.net/cgi-bin/hotw/sendpassword.chm");

curlCode = curl_easy_perform(easyHandle);

curl_easy_cleanup(easyHandle);
curl_global_cleanup();
}

Because i'm just beginning to learn c++ perhaps there is a better way
to do the job; I am absolutely available to change approach; any
suggestion is very accepts.

Thanks in advance and sorry for my not perfect English (i'm italian),
Giuseppe

Nov 14 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi,
I solved the problem of viewing the image:

1) i added the function

size_t writeFunction( void *ptr, size_t size, size_t nmemb, void
*stream)
{
fwrite(ptr, size, nmemb, (FILE *)stream);
return nmemb*size;
}

to write the image stream in a file.

2) in the sendSMS() function added the following instructions (to
download the image):

curl_easy_setopt(easyHandle, CURLOPT_URL, urlImage.c_str());
curl_easy_setopt(easyHandle, CURLOPT_VERBOSE, 1);

// Open the file to write the copied file to
destFile = fopen("/tmp/code.png","w");

// Tell libcurl where to write the file
curl_easy_setopt(easyHandle,CURLOPT_WRITEFUNCTION, writeFunction);
curl_easy_setopt(easyHandle,CURLOPT_WRITEDATA,dest File);

curlCode = curl_easy_perform(easyHandle);

/* always cleanup */
fclose(destFile);

3) Call xview to show the downloaded image:

string command = "xview -quiet /tmp/code.png";
system(command.c_str());

But the sms is not sent :-(
In the console is written

Insert code: 25558
* Re-using existing connection! (#0) with host it.mobi.dada.net
* Connected to it.mobi.dada.net (195.110.126.144) port 80
POST /cgi-bin/hotw/sendpassword.chm HTTP/1.1
Host: it.mobi.dada.net
Accept: */*
Content-Length: 121
Content-Type: application/x-www-form-urlencoded

returnurl=&code_lc=it&prefix=%2B39&md5check=a6d701 c4fd5603bb1146f9ca1e60ecaf&code=25558&cellulare=34 76875137&Submit=Invia
< HTTP/1.1 302 Moved
< Date: Tue, 14 Nov 2006 15:11:15 GMT
< Server: Apache
< Location: http://it.mobi.dada.net/error.php?error=-9
< Content-Length: 0
< Content-Type: text/plain
* Connection #0 to host it.mobi.dada.net left intact
* Closing connection #0

i have searched in the site info about error -9 and it means "wrong
control code".
It could be a cookie problem?

Thanks,
Giuseppe

P.S.
The new complete code is:

#include<iostream>
#include<string>
#include<iomanip>
#include "curl/curl.h"
#include "curl/types.h"
#include "curl/easy.h"

using namespace std;

string outputStream;

void sendSMS();
char *chunk;

/* imaginary struct */
struct MemoryStruct {
char *memory;
size_t size;
};

size_t writeFunction( void *ptr, size_t size, size_t nmemb, void
*stream)
{
fwrite(ptr, size, nmemb, (FILE *)stream);
return nmemb*size;
}

/* imaginary callback function */
size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void
*data)
{
register int realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)data;

mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
if (mem->memory) {
memcpy(&(mem->memory[mem->size]), ptr, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
}
return realsize;
}

int main()
{
cout << "Sending sms...\n";
sendSMS();
cout << "Sms sent.\n";

return 0;
}

void sendSMS()
{
struct MemoryStruct chunk;

chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
chunk.size = 0;

CURL *easyHandle;
CURLcode curlCode;
FILE *destFile;

curl_global_init(CURL_GLOBAL_ALL );

easyHandle = curl_easy_init();

curl_easy_setopt(easyHandle, CURLOPT_URL,
"http://it.mobi.dada.net/lostpassword.php");

curl_easy_setopt(easyHandle, CURLOPT_WRITEFUNCTION,
WriteMemoryCallback);

curl_easy_setopt(easyHandle, CURLOPT_WRITEDATA, (void *)&chunk);

curlCode = curl_easy_perform(easyHandle);

outputStream = chunk.memory;

string subStr = ".png";
int pos = outputStream.find(subStr);

string md5Check = "";

for(int i = pos - 32; i < pos; i++)
{
md5Check += outputStream[i];
}

string urlImage = "http://it.mobi.dada.net/data_captcha/" + md5Check +
".png";

curl_easy_setopt(easyHandle, CURLOPT_URL, urlImage.c_str());
curl_easy_setopt(easyHandle, CURLOPT_VERBOSE, 1);

// Open the file to write the copied file to
destFile = fopen("/tmp/code.png","w");

// Tell libcurl where to write the file
curl_easy_setopt(easyHandle,CURLOPT_WRITEFUNCTION, writeFunction);
curl_easy_setopt(easyHandle,CURLOPT_WRITEDATA,dest File);

curlCode = curl_easy_perform(easyHandle);

/* always cleanup */
fclose(destFile);

string command = "xview -quiet /tmp/code.png";

system(command.c_str());

string code;

cout << "\nInsert code: ";
cin >code;

string formInput =
"returnurl=&code_lc=it&prefix=%2B39&md5check=a6d70 1c4fd5603bb1146f9ca1e60ecaf&code="
+ code + "&cellulare=3476875137&Submit=Invia";

const char *data1 = formInput.c_str();

curl_easy_setopt(easyHandle, CURLOPT_POSTFIELDS, data1);

curl_easy_setopt(easyHandle, CURLOPT_URL,
"http://it.mobi.dada.net/cgi-bin/hotw/sendpassword.chm");

curl_easy_cleanup(easyHandle);
curl_global_cleanup();
}

Nov 14 '06 #2

P: n/a
Solved! I missing a couple of rows in the previous code.

The final code is this:

#include<iostream>
#include<string>
#include<iomanip>
#include "curl/curl.h"
#include "curl/types.h"
#include "curl/easy.h"

using namespace std;

string outputStream;

void sendSMS();
char *chunk;

/* imaginary struct */
struct MemoryStruct {
char *memory;
size_t size;
};

size_t writeFunction( void *ptr, size_t size, size_t nmemb, void
*stream)
{
fwrite(ptr, size, nmemb, (FILE *)stream);
return nmemb*size;
}

/* imaginary callback function */
size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void
*data)
{
register int realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)data;

mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
if (mem->memory) {
memcpy(&(mem->memory[mem->size]), ptr, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
}
return realsize;
}

int main()
{
cout << "Sending message...\n";
sendSMS();
cout << "OK\n\n";

return 0;
}

void sendSMS()
{
struct MemoryStruct chunk;

chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
chunk.size = 0;

CURL *easyHandle;
CURLcode curlCode;
FILE *destFile;

curl_global_init(CURL_GLOBAL_ALL );

easyHandle = curl_easy_init();

curl_easy_setopt(easyHandle, CURLOPT_URL,
"http://it.mobi.dada.net/lostpassword.php");

curl_easy_setopt(easyHandle, CURLOPT_WRITEFUNCTION,
WriteMemoryCallback);

curl_easy_setopt(easyHandle, CURLOPT_WRITEDATA, (void *)&chunk);

curlCode = curl_easy_perform(easyHandle);

outputStream = chunk.memory;

string subStr = ".png";
int pos = outputStream.find(subStr);

string md5Check = "";

for(int i = pos - 32; i < pos; i++)
{
md5Check += outputStream[i];
}

string urlImage = "http://it.mobi.dada.net/data_captcha/" + md5Check +
".png";

curl_easy_setopt(easyHandle, CURLOPT_URL, urlImage.c_str());

// Open the file to write the copied file to
destFile = fopen("/tmp/code.png","w");

// Tell libcurl where to write the file
curl_easy_setopt(easyHandle,CURLOPT_WRITEFUNCTION, writeFunction);
curl_easy_setopt(easyHandle,CURLOPT_WRITEDATA,dest File);

curlCode = curl_easy_perform(easyHandle);

/* always cleanup */
fclose(destFile);

string command = "xview -quiet -zoom 200 /tmp/code.png";

system(command.c_str());

string code;

cout << "\nInsert code: ";
cin >code;

string formInput = "returnurl=&code_lc=it&prefix=+39&md5check=" +
md5Check +
"&code=" + code + "&cellulare=3476875137&Submit=Invia";

curl_easy_setopt(easyHandle, CURLOPT_POSTFIELDS, formInput.c_str());

curl_easy_setopt(easyHandle, CURLOPT_URL,
"http://it.mobi.dada.net/cgi-bin/hotw/sendpassword.chm");

curlCode = curl_easy_perform(easyHandle);

curl_easy_cleanup(easyHandle);
curl_global_cleanup();
}

Nov 14 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.