Moth
asked on
problem retrieving a file on second attempt?
I've got a function that connects to url containing text, then reads each line into an array and returns that array.
The text file located at the url changes every so often. My problem is that the function doesn't re-connect to the server again when the function is called a second time.
Here's the code,
CStringArray *CAllUserDlg::GetAllUserSt atus()
{
CStringArray *pArray = new CStringArray;
sUrl = "www.somedomain.net/~index.html?user=getStatus";
int iLen = sUrl.GetLength();
char *buffer = new char[1000];//iLen];
DWORD dwBuffLen = 1000;
int iVal =InternetCanonicalizeUrl(s Url, buffer, &dwBuffLen, ICU_ENCODE_SPACES_ONLY );
if (iVal == ERROR_BAD_PATHNAME ) {
AfxMessageBox("Error: bad pathname in URL");
return pArray;
}
else if (iVal == ERROR_INSUFFICIENT_BUFFER ) {
AfxMessageBox("Error: Insufficient buffer");
return pArray;
}
else if (iVal == ERROR_INTERNET_INVALID_URL ) {
AfxMessageBox("Error: Internet invalid url");
return pArray;
}
else if (iVal == ERROR_INVALID_PARAMETER ) {
AfxMessageBox("Error: Invalid parameter");
return pArray;
}
CString strTmp = buffer;
CInternetSession Connection;
CHttpConnection* pHttpConnection = NULL;
CHttpFile* pFile = NULL;
delete [] buffer;
try {
pHttpConnection = Connection.GetHttpConnecti on(strTmp) ;
pFile = pHttpConnection->OpenReque st(CHttpCo nnection:: HTTP_VERB_ GET,"/");
BOOL b = pFile->SendRequest();
DWORD dwLen = pFile->GetLength();
CString s;
BOOL bDone = FALSE;
while (!bDone) {
if (pFile->ReadString(s))
{
int j = s.Find("userEOF");
if (j >= 0) {
bDone = TRUE;
}
else {
pArray->Add(s);
}
}
else {
// can't read the file anymore
bDone = TRUE;
}
}
pFile->Flush();
pFile->Close();
delete pFile;
pHttpConnection->Close();
Connection.Close();
delete pHttpConnection;
delete Connection;
}
catch (CInternetException* pEx)
{
AfxMessageBox("There was a problem sending your status");
pEx->ReportError();
pEx->Delete();
}
return pArray;
}
Can anyone give me a pointer as to where I'm going wrong? Is it that I'm not deleting something correctly?
Thanks,
Moth
The text file located at the url changes every so often. My problem is that the function doesn't re-connect to the server again when the function is called a second time.
Here's the code,
CStringArray *CAllUserDlg::GetAllUserSt
{
CStringArray *pArray = new CStringArray;
sUrl = "www.somedomain.net/~index.html?user=getStatus";
int iLen = sUrl.GetLength();
char *buffer = new char[1000];//iLen];
DWORD dwBuffLen = 1000;
int iVal =InternetCanonicalizeUrl(s
if (iVal == ERROR_BAD_PATHNAME ) {
AfxMessageBox("Error: bad pathname in URL");
return pArray;
}
else if (iVal == ERROR_INSUFFICIENT_BUFFER ) {
AfxMessageBox("Error: Insufficient buffer");
return pArray;
}
else if (iVal == ERROR_INTERNET_INVALID_URL
AfxMessageBox("Error: Internet invalid url");
return pArray;
}
else if (iVal == ERROR_INVALID_PARAMETER ) {
AfxMessageBox("Error: Invalid parameter");
return pArray;
}
CString strTmp = buffer;
CInternetSession Connection;
CHttpConnection* pHttpConnection = NULL;
CHttpFile* pFile = NULL;
delete [] buffer;
try {
pHttpConnection = Connection.GetHttpConnecti
pFile = pHttpConnection->OpenReque
BOOL b = pFile->SendRequest();
DWORD dwLen = pFile->GetLength();
CString s;
BOOL bDone = FALSE;
while (!bDone) {
if (pFile->ReadString(s))
{
int j = s.Find("userEOF");
if (j >= 0) {
bDone = TRUE;
}
else {
pArray->Add(s);
}
}
else {
// can't read the file anymore
bDone = TRUE;
}
}
pFile->Flush();
pFile->Close();
delete pFile;
pHttpConnection->Close();
Connection.Close();
delete pHttpConnection;
delete Connection;
}
catch (CInternetException* pEx)
{
AfxMessageBox("There was a problem sending your status");
pEx->ReportError();
pEx->Delete();
}
return pArray;
}
Can anyone give me a pointer as to where I'm going wrong? Is it that I'm not deleting something correctly?
Thanks,
Moth
Hoe can you delete Connection if it is not a pointer?
Does this thing compile "as is"?
Does this thing compile "as is"?
ASKER
I started off only calling delete on the buffer, but then as I couldn't find a solution, I called delete on everything.
This code compiles fine on my system (visual c 6). You also see I've called delete on the pFile variable just in case the compilet had called 'new' somewhere without me knowing....
I also call delete on the array that I pass out of this function - after I've done stuff to it of cource ;)
But, yes, this does compile ok.
Moth
This code compiles fine on my system (visual c 6). You also see I've called delete on the pFile variable just in case the compilet had called 'new' somewhere without me knowing....
I also call delete on the array that I pass out of this function - after I've done stuff to it of cource ;)
But, yes, this does compile ok.
Moth
CInternetSession Connection; <<-This is not a pointer, therefor, the following line is incorrect:
delete Connection;
You cannot delete a stack variable from the heap!
----
The compiler doesn' allocate a thing. On the other hand, MFC as a software package you are using, might allocate variables. If you are suopposed to delete something that MFC doesn't then the documandation says that, otherwise both MFC code and your own will attemt to free the same memory block. This can be fatal.
Before you use pFile, check if it is valide (not NULL).
I never used the functions you used, but I can tell that you should program more cerfully.
Regards.
delete Connection;
You cannot delete a stack variable from the heap!
----
The compiler doesn' allocate a thing. On the other hand, MFC as a software package you are using, might allocate variables. If you are suopposed to delete something that MFC doesn't then the documandation says that, otherwise both MFC code and your own will attemt to free the same memory block. This can be fatal.
Before you use pFile, check if it is valide (not NULL).
I never used the functions you used, but I can tell that you should program more cerfully.
Regards.
ASKER
Adjusted points from 150 to 200
ASKER
Right,
I've removed the lines "delete Connection" and "delete pFile" (because I never call 'new' on pFile. I still get the problem of retrieving the file ok first-time round, but it doesn't download the second time.
I've got access to the servers access and error logs. The first connection appears fine in the urls access log, but nothing appears in either the error or access logs. This is why I thought it might be the connection not terminating correctly - fo I called delete on it.
The GetAllUserStatus() function is called in the OnInitDialog() function.
I press a button in a parent window and it creates an instance of the userstatus dialog, in turn calling the GetAllUserStatus() function. I need to close this dialog to get back to the parect window (where I can press the button again). Should I have some asserts in the onClose() function in the child window (even though I'm only using local variables in the http connection functions)?
I've removed the lines "delete Connection" and "delete pFile" (because I never call 'new' on pFile. I still get the problem of retrieving the file ok first-time round, but it doesn't download the second time.
I've got access to the servers access and error logs. The first connection appears fine in the urls access log, but nothing appears in either the error or access logs. This is why I thought it might be the connection not terminating correctly - fo I called delete on it.
The GetAllUserStatus() function is called in the OnInitDialog() function.
I press a button in a parent window and it creates an instance of the userstatus dialog, in turn calling the GetAllUserStatus() function. I need to close this dialog to get back to the parect window (where I can press the button again). Should I have some asserts in the onClose() function in the child window (even though I'm only using local variables in the http connection functions)?
> delete pFile" (because I never call 'new' on pFile
unfortunately you have to delete that one. :) or else you will get a leak
other than that your code works fine for me - I tried against a local server page and it downloaded correctly everytime.
unfortunately you have to delete that one. :) or else you will get a leak
other than that your code works fine for me - I tried against a local server page and it downloaded correctly everytime.
ASKER
Hmmm,
I've replaced the 'delete pFile' as suggested, but it still doesnt work..
Does the local server page change ever, or is it static? The page I'm trying to download changes, so I'm trying to get the latest version of the page. At the moment I'm only retrieving the first version...
If getting the latest version works for you, maybee my code is going pearshaped somewhere else... That or maybee there's an internet options setting of using cached pages somewhere that I have set on my system that maybee you don't on yours?
I've replaced the 'delete pFile' as suggested, but it still doesnt work..
Does the local server page change ever, or is it static? The page I'm trying to download changes, so I'm trying to get the latest version of the page. At the moment I'm only retrieving the first version...
If getting the latest version works for you, maybee my code is going pearshaped somewhere else... That or maybee there's an internet options setting of using cached pages somewhere that I have set on my system that maybee you don't on yours?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I've just added a string version of the current time, and that seems to work a treat.
Thanks!
Thanks!
yes - as long as you don't have 2 requests in the same second but you can control that so no probs there
the method I gave should work if you have more than 1 request in the same second as the random number should change with each query
anyway - glad to help
the method I gave should work if you have more than 1 request in the same second as the random number should change with each query
anyway - glad to help
ASKER