views:

210

answers:

3

I am sending HTTP POST to SSL server using wininet apis. I want to wrap these apis to C# using P/Invoke because original code in C#.How do i wrap these apis to c# . I tried however it wasn't worked. I am doing this one for windows mobile and smatphones.

LPCTSTR lpszAgent = _T("CeHttp");
DWORD dwError; 
DWORD sizeInResult, sizeOutResult, sizeToWrite, sizeWritten,dwRead;
HINTERNET hInternet=NULL; 
HINTERNET hConnect=NULL;
HINTERNET hRequest=NULL;
LPDWORD pSizeInResult = &sizeInResult;

LPDWORD pSizeOutResult = &sizeOutResult;
LPDWORD pSizeToWrite = &sizeToWrite;
LPDWORD pSizeWritten = &sizeWritten;

 hInternet=InternetOpen(lpszAgent,INTERNET_OPEN_TYPE_PRECONFIG,NULL,INTERNET_INVALID_PORT_NUMBER,0);
    if(!hInternet)
 {

  dwError = GetLastError();

 }

 LPCTSTR lpszServerName = _T("mundusync");
 INTERNET_PORT nServerPort =444;
 LPCTSTR lpszUserName = L"";
 LPCTSTR lpszPassword = L"";
 DWORD dwConnectFlags =  0;
 DWORD dwConnectContext = 0;
 DWORD dwCode = NULL;
 DWORD dwSize = 8000;
 char charBuffer[1000];
 TCHAR szBuffer[1000];


 int read = 0;
 char* str = "*/*", buff[1024] = {};



 hConnect = InternetConnect(hInternet,L"s-syncml.geodesic.net",
           nServerPort,
           L"",
           L"",
           INTERNET_SERVICE_HTTP,
           0,
           0);




  if(! hConnect)
  {


   dwError = GetLastError();

  }


 LPCWSTR lpszVerb = _T("POST");
 LPCWSTR lpszObject = L"/";
 LPCWSTR lpszVersion = NULL;
 LPCWSTR lpszReferer = NULL;
 LPCWSTR *lplpszAcceptTypes = NULL;

 DWORD dwFlags = INTERNET_FLAG_SECURE |
           INTERNET_FLAG_IGNORE_CERT_CN_INVALID|
          INTERNET_FLAG_IGNORE_CERT_DATE_INVALID;

 DWORD dwOpenRequestContext = 0;

 hRequest=HttpOpenRequest(hConnect,L"POST",L"/",HTTP_VERSION,L"",lplpszAcceptTypes,dwFlags,dwOpenRequestContext);


 if(!hRequest )

 {

  dwError = GetLastError();

 }




 char lpPostData[637] ="<SyncML> some post data </SyncML>";

 LPCWSTR lpszHeaders =_T("Content-Type: application/vnd.xyz+xml");
 BOOL bResult;
 if(!HttpSendRequest(hRequest,lpszHeaders,wcslen(lpszHeaders),lpPostData,strlen(lpPostData)))
 {

  dwError = GetLastError();

 }

 if (!(InternetQueryDataAvailable (hRequest, pSizeInResult, 0, 0)))
    {
      //wsprintf (dwError, TEXT("%s: %x"), TEXT("InternetQueryDataAvailableError"), GetLastError());
      return;
      //WriteFile (hFile, szErrMsg, sizeToWrite, pSizeWritten, NULL);
      //goto error;
    }

if (sizeInResult >= 1024)
    {
      int num = sizeInResult / 1024;

      do
      {
        //InternetReadFile (hRequest, lpBuffer, 1024, pSizeOutResult);
          // WriteFile (hFile, lpBuffer, sizeOutResult, pSizeWritten, NULL);
      }
      while (--num > 0);
    }
    else
    {
      //InternetReadFile (hRequest, lpBuffer, sizeInResult, pSizeOutResult);
        // WriteFile (hFile, lpBuffer, sizeOutResult, pSizeWritten, NULL);

     do
       {
       // read from Internet HTTP server
       if(InternetReadFile(hRequest, charBuffer,
          1000, &dwRead))
       {
          // convert to Unicode and display
       charBuffer[dwRead] = '\0';
       mbstowcs(szBuffer, charBuffer, dwRead); 
       //cout « szBuffer;
       szBuffer[dwRead] = '\0';
       }


       } while(dwRead > 0);

    }

 if(!hInternet)
 InternetCloseHandle(hInternet);
 if(!hConnect)
 InternetCloseHandle(hConnect);
 if(!hRequest)
 InternetCloseHandle(hRequest);
+1  A: 

You should check the P/Invoke website. They list all the P/Invoke definitions for well-known DLLs and "wininet" in particular. You can also find sample code.

Laurent Etiemble
A: 

You can read this article about p/Invoke in the CF.

Shaihi
+1  A: 

If you want to reuse your native code you could expose a function that takes the server name and port as input parameters and that returns the data read. This native function could then be invoked in C#:

[DllImport("yourlib.dll")]
static extern string FetchData(string host, int port);

Another option is to use .NET built-in classes to achieve similar functionality:

var request = (HttpWebRequest)WebRequest.Create("http://s-syncml.geodesic.net:444");
request.UserAgent = "CeHttp";
var response = request.GetResponse();
using (var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
    string result = reader.ReadToEnd();
}
Darin Dimitrov