繁体   English   中英

发送或接收时是否出错? 未发送完整消息或消息未正确解码

[英]Is the error upon Sending or Receiving? Full message not sent or message not decoded properly

因此,我正在将一些数据从C ++桌面应用程序发布到服务器(PHP脚本)。

服务器未收到所有过帐数据。 您认为错误发生在哪里? 在服务器端解码(UTF-8)还是在客户端传输时?

C ++代码:请注意其Unicode。 如果我发送ASCII脚本将接收/解码整个post数据字符串:

static TCHAR hdrs[] =
    _T("Content-Type: application/x-www-form-urlencoded; charset=UTF-8\0\0");
static TCHAR frmdata[] =
    _T("name=John+Doe&auth=abc\0\0");  // use 2 null chars just incase
static LPSTR accept[2] = { "*/*", NULL };


HINTERNET hSession = InternetOpen(_T("MyAgent"),
    INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
// error checking removed but none of these fail

HINTERNET hConnect = InternetConnect(hSession, _T("mydomain.com"),
    INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);

HINTERNET hRequest = HttpOpenRequest(hConnect, _T("POST"),
    _T("upload.php"), NULL, NULL, (LPCWSTR*)&accept, INTERNET_FLAG_NO_CACHE_WRITE, 1);


HttpSendRequest(hRequest, hdrs, _tcslen(hdrs), frmdata, _tcslen(frmdata));
// The above function returns true and I query the response code and its HTTP 200 ok so sending is working

简单的PHP脚本:

$data = file_get_contents("php://input");
file_put_contents("post.txt", $data);  // outputs "name=John+D" so its missing text

// To make things even more confusing
echo mb_detect_encoding($data); // outputs ASCII!!!???

奇怪的是,如果我以ASCII格式发送脚本,则会接收/解码整个帖子数据

static char hdrs[] =
    _T("Content-Type: application/x-www-form-urlencoded; charset=UTF-8\0\0");
static char frmdata[] =
    _T("name=John+Doe&auth=abc\0\0"); 
static LPCSTR accept[2] = { "*/*", NULL };

...

HttpSendRequestA(hRequest, hdrs, strlen(hdrs), frmdata, strlen(frmdata));
// The above function returns true and I query the response code and its HTTP 200 ok so sending is working

使用ASCII post.txt包含name=John+Doe&auth=abc 那么错误将在哪里发生? 是不是发送了整个帖子字符串,还是PHP脚本未正确处理unicode?

您不会发送所有字符。 您还指定了错误的编码。

wchar_t *s1 = L"abc"; 不是UTF-8编码的char *s2 = "abc"; 碰巧是UTF-8编码的(这是UTF-8的不错的属性),但是使用此符号时,您只能使用拉丁字符。 请参见下面的示例。

_tcslen(frmdata)返回字符数,而不是字节数。 如果定义Unicode,则字符串比字符占用更多的字节。 您的服务器需要UTF-8字节序列,但实际编码不是UTF-8。

很少有关于如何在C ++ 11中指定文字字符串编码的示例

// Greek small letter tau
char const *tau8 = u8"\u03C4"; // UTF-8
char16_t tau16 = u'\u03C4';    // UTF-16
wchar_t tau32 = U'\U000003C4'; // UTF-32

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM