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++)
	TCHAR buf[20000] = {0};
	DWORD byteWritten = 0;
	_tcscat(mainbuf, (const char*) buf);
		if(strlen(mainbuf) < 30)
        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(""), 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));

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!

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JoeD77Author Commented:
Please ignore the "GET" request, I was only using it for debug purposes. Consider that as "POST"
Well, one thing is that you're merrily mixing ANSI and UNICODE, e.g.

_tcscat(mainbuf, (const char*) buf);


char *pch;
  pch = strtok (mainbuf,"\n"); // 'mainbuf' is TCHAR, isn't it?

which can easily result in truncated or empty strings. I'd try to get that into a consitant state first.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JoeD77Author Commented:
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?

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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.