Re: WideCharToMultiByte, HttpSendRequestEx problem with Unicode

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



I'm pretty sure CP_UTF8 is supported on all PPC devices, though the
documentation seems to state otherwise. You can definitely convert to utf-8
using mlang.dll.

Andy.

"Manish" <Manish@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:AD29AFF4-9EA3-4E47-AFAC-C5545870CB5C@xxxxxxxxxxxxxxxx
> We are using Internet API's like HttpSendRequestEx etc. for a pocket PC
> based
> application. A C++ dll is created for internet access that uses Win APIs
> for
> Windows CE.
>
> There is an payload (XML format) we are sending in InternetWriteFile
> function as lpBuffer parameter.
> This payload is generated in Unicode and if we send it as it is, it is not
> recognized at the server and an error is returned.
>
> The header we send in HttpSendRequestEx is _T("Content-Type: text/xml;
> charset=utf-8\nSOAPAction: /")
>
> We found out that ANSI payload works properly. SO currently we are
> converting the payload to ANSI using CP_ACP in WideCharToMultiByte, since
> the
> ANSI payload is recognized at the server properly and the request is
> working.
>
> But in order to use the code globally, we need to convert the payload in
> utf-8 or so. But Windows CE does not support the CP_UTF7 and CP_UTF8
> values
> for the CodePage parameter in WideCharToMultiByte.
>
> How to solve this problem for Windows CE? Is there any way such that the
> payload can be passed as unicode and will be recognized at the server
> properly?
>
> Regards
> Hrushi
>
>
>
> P.S
> Following is the code we are using to do the whole process of send request
> and get response.
>
> first the two handles are opened in some initproxy function and then the
> ExecRequestRetResponse function is called to do the send receive request
> using these handles. The bsOptionalPayload parameter in the
> ExecRequestRetResponse function is the payload we are talking about above.
>
> g_hOpen = InternetOpen (TEXT("DscoreHttp"),
> g_dwAccessType,//INTERNET_OPEN_TYPE_PROXY, INTERNET_OPEN_TYPE_PRECONFIG
> g_pszPRoxyServer, g_pszPRoxyBypass, 0);
>
> if(!(g_hConnect = InternetConnect (g_hOpen,
> (LPCWSTR)g_szURL, //purl, //g_szURL, //_T("172.16.235.154"),
> g_nPort,
> NULL, NULL,
> INTERNET_SERVICE_HTTP,
> 0, 0)))
> {
> hr = E_FAIL; goto exit;
> }
>
>
>
> HRESULT ExecRequestRetResponse(TCHAR* bsOptionalPayload, BOOL*
> pbFailResponse, TCHAR** ppResponse)
> {
> FDEBUGLOG (_T("ExecRequestRetResponse... Enter"), 1);
> USES_CONVERSION;
>
> LOGDBG1("REQUEST : ", bsOptionalPayload);//Added for logs
> HRESULT hr = S_OK;
> ULONG g_ulTimeOut = 3*60*1000; // 3 minutes
>
> DWORD tosend = 0, remainingbytes = 0, curstartpos = 0;
> INTERNET_BUFFERS BufferIn;
> DWORD dwBytesWritten = 0;
> BOOL bRet = FALSE;
> CComBSTR response = _T("");
> char* lpBufferW = NULL;
> TCHAR *lpHeadersW = NULL;
> HINTERNET hRequest = NULL;
> DWORD dwSize = 0;
> DWORD dwPostSize = 0;
> LPSTR pOptionalPayload = NULL;
> DWORD dwRqstChunkSize = 10*1024; // 10 K. max 25 k was working ok on
> device
> // normal rqst is approx. 3 K.
>
> DSTRY
> {
> FDEBUGLOG(_T("Before WideCharToMultiByte"), 0);
>
> int wsize = _tcslen(bsOptionalPayload) + 2;
> pOptionalPayload = (LPSTR)malloc(wsize);
> ZeroMemory(pOptionalPayload, wsize);
> if(!(WideCharToMultiByte(CP_ACP, 0, bsOptionalPayload, wsize,
> pOptionalPayload, wsize, NULL, NULL)))
> {
> DWORD dw = GetLastError();
> hr = E_FAIL;
> goto exit;
> }
>
> dwPostSize = (DWORD)strlen(pOptionalPayload);
> FDEBUGLOG(_T("Before HttpOpenRequest"), 0);
> // Open an HTTP request handle.
> if (!(hRequest = HttpOpenRequest (g_hConnect,
> TEXT("POST"),
> (LPCWSTR)(pFile), //pFile,
> HTTP_VERSION,
> NULL,
> //NULL,
> (LPCTSTR*)AcceptTypes,
> INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE, 0)))
> {
> FDEBUGLOG (_T("HttpOpenRequest failed"),0);
> hr = E_FAIL; goto exit;
> }
>
> TCHAR header[256];
>
> _stprintf(header, _T("Content-Type: text/xml; charset=utf-8\nSOAPAction:
> /"));
>
> FDEBUGLOG(_T("Before InternetSetOption"), 0);
> if(!(InternetSetOption(hRequest, INTERNET_OPTION_SEND_TIMEOUT,
> (LPVOID)&g_ulTimeOut, sizeof(ULONG))))
> {
> FDEBUGLOG (_T("InternetSetOption failed for send timeout"),0);
> //hr = E_FAIL; goto exit; should not exit
> }
> else
> {
> FDEBUGLOG (_T("InternetSetOption passed"),0);
> }
> FDEBUGLOG(_T("Before InternetSetOption"), 0);
> if(!(InternetSetOption(hRequest, INTERNET_OPTION_RECEIVE_TIMEOUT,
> (LPVOID)&g_ulTimeOut, sizeof(ULONG))))
> {
> FDEBUGLOG (_T("InternetSetOption failed for send timeout"),0);
> //hr = E_FAIL; goto exit; should not exit
> }
> else
> {
> FDEBUGLOG (_T("InternetSetOption passed"),0);
> }
>
> BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS ); // Must be set or
> error
> will occur
> BufferIn.Next = NULL;
> BufferIn.lpcszHeader = header;
> BufferIn.dwHeadersLength = _tcslen(header);
> BufferIn.dwHeadersTotal = 0;
> BufferIn.lpvBuffer = NULL;
> BufferIn.dwBufferLength = 0;
> BufferIn.dwBufferTotal = dwPostSize; // This is the only member used
> other than dwStructSize
> BufferIn.dwOffsetLow = 0;
> BufferIn.dwOffsetHigh = 0;
>
> FDEBUGLOG(_T("Before HttpSendRequestEx"), 0);
> if(!HttpSendRequestEx( hRequest, &BufferIn, NULL, 0, 0))
> {
> LOGDBG("Error on HttpSendRequestEx ");//LOGDBG(printf( "Error on
> HttpSendRequestEx %d\n",GetLastError() ));
> //return FALSE;
> hr = E_FAIL; goto exit;
> }
> // Send a request to the HTTP server.
> bRet=TRUE;
> if(dwPostSize <= dwRqstChunkSize)
> {
> FDEBUGLOG(_T("Before InternetWriteFile"), 0);
> bRet=InternetWriteFile( hRequest, pOptionalPayload, dwPostSize,
> &dwBytesWritten);
> }
> else
> {
> while(true)
> {
> remainingbytes = dwPostSize - curstartpos;
> if(!remainingbytes)
> break;
> tosend = (remainingbytes < dwRqstChunkSize ? remainingbytes :
> dwRqstChunkSize);
> FDEBUGLOG(_T("Before InternetWriteFile"), 0);
> if(!(bRet=InternetWriteFile( hRequest, (void*)(pOptionalPayload +
> curstartpos), tosend, &dwBytesWritten)))
> {
> bRet = FALSE;
> break;
> }
> if(dwBytesWritten < tosend) // may be some error
> {
> bRet = FALSE;
> break;
> }
> curstartpos += tosend;
> }
> }
>
> if(!bRet)
> {
> LOGDBG(printf( "\nError on InternetWriteFile
> %lu\n",GetLastError() ));
> hr = E_FAIL;
> goto exit;
> }
>
> FDEBUGLOG(_T("Before HttpEndRequest"), 0);
> if(!HttpEndRequest(hRequest, NULL, 0, 0))
> {
> LOGDBG(printf( "Error on HttpEndRequest %lu \n", GetLastError()));
> hr = E_FAIL;
> goto exit;
> }
>
> // Call HttpQueryInfo to find out the size of the headers.
> FDEBUGLOG(_T("Before HttpQueryInfo"), 0);
> HttpQueryInfo (hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &dwSize,
> NULL);
>
> // Allocate a block of memory for lpHeadersA.
> lpHeadersW = new TCHAR[dwSize];
>
> // Call HttpQueryInfo again to get the headers.
> FDEBUGLOG(_T("Before HttpQueryInfo"), 0);
> if (!HttpQueryInfo (hRequest,
> HTTP_QUERY_RAW_HEADERS_CRLF,
> (LPVOID) lpHeadersW, &dwSize, NULL))
> {
> FDEBUGLOG (_T("HttpQueryInfo failed in getting headers"),0);
> hr = E_FAIL; goto exit;
> }
> else
> {
> FDEBUGLOG (_T("HttpQueryInfo passed"),0);
> }
>
> // Terminate headers with NULL.
> lpHeadersW [dwSize] = '\0';
>
> // Allocate a block of memory for lpBufferW.
> lpBufferW = new char[32000];
> ZeroMemory(lpBufferW, 32000);
>
> do
> {
> FDEBUGLOG(_T("Before InternetReadFile"), 0);
> if (!InternetReadFile (hRequest, (LPVOID)lpBufferW, 32000, &dwSize))
> {
> FDEBUGLOG (_T("InternetReadFile failed"),0);
> hr = E_FAIL; goto exit;
> }
>
> if (dwSize != 0)
> {
> // Terminate headers with NULL.
> lpBufferW [dwSize] = '\0';
>
> response += A2W(lpBufferW);
>
> }
> } while (dwSize);
>
> FDEBUGLOG (_T("After InternetReadFile loop"),0);
> // check whether the request went through. else don't go further.
> if(!(_tcsstr(lpHeadersW, _T("200 OK")))) // hrushi check this
> {
> FDEBUGLOG (_T("header is showing request failure. so exiting"),0);
> *pbFailResponse = TRUE;
> }
>
> *ppResponse = _tcsdup(response);
> LOGDBG1("response received in ExecRequestRetResponse is", *ppResponse);
>
> exit:
>
> // Close the Internet handles.
> if (hRequest)
> {
> FDEBUGLOG(_T("Before InternetCloseHandle"), 0);
> InternetCloseHandle (hRequest);
> }
>
> delete lpBufferW;
> delete lpHeadersW;
>
> }
> DSCATCH
> {
> FDEBUGLOG (_T("in catch of ExecRequestRetResponse"),0);
> }
> FDEBUGLOG (_T("ExecRequestRetResponse... Exit"), -1);
> DSFREE(pOptionalPayload);
> LOGDBG("after pOptionalPayload freed");
> return hr;
> }
>


.



Relevant Pages

  • Re: Windows CE, WideCharToMultiByte, HttpSendRequestEx prob with Unic
    ... > There is an payload we are sending in InternetWriteFile ... > Following is the code we are using to do the whole process of send request ... > HINTERNET hRequest = NULL; ... > // Call HttpQueryInfo to find out the size of the headers. ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: WideCharToMultiByte, HttpSendRequestEx problem with Unicode
    ... A C++ dll is created for internet access that uses Win APIs ... > There is an payload we are sending in InternetWriteFile ... > Following is the code we are using to do the whole process of send request ... > // Call HttpQueryInfo to find out the size of the headers. ...
    (microsoft.public.pocketpc.developer)
  • Re: CodeRed II Mutants - not
    ... CodeRed II Mutants - not ... The overall request is usually 3818 bytes, but this is 3379 bytes of payload ... I routinely find other headers too, ...
    (Incidents)
  • [PATCH, RFC] block: dont allocate a payload for discard request
    ... Allocating a fixed payload for discard requests always was a horrible hack, ... * Discard request come in as REQ_TYPE_FS but we turn them into ...
    (Linux-Kernel)
  • Re: Creating Outlook "Task" email without using OOM
    ... that the incoming email contains a Task (request) and process it accordingly ... the machine or the standalone server-side Extended MAPI to be there. ... without Outlook installed it would want to work with an Exchange mailbox. ... I am unsure on how to generate the OL Task payload. ...
    (microsoft.public.outlook.interop)