I'm trying to fetch contents of an URL on a WM 6.5 not so uber gizmo, using VC++ 2008
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;
}
I do see the request coming in but the URI does not get passed, the server throws a 500 or 404 because of this.
I've tried passing params as "GET /blah.txt HTTP/1.0" and also "/blah.txt", no luck, would like to keep testing various input parameters but the device hangs for some reason...
Any pointers would be greatly appreciated!
TIA
Later edit: Solution:
Here's how I got it to work, full code snippet in case anyone wants a copy paste solution (yeah these come in handy)
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(),¶ms, 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;
}
your strObject is empty. Shouldn't this be where you put "blah.txt"?
also, you should delete[] szBuf;
and you're calling pServer->Close() after you've deleted pServer which will probably cause your app to except.