使用MFC的HTTP封装库,与服务器进行连接。服务器没问题。
执行httpsendrequest失败,GetLastError 返回2(系统找不到指定文件)
代码如下:
int CHttpClient::ExecuteRequest(LPCTSTR strMethod, LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse)
{
CString strServer;
CString strObject;
DWORD dwServiceType;
INTERNET_PORT nPort;
strResponse = "";
LogString1.Empty();
LogString2.Empty();
CString StrTmp("");
BOOL l_BRet = FALSE;
DWORD l_ErrorCode = 0x00000000;
LogString1.Format("AfxParseURL");
AfxParseURL(strUrl, dwServiceType, strServer, strObject, nPort);
if(AFX_INET_SERVICE_HTTP != dwServiceType && AFX_INET_SERVICE_HTTPS != dwServiceType)
{
LogString1.Format("dwServiceType:%lu",dwServiceType);
return FAILURE;
}
try
{
m_pConnection = m_pSession->GetHttpConnection(strServer,
dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_CONNECT : SECURE_CONNECT,
nPort);
m_pFile = m_pConnection->OpenRequest(strMethod, strObject,
NULL, 1, NULL, NULL,
(dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_REQUEST : SECURE_REQUEST));
DWORD dwFlags;
l_BRet = m_pFile->QueryOption(INTERNET_OPTION_SECURITY_FLAGS, dwFlags);
if(l_BRet == FALSE)
{
l_ErrorCode = GetLastError();
LogString1.Format("QueryOption FAIL,GetLastError:%lu\n",l_ErrorCode);
}
dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
l_BRet = m_pFile->SetOption(INTERNET_OPTION_SECURITY_FLAGS, dwFlags);
if(l_BRet == FALSE)
{
l_ErrorCode = GetLastError();
StrTmp.Format("SetOption FAIL,GetLastError:%lu\n",l_ErrorCode);
LogString1 = LogString1 + StrTmp;
}
CString szHeaders = L"Accept: audio/x-aiff, audio/basic, audio/midi,\
audio/mpeg, audio/wav, image/jpeg, image/gif, image/jpg, image/png,\
image/mng, image/bmp, text/plain, text/html, text/htm\r\n";
m_pFile->AddRequestHeaders("Accept: application/json");
m_pFile->AddRequestHeaders("Content-Type: application/json;charset=UTF-8");
l_BRet = m_pFile->SendRequest(NULL, 0, (LPVOID)(LPCTSTR)strPostData, strPostData == NULL ? 0 : _tcslen(strPostData));
if(l_BRet == FALSE)
{
l_ErrorCode = GetLastError();
StrTmp.Format("HttpSendRequest FAIL,GetLastError:%lu\n",l_ErrorCode);
}
DWORD dwRet;
m_pFile->QueryInfoStatusCode(dwRet);
if(l_BRet == FALSE)
{
l_ErrorCode = GetLastError();
StrTmp.Format("QueryInfoStatusCode FAIL,GetLastError:%lu\n",l_ErrorCode);
LogString1 = LogString1 + StrTmp;
}
if(dwRet != HTTP_STATUS_OK)
{
CString errText;
errText.Format("POST出错,错误码:%d", dwRet);
AfxMessageBox(errText);
}
char szChars[BUFFER_SIZE + 1] = {0};
string strRawResponse = "";
UINT nReaded = 0;
while ((nReaded = m_pFile->Read((void*)szChars, BUFFER_SIZE)) > 0)
{
szChars[nReaded] = '\0';
strRawResponse += szChars;
memset(szChars, 0, BUFFER_SIZE + 1);
}
int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, strRawResponse.c_str(), -1, NULL, 0);
WCHAR *pUnicode = new WCHAR[unicodeLen + 1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8,0,strRawResponse.c_str(),-1, pUnicode,unicodeLen);
CString cs(pUnicode);
delete []pUnicode;
pUnicode = NULL;
strResponse = cs;
Clear();
}
catch (CInternetException* e)
{
char szErr[1025] = {0};
e->GetErrorMessage(szErr, 1024);
LogString2 = szErr;
Clear();
DWORD dwErrorCode = e->m_dwError;
e->Delete();
DWORD dwError = GetLastError();
if (ERROR_INTERNET_TIMEOUT == dwErrorCode)
{
StrTmp.Format("\nHttp TimeOut,ErroCoder:%lu",dwErrorCode);
LogString2 += StrTmp;
return OUTTIME;
}
else
{
StrTmp.Format("\nHttp FAIL,ErroCoder:%lu",dwErrorCode);
LogString2 += StrTmp;
return FAILURE;
}
}
return SUCCESS;
}
我找了找了一下,实际是httpsendrequest调用失败了。
我不明白的地方是,系统找不到指定文件指的是哪个文件?
这个文件是在本机,还是在服务器上?
先谢过各位大神的指教了。