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

问题描述 投票:-1回答:1

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

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;

}
visual-c++ windows-mobile windows-mobile-6.5
1个回答
0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.