gpt4 book ai didi

C++ char array unicode cyrillic symbols 不正确

转载 作者:行者123 更新时间:2023-11-30 04:06:14 24 4
gpt4 key购买 nike

我在 c++ 中有一个函数可以请求 java servlet。它有参数 char* 函数写入响应的地方。这个参数中的符号是可以的,西里尔字母除外。示例:Рванов Рван(事实上,它必须是:Иванов Иван)

但是当我将此参数写入文件时,西里尔符号也可以。示例代码:

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

这是我的函数代码

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;
}

地点:pszPostData - 发布请求正文servletUrl - servlet 的路径,不包括协议(protocol)、IP 地址和端口resultBuffer 必须由服务器响应写入outputFile - 日志文件

最佳答案

当您将文本数据发布到网络服务器时(实际上,当您处理文本数据时根本),您需要知道正在使用什么字符编码。这里,char *pszPostData 只是指向一些字节。我们知道它们是文本,但它可以用 ASCII 编码。或 UTF-8。或者完全不同的东西。

在网络上,application/x-www-form-urlencoded 的默认字符编码是 ISO-8859-1 , 或“latin1”,它不能对西里尔字母进行编码。 (字符编码将字节映射到字符;在 latin1 的情况下,它不会将任何字节映射到 Cryllic 字符,但它会将所有字节映射到某物,这意味着您的数据将通过,但不会解码为正确的字符。)

这是默认值。您可以通过以下方式指定特定的编码:

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

但是,您需要弄清楚这一点,因为我不清楚您的数据实际位于哪个字符集。(我不确定“Иванов Иван”如何被解码为“Рванов РРІР° РЅ")

关于C++ char array unicode cyrillic symbols 不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22978910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com