Manage web form with libcurl

G

Giuseppe

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 <cstdlib> to 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;
}

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=a6d701c4fd5603bb1146f9ca1e60ecaf&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
 
G

Giuseppe

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,destFile);

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=a6d701c4fd5603bb1146f9ca1e60ecaf&code=25558&cellulare=3476875137&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;
}

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,destFile);

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=a6d701c4fd5603bb1146f9ca1e60ecaf&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();
}
 
G

Giuseppe

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;
}

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,destFile);

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();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top