试图制作CHttpFile :: SendRequest,设备挂起

时间:2013-11-18 16:57:57

标签: visual-c++ windows-mobile windows-mobile-6.5

我正在尝试使用VC ++ 2008获取WM 6.5上的URL内容,而不是如此超级小发明

CString http_request(CString params){

CInternetSession session(_T("My Session")); // add device identifier here?
CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL;
CString szHeaders( _T("Content-Type: application/x-www-form-urlencoded;Accept: text/xml, text/plain, text/html, text/htm\r\nHost: www.mydomain.com\r\n\r\n"));
CString strObject(""); 
CString out;
DWORD dwRet;

char *szBuff = new char[1023];

try
{

    CString strServerName("www.mydomain.com");
   INTERNET_PORT nPort(80);

   pServer = session.GetHttpConnection(strServerName, nPort);
   pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject);
   pFile->AddRequestHeaders(szHeaders);
   pFile->SendRequest(LPCTSTR(params),params.GetLength());
   pFile->QueryInfoStatusCode(dwRet);

   if (dwRet == HTTP_STATUS_OK)
   {
       UINT nRead = pFile->Read(szBuff, 1023);
       while (nRead > 0)
       {
           //read file...
           out = CString(szBuff);
       }
   }
   delete pFile;
   delete pServer;
}
catch (CInternetException* pEx)
{
   //catch errors from WinInet
out = CString("Something went wrong.");
}
session.Close();

return out;

}

我确实看到请求进入但是URI没有通过,因此服务器抛出500或404。

我已经尝试将params作为“GET /blah.txt HTTP / 1.0”以及“/blah.txt”传递,没有运气,想继续测试各种输入参数但设备因某些原因而挂起...

任何指针都将非常感谢!

TIA

稍后修改:解决方案:

以下是我如何使用它,完整的代码片段,以防任何人想要一个复制粘贴解决方案(是的,这些都派上用场)

CString http_request(CString server, CString uri){

CInternetSession session(_T("My Session")); // add device identifier here? IMEI ftw
CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL;
CString szHeaders( _T("Content-Type: application/x-www-form-urlencoded;Accept: text/xml, text/plain, text/html, text/htm\r\nHost: www.domain.com\r\n\r\n")); // maybe pass as param?
CString strObject(uri); 
CString out;
DWORD dwRet;
CString params;

char *szBuff = new char[1023];

try
{

    CString strServerName(server);
   INTERNET_PORT nPort(80);

   pServer = session.GetHttpConnection(strServerName, nPort);
   pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject);
   pFile->AddRequestHeaders(szHeaders);

   pFile->SendRequest(szHeaders, szHeaders.GetLength(),&params, params.GetLength());
   pFile->QueryInfoStatusCode(dwRet);

   if (dwRet == HTTP_STATUS_OK)
   {
       UINT nRead = pFile->Read(szBuff, 1023);
       //while (nRead > 0)
       //{
           //read, for more data you might want to uncomment the while and append to a var. I only needed a few bytes actually.

           out = CString(szBuff);
       //}
   } else {
        out = CString("Communication error!");
   }
   delete pFile;
   delete pServer;
}
catch (CInternetException* pEx)
{
    //catch errors from WinInet

    out = CString("Network error!"); // stupid a$$ cpp the code to handle this would be longer than my ****
}
session.Close();
pServer->Close();

return out;

}

1 个答案:

答案 0 :(得分:0)

你的strObject是空的。这不应该是你放“blah.txt”的地方吗?

另外,你应该删除[] szBuf;

并且在删除pServer后调用pServer-> Close(),这可能会导致您的应用除外。