我在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的路径,不包括协议,IP地址和端口 resultBuffer必须由服务器响应写入 outputFile - 日志文件
答案 0 :(得分:2)
当您将文本数据发布到网络服务器时(实际上,当您使用文本数据完全时),您需要了解正在使用的字符编码。这里,char *pszPostData
只指向一些字节。我们知道它们是文本,但它可以用ASCII编码。或UTF-8。或者完全不同的东西。
在网络上,application/x-www-form-urlencoded
的默认字符编码为ISO-8859-1或“latin1”,无法对Cyrillic进行编码。 (字符编码将字节映射到字符;在latin1的情况下,它不会将任何字节映射到Cryllic字符,但它会将所有字节映射到某些,这意味着您的数据将通过,但不会解码为正确的字符。)
这是默认值。您可以通过以下方式指定特定编码:
application/x-www-form-urlencoded; charset=<your data's character encoding>
然而,你需要弄明白这一点,因为我不清楚你的数据实际上是什么字符集。(我不确定“ИвановИван”如何解码为“РвановРРІР°” РЅ“)
答案 1 :(得分:0)
示例中的文本编码为UTF8并被视为CP1251。
请考虑阅读Character encoding