Question

FIXED: Thanks Igor Fix is at bottom.

I want to use VS 2008 to send intents to Android. Could not be a simpler request, I thought. Openssl is crashing though and it is curious as to why. Let's start with the code:

#include <stdio.h>
#include <conio.h>
#include <curl/curl.h>
#include <string>
#include <iostream>
using namespace std;
#pragma comment( lib, "ws2_32.lib" )

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

   curl_global_init( CURL_GLOBAL_DEFAULT );

   curl = curl_easy_init();
   if(curl) 
   {
      string website = "https://android.googleapis.com/gcm/send";
      const char* headers [] = { "Content-Type: application/json", "Authorization:key=..."   };
      string json = "{ 'registration_ids':['...']}";//, 'data':'none'}";

      curl_easy_setopt( curl, CURLOPT_URL, website.c_str() );
      curl_easy_setopt( curl, CURLOPT_HTTPHEADER, headers );
      //curl_easy_setopt( curl, CURLOPT_POSTFIELDS, json.c_str() ); << not currently needed
      curl_easy_setopt( curl, CURLOPT_SSL_VERIFYPEER, FALSE ); // --insecure or -k

      try
      {
         res = curl_easy_perform( curl );  << crashes here!
      }
      catch( ... ) {}

The rest of the code is really unimportant and this code is boilerplate. The crash point is at curl_easy_perform. The error is:

Unhandled exception at 0x61524ffb in HTTPS_sender01.exe: 0xC0000005: 
Access violation         reading location 0x68747541.

So this crashes deep in the curllib.dll and appears to be a call to openssl, but I can't be sure. However, if I comment out the option CURLOPT_SSL_VERIFYPEER, then this runs fine but the android server rejects me because of no certificate.

Downloading source has gotten me almost nowhere since that requires openssl and who knows where to download the correct version of that (I have found around 30 sites... all with different versions including GunWin32 and I have so many download folders now, I have no idea what to do). I also don't know which versions of libs to use for libcurl for vs 2008. After several days of looking at this, I thought that I'd ask for guidance or help.

Any thoughts on https for android from visual studio. BTW, I will need to port this to linux eventually, so any advice there might be helpful too.

libcurl is downloaded from here: http://www.dll-files.com. the includes and source are from 7.19.3. This lib came with a version of openssl... I copied the instructions from here... http://quantcorner.wordpress.com/2012/04/08/using-libcurl-with-visual-c-2010/

Also, I did try using the openssl cert from the libcurl... the opensource one, and Android.google rejected that:

curl_easy_setopt( curl, CURLOPT_CAPATH, "C:/projects/libraries/libcurl/curl-ca-bundle.crt");

FIX:

  struct curl_slist *slist=NULL;
  int numHeaders = sizeof( headers) / sizeof(headers[0]);

  for( int i=0; i<numHeaders; i++ )
  {
     slist = curl_slist_append( slist, headers[i] );
  }
  curl_easy_setopt( curl, CURLOPT_HTTPHEADER, slist );
Était-ce utile?

La solution

CURLOPT_HTTPHEADER option does not accept an array of char* pointers as you seem to believe, but curl_slist* pointer you obtained from curl_slist_append.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top