Link to home
Start Free TrialLog in
Avatar of JoeD77
JoeD77

asked on

POSTing information from file - Strange behavior

Hello. Here's my code:
TCHAR mainbuf[50000] = {0};
	int i;
	TCHAR *tempdirw[] =  {_T("log0.log"), _T("log1.log"), _T("log2.log")};
	for(i = 0; i <= 2; i++)
	{
	HANDLE hFile = CreateFile((const char*)tempdirw[i],GENERIC_WRITE | GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
	TCHAR buf[20000] = {0};
	DWORD byteWritten = 0;
	ReadFile(hFile,buf,sizeof(buf),&byteWritten,NULL);
	CloseHandle(hFile);
	_tcscat(mainbuf, (const char*) buf);
	}
		if(strlen(mainbuf) < 30)
		{
			ExitThread(0);
		}
		Sleep(1000);
        static TCHAR hdrs[] =
      _T("Content-Type: application/x-www-form-urlencoded");
	 TCHAR frmdata[5000] = {0};
  static LPSTR accept[2]={"*/*", NULL};
     HINTERNET hSession;

   HINTERNET hConnect;
   BOOL SendRequest;

	  char *pch;
  pch = strtok (mainbuf,"\n");
  HINTERNET hRequest;
  while (pch != NULL)
  {
    pch = strtok (NULL, _T("~"));

	  sprintf(frmdata, _T("index.php?data=%s&action=add"), pch);
	  hSession = InternetOpen(_T("Mozilla/5.0"),INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
	  hConnect = InternetConnect(hSession, _T("www.mysite.com"), INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
	  hRequest = HttpOpenRequest(hConnect, _T("GET"), frmdata, _T("HTTP/1.0"), NULL, (const char**)accept, 0, 1);
	  SendRequest = HttpSendRequest(hRequest, hdrs, strlen(hdrs), NULL, strlen(frmdata));
   InternetCloseHandle(hRequest);
   InternetCloseHandle(hConnect);
   InternetCloseHandle(hSession);
  }

Open in new window


I've been testing this code and its demonstrating strange behavior. Sometimes it won't POST any data at all, even though the files are occupied with the required data. Other times it will only post 1 piece of data, even though there's much more posting to do... I thought reopening/closing each handle would solve this problem but it's still giving me problems. I even tried to implement unicode for maximum compatibility but its still having the same problems.

So basically my question is:
do you see anything wrong with this code that would cause:
1. Not sending of the data
2. Sending only 1 piece of data even though theres more to send

I notice when it doesn't send the data, or just sends 1 piece of data, if I run it a SECOND time, it will work perfectly. So maybe it's a cache problem? Very confusing!

Thanks
Avatar of JoeD77
JoeD77

ASKER

Please ignore the "GET" request, I was only using it for debug purposes. Consider that as "POST"
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JoeD77

ASKER

thanks for pointing that out. I fixed it up now.

do you think I should be URLencoding the URL and the subsequent data which is held under the "data?=" parameter?

perhaps not encoding that is causing some lack of consistency?

Thanks
Well, if the URL isn't properly encoded, you will run into problems, yet that should be easy to verify by the server's logs.