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" /> <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