Pergunta

How could I replicate such function in C++ using the BlackBerry Native Library?

$username="admin";
$password="admin";
$url="http://www.yourdomain.com/";
$cookie="cookie.txt";

$postdata = "log=". $username ."&pwd=". $password ."&wp-submit=Log%20In&redirect_to=".    
$url ."wp-admin/&testcookie=1";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url . "wp-login.php");
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url . "wp-admin/");
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
curl_close($ch);
echo $result;
exit;

A Request in the BlackBerry Native Library would usually look like the following as an example :

QNetworkAccessManager* networkAccessManager = new QNetworkAccessManager(this);

const QString queryUri = QString::fromLatin1("http://website.com/get.php?email=%1").arg(email);

QNetworkRequest request(queryUri);

QNetworkReply* reply = networkAccessManager->get(request);

Any information or a point in the correct direction would be amazing!

Foi útil?

Solução

libcurl is available on BB10, and it is already in the sdk. Don't forget to add LIBS += -lcurl to your .pro file

There's a lot of examples on their site. Save for some options that should translate nicely from your PHP code, it should almost look like that.

#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
    /* example.com is redirected, so we tell libcurl to follow redirection */ 
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

    /* Perform the request, res will get the return code */ 
    res = curl_easy_perform(curl);
    /* Check for errors */ 
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

    /* always cleanup */ 
    curl_easy_cleanup(curl);
  }
  return 0;
}

A few items:

  • Curl expects raw data. QStrings hides all the low level stuff. Expect pain when converting data to char*. Do test with întèrnâtioñal data.
  • This is C, do your homework and understand what is thread friendly and what is not, understand who is responsible for cleaning stuff.
  • QNetworkAccessManager and his friends use a completely different API that is asynchronous. Expect incredibly more pain that in making libcurl work. There are some gains though:
    • Those APIs implements a queue, so you can start an unlimited (like, thousands) number of tasks and Qt/BB10 will throttle requests so you don’t loose connectivity, group request by hosts to save some overhead, …
    • Those APIs are mobile-aware: want transfer if 3G or better, no transfer on cell? They can do that.
    • These are native Qt API. Work a while with libcurl, you’ll understand.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top