Domanda

I have function in c++ that requests to java servlets. It has parameter char* where function writes response. The symbols in this parameter are ok, except cyrillic ones. Example: Рванов Рван (in fact, it has to be: Иванов Иван)

But when I write this parameter to file, cyrillic symbols are OK as well. Example code:

ofstream outputFile;
outputFile.open("log.txt");
printf("Respond from servlet: %s", parameter);

Here is code of my function

int sendPostRequest(char *pszPostData, LPCTSTR servletUrl, char* resultBuffer, ofstream &outputFile) {

    outputFile << "============== SENDING REEQUEST ====================" << endl;

    HINTERNET hSession = WinHttpOpen(
        userAgent,
        WINHTTP_ACCESS_TYPE_NO_PROXY,
        WINHTTP_NO_PROXY_NAME,
        WINHTTP_NO_PROXY_BYPASS,
        0);
    if (!hSession)
    {
        _tprintf(_TEXT("Failed to open WinHTTP session: %ld\n"), GetLastError());
        outputFile << "Failed to open WinHTTP session: %ld\n" << GetLastError() << endl;
        return NULL;
    }
    else {
        _tprintf(_TEXT("Oppening WinHTTP session successful: %ld\n"), GetLastError());
        outputFile << "Oppening WinHTTP session successful: %ld\n" << GetLastError() << endl;
    }

    HINTERNET hConnect = WinHttpConnect(
        hSession,
        serverIP,
        serverPort,
        0);
    if (!hConnect)
    {
        _tprintf(_TEXT("Failed to connect to server: %ld\n"), GetLastError());
        outputFile << "Failed to connect to server: %ld\n" << GetLastError() << endl;
        WinHttpCloseHandle(hSession);
        return NULL;
    }
    else {
        _tprintf(_TEXT("Connection to server successful: %ld\n"), GetLastError());
        outputFile << "Connection to server successful: %ld\n" << GetLastError() << endl;
    }

    _tprintf(_TEXT("Post data : %ld\n"), pszPostData);
    outputFile << "Post data : %ld\n" << pszPostData << endl;
    DWORD dwDataLen = strlen(pszPostData);

    HINTERNET hRequest = WinHttpOpenRequest(
        hConnect,
        _TEXT("POST"),
        servletUrl,
        NULL,
        WINHTTP_NO_REFERER,
        WINHTTP_DEFAULT_ACCEPT_TYPES,
        WINHTTP_FLAG_REFRESH);
    if (!hRequest)
    {
        _tprintf(_TEXT("Failed to open request: %ld\n"), GetLastError());
        outputFile << "Failed to open request: %ld\n" << GetLastError() << endl;
        WinHttpCloseHandle(hConnect);
        WinHttpCloseHandle(hSession);
        return -1;
    }
    else {
        _tprintf(_TEXT("Opening request successful: %ld\n"), GetLastError());
        outputFile << "Opening request successful: %ld\n" << GetLastError() << endl;
    }

    DWORD dwReqOpts = 0;
    DWORD dwSize = sizeof(DWORD);
    WinHttpSetOption(
        hRequest,
        WINHTTP_OPTION_SECURITY_FLAGS,
        &dwReqOpts,
        sizeof(DWORD));

    BOOL done = false;
    BOOL rc = WinHttpSendRequest(
        hRequest,
        contentTypeHeader,
        -1,
        (LPVOID)pszPostData,
        dwDataLen,
        dwDataLen,
        NULL);

    if (rc) {
        rc = WinHttpReceiveResponse(hRequest, NULL);
        _tprintf(_TEXT("Sending request successful: %ld\n"), GetLastError());
        outputFile << "Sending request successful: %ld\n" << GetLastError() << endl;
    }
    else
    {
        _tprintf(_TEXT("Send request failed: %ld\n"), GetLastError());
        outputFile << "Send request failed: %ld\n" << GetLastError() << endl;
        WinHttpCloseHandle(hRequest);
        WinHttpCloseHandle(hConnect);
        WinHttpCloseHandle(hSession);
        return -1;
    }

    // Get the status from the server
    DWORD dwCode = 0;
    if (rc)
    {
        rc = WinHttpQueryHeaders(
            hRequest,
            WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
            NULL,
            &dwCode,
            &dwSize,
            NULL);
    }

    if (dwCode != HTTP_STATUS_OK)
    {
        _tprintf(_TEXT("HTTP Request failed: %ld\n"), dwCode);
        outputFile << "HTTP Request failed: %ld\n" << dwCode << endl;
        WinHttpCloseHandle(hRequest);
        WinHttpCloseHandle(hConnect);
        WinHttpCloseHandle(hSession);
    }
    else
    {
        _tprintf(_TEXT("HTTP Request is ok: %ld\n"), dwCode);
        outputFile << "HTTP Request is ok : %ld\n" << dwCode << endl;

        // Keep reading from the remote server until there's nothing left to read
        DWORD dwBytesToBeRead = 0, dwBytesRead = 0;
        //char szBuffer[8192] = { 0 };
        //strcpy(resultBuffer, "");
        do
        {
            if (!WinHttpQueryDataAvailable(hRequest, &dwBytesToBeRead))
            {
                _tprintf(_TEXT("No data available from server? %ld"), GetLastError());
                outputFile << "No data available from server? %ld" << GetLastError() << endl;

                WinHttpCloseHandle(hRequest);
                WinHttpCloseHandle(hConnect);
                WinHttpCloseHandle(hSession);
                return -1;
            }

            if (!WinHttpReadData(
                hRequest,

                //szBuffer,
                resultBuffer,

                //sizeof(szBuffer),
                RESULT_BUFFER_SIZE,

                &dwBytesRead))
            {
                _tprintf(_TEXT("Failed to read data from server: %ld"), GetLastError());
                outputFile << "Failed to read data from server: %ld" << GetLastError() << endl;

                WinHttpCloseHandle(hRequest);
                WinHttpCloseHandle(hConnect);
                WinHttpCloseHandle(hSession);
                return -1;
            }
            if (dwBytesRead > 0)
            {
                //szBuffer[dwBytesRead] = 0;
                resultBuffer[dwBytesRead] = 0; // NULL-terminated returned buffer
            }
        } while (dwBytesRead > 0);
        WinHttpCloseHandle(hRequest);
        WinHttpCloseHandle(hConnect);
        WinHttpCloseHandle(hSession);
        return 0;
    }
    return -1;
}

Where: pszPostData - post request body servletUrl - path to servlet, excluding protocol, ip address and port resultBuffer have to be written by server response outputFile - log-file

È stato utile?

Soluzione

When you POST text data to a webserver (actually, when you work with text data at all) you need to be aware of what character encoding is in use. Here, char *pszPostData just points to some bytes. We know they're text, but it could be encoded in ASCII. or UTF-8. Or something entirely different.

On the web, the default character encoding for application/x-www-form-urlencoded is ISO-8859-1, or "latin1", which cannot encode Cyrillic. (A character encoding maps bytes to characters; in latin1's case, it doesn't map any bytes to Cryllic characters, but it does map all the bytes to something, which means your data will get through, but won't decode to the right characters.)

That's the default. You can specify a specific encoding by saying:

application/x-www-form-urlencoded; charset=<your data's character encoding>

You'll need to figure this out, however, as it's not clear to me what character set your data is actually in. (I'm not sure how "Иванов Иван" might get decoded as "Рванов Рван")

Altri suggerimenti

The text in your example is encoded as UTF8 and viewed as CP1251.

Please consider reading about Character encoding

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top