473,403 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Manage web form with libcurl

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
2 6682
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michael T. Peterson | last post by:
Hi, What are the differences, if any, between these two libraries? The curl website doesn't mention php_curl.dll and php websites often use libcurl and php_curl interchangeably. Thanks, ...
4
by: Jeff Shannon | last post by:
I'm looking to script a routine file-upload through an HTTP server (which I don't have access to). I've got specs on the CGI app that receives this upload (i.e. field names and expected contents)....
2
by: max | last post by:
Hi all, I have a problem that maybe has no solution, but before giving up, I was wondering if somebody has an idea.... I have a form with a subform (continous) that shows the related records; the...
0
by: loris128 | last post by:
A port of the famous libCURL for the .net platform http://sourceforge.net/projects/libcurl-net/ Has anyone used this? Is it good? >From a search in the newsgroops it seems like noone has...
0
by: Yandos | last post by:
Hello all, I'm sorry for a bit off-topic post, but curl does not have own newsgroup, so I hope someone might help me here... I need to feed form like the following using libcurl: <form...
0
by: mahesh anasuri | last post by:
Hi all, I am new to this mailing list. Thankful if any one is using curl/linux version to and worked on Https. I have created certificates (PEM format) for client and server using openSSL. I...
1
by: Sylvain/11XX | last post by:
Dear all, In order to retrieve and parse a XML document over internet (through http) I use libcurl and expat in my C program. For some long item I have 2 callback, that's ok for the moment....
2
by: borucik | last post by:
I am trying to use libcurl with Visual C++ 2005 Express Edition. I downloaded the file from here: http://curl.haxx.se/dlwiz/?type=lib&os=Win32&flav=-&ver=2000%2FXP It is the 7.16.0 version of the...
3
by: Jake | last post by:
Hi, I am trying to make an application in C which must establish a telnet session with a remote server, execute some scripts on the remote server and close down the telnet session. I have...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.