Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 用于连接在线REST服务器的REST API_C++_Windows_Rest_Post_Mfc - Fatal编程技术网

C++ 用于连接在线REST服务器的REST API

C++ 用于连接在线REST服务器的REST API,c++,windows,rest,post,mfc,C++,Windows,Rest,Post,Mfc,我正在尝试编写一个RESTAPI来连接在线REST服务器。不幸的是,我无法连接到服务器,结果收到一条错误消息,上面写着“方法不允许”。我已经使用web浏览器中的RESTAPI插件在在线REST服务器上测试了POST方法,该方法被接受,测试成功,但我不理解我的应用程序失败的原因。我想知道是否有人能很好地引导我走上正确的方向。多谢各位 CInternetSession session(_T("MySession")); CHttpConnection* pServer = NULL;

我正在尝试编写一个RESTAPI来连接在线REST服务器。不幸的是,我无法连接到服务器,结果收到一条错误消息,上面写着“方法不允许”。我已经使用web浏览器中的RESTAPI插件在在线REST服务器上测试了POST方法,该方法被接受,测试成功,但我不理解我的应用程序失败的原因。我想知道是否有人能很好地引导我走上正确的方向。多谢各位

    CInternetSession session(_T("MySession"));
    CHttpConnection* pServer = NULL;
    CHttpFile* pFile = NULL;
    char *szBuff = new char[500];
    CString strServerName = _T("rest.cleverreach.com");
    CString headers = _T("Content-Type: application/x-www-form-urlencoded\r\n");
    headers += _T("Host: rest.cleverreach.com\r\n");
    headers += _T("Method: POST\r\n");
    headers += _T("Pragma: no-cache\r\n");
    headers += _T("Keep-Alive: true\r\n");
    headers += _T("Accept-Language: en-US\r\n");
    CString szHeaders = headers;
    DWORD dwRet;
    CString strObject = _T("/v2/login.json");
    INTERNET_PORT nPort = INTERNET_DEFAULT_HTTP_PORT;
    CString parameters;
    parameters = "client_id=123456&login=test@test.de&password=blahblahblah";
    try
    {
        pServer = session.GetHttpConnection(strServerName, nPort, NULL, NULL);
        pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject);
        pFile->AddRequestHeaders(szHeaders);
        pFile->SendRequest(szHeaders, szHeaders.GetLength(), &parameters, parameters.GetLength());
        pFile->QueryInfoStatusCode(dwRet);
        pFile->Read(szBuff, 500);
        CString tempSzBuff = CString(szBuff);
        _tprintf_s(tempSzBuff);     
    }
    catch (CInternetException *e)
    {
        TCHAR sz[1024];
        e->GetErrorMessage(sz, 1024);
        _tprintf_s ((_T("ERROR!  %s\n"), sz));
        e->Delete();
    }
结果:

{"error":{"code":405,"message":"Method Not Allowed"}
SendRequest
中的第三个参数是
LPVOID
,它要求字符通常为ASCII或UTF8格式。不要传递
&参数的地址
。您可以使用
CString::GetBuffer

在ANSI中,使用

pFile->SendRequest(szHeaders, szHeaders.GetLength(), 
    parameters.GetBuffer(), parameters.GetLength());
parameters.ReleaseBuffer();
如果定义了
UNICODE
,则转换为UTF8

CStringA copy = CW2A(parameters, CP_UTF8);
pFile->SendRequest(szHeaders, szHeaders.GetLength(), 
    copy.GetBuffer(), parameters.GetLength());
copy.ReleaseBuffer();
pFile
也需要清理


另请参见

非常感谢您的回复。正如您所建议的,我将参数转换为UTF-8,但仍然收到相同的错误消息<代码>CStringA paramtersUTF8=CW2A(参数,CP_UTF8);pFile->SendRequest(szHeaders,szHeaders.GetLength(),parametrSutf8.GetBuffer(),parametrSutf8.GetLength());参数SUTF8.ReleaseBuffer()I还将strServerName、szHeaders和strObject转换为UTF-8,但仍然存在相同的错误
strServerName
和`szHeaders`不应转换为UTF8。看起来你的代码是ANSI,在这种情况下,不应该有任何UTF8@Barmak Shemirani谢谢你的回复,好吧,即使这样它仍然无法修复错误。该网站是安全的,你可能需要OAuth授权,在这种情况下,你没有走上正确的轨道。
CStringA copy = CW2A(parameters, CP_UTF8);
pFile->SendRequest(szHeaders, szHeaders.GetLength(), 
    copy.GetBuffer(), parameters.GetLength());
copy.ReleaseBuffer();