<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 6.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>send http request</title>
</head>

<body>



<p align="center"><span lang="en-ca"><font size="6" color="#FF0000"><b>Send Http 
Request</b></font></span></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A.<span lang="en-ca">First</span> Edition</font></b></pre>
</div>
<div align="left">
  <pre><b><font size="3">This is a small tool to send http request to a web server.</font></b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<p><span lang="en-ca"><b>Probably it is too common for many programmers. 
However, it is still a fresh try for me to send http request.</b></span></p>
<p><span lang="en-ca"><b>1. wininet is not fully implemented in win2k. (DC says 
it is not part of OS, instead wininet is just a part of internet explorer and I 
need to upgrade to ie6 sp2. However, I cannot find upgrade for sp2 for win2k.)</b></span></p>
<p><span lang="en-ca"><b>2. HttpSendRequest will fail and give error 12007 which 
means unresolved url. This is because you have to use either pure &quot;hostname&quot; or 
decimal format of ip address in previous call of InternetConnect. When I say 
&quot;pure&quot; host name, I mean you don't add &quot;theme string&quot; like &quot;http&quot; or &quot;ftp&quot; and 
you don't include relative path. Not even a slash &quot;/&quot; after your host name, i.e. 
&quot;<a href="http://www.google.com">www.google.com</a>&quot;.</b></span></p>
<p><span lang="en-ca"><b>3. When HttpSendRequest returns true, it only means the 
request is sent successfully. And in many cases, you still need response http 
headers from server which is my case. You need call httpqueryinfo. The message 
is stored in a series of string separated by CRLF or in c/c++ _T('\r') _T('\n');</b></span></p>
<p><span lang="en-ca"><b>4. Usually the http request is in a format of 
&quot;/url?paraname1=value1&amp;paraname2=value2&amp;paraname3=value3&quot;. In plain English, it 
means the target object, usually the script or cgi object followed by a &quot;?&quot; 
which indicates a series parameters separated by &quot;&amp;&quot;.</b></span></p>
<p><span lang="en-ca"><b>5. Compiling is always a little problem because you 
need platform sdk, like http.h, winsock2.h</b></span></p>
<div align="left">
  　</div>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca">C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">C</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<pre>　</pre>
<p>&nbsp;&nbsp;&nbsp;&nbsp;</p>
<pre>//compilation tips: It needs platformsdk2003. You need to setup your directory of 
//platformsdk BEFORE original vc6 directory. i.e. &quot;tools\options\directory\include&quot; 
//must have &quot;platformsdk\include&quot; before &quot;vc98\include&quot;

#include &quot;baseTsd.h&quot;
#include &quot;winsock2.h&quot;
#include &lt;windows.h&gt;
#include &quot;http.h&quot;
#include &lt;stdio.h&gt;
#include &lt;TCHAR.H&gt;

//#include &quot;Windns.h&quot;
#include &lt;wininet.h&gt;



//#pragma comment(lib, &quot;Ws2_32.lib&quot;)
//#pragma comment(lib, &quot;Dnsapi.lib&quot;)
#pragma comment(lib, &quot;Wininet.lib&quot;)


enum EnumSessionItem
{
   EnumSerial, EnumBrand, EnumModel, EnumProvider, EnumProviderKey, EnumAction, EnumTime
};

const int SessionItemCount = 7;
const int MaxSessionItemLength = 64;

LPCTSTR SessionItemName[SessionItemCount] =
{
   _T(&quot;serial&quot;), _T(&quot;brand&quot;), _T(&quot;model&quot;), _T(&quot;provider&quot;), _T(&quot;providerkey&quot;), _T(&quot;action&quot;),  _T(&quot;time&quot;)
};

LPCTSTR SessionItemValue[SessionItemCount] = 
{
   _T(&quot;EmuNick&quot;), _T(&quot;GLO.ng&quot;), _T(&quot;datawind.uk&quot;), _T(&quot;PDA_SERVER&quot;), _T(&quot;PREPAID_UPDATE&quot;), _T(&quot;update&quot;), _T(&quot;1000&quot;)
};


struct SessionInfo
{
   TCHAR sessionItem[SessionItemCount][MaxSessionItemLength];
};


#if 0
void packRequest(SessionInfo&amp; sessionInfo, LPTSTR requestBuffer)
{
   int len = 0;
   for (int i = 0; i &lt; SessionItemCount; i ++)
   {
      _stpintf(requestBuffer + len, _T(&quot;&amp;%s=%s&quot;), SessionItemName[i], sessionInfo.sessionItem[i]);
      len = _tcslen(requestBuffer);
   }
}
#else
bool packRequest(LPTSTR requestBuffer, DWORD requestBufferLength)
{
   DWORD len = 0;
   _sntprintf(requestBuffer, requestBufferLength, _T(&quot;%s=%s&quot;), SessionItemName[0], SessionItemValue[0]);
   
   for (int i = 1; i &lt; SessionItemCount; i ++)
   {
	   if ((len = _tcslen(requestBuffer)) &gt;= requestBufferLength)
	   {
		   return false;
	   }
      _sntprintf(requestBuffer + len, requestBufferLength - len, _T(&quot;&amp;%s=%s&quot;), SessionItemName[i], SessionItemValue[i]); 	    
   }
   return true;
}
#endif

bool sendWebRequestHelper(LPCTSTR serverIp, LPCTSTR targetUrl, LPCTSTR requestBuffer, LPSTR responseBuffer, DWORD&amp; responseBufferLength)
{
	DWORD headerIndex = 0;
	HINTERNET hInternet   = NULL, 
		      hConnect    = NULL, 
			  hRequest    = NULL;
   	
	LPCTSTR acceptType[2] = {_T(&quot;text/html&quot;), NULL};
	DWORD requestLength   = 0;
	bool result = false;
   int err = 0;

	if ((hInternet = InternetOpen(_T(&quot;Microsoft Internet Explorer 6.0&quot;), INTERNET_OPEN_TYPE_DIRECT,
		NULL, NULL, 0)) != NULL)
	{
		if ((hConnect = InternetConnect(hInternet, serverIp, INTERNET_DEFAULT_HTTP_PORT,
			NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0)) != NULL)
		{
			
			if ((hRequest = HttpOpenRequest(hConnect, _T(&quot;POST&quot;), requestBuffer, _T(&quot;HTTP/1.1&quot;),
				NULL, &amp;acceptType[0], INTERNET_FLAG_NO_UI, NULL))!= NULL)
			{	            
				requestLength = _tcslen(requestBuffer) * sizeof(TCHAR);
				if (HttpSendRequest(hRequest, NULL, 0, (LPVOID)requestBuffer, requestLength))
				{
					if (HttpQueryInfo(hRequest, HTTP_QUERY_RAW_HEADERS_CRLF , responseBuffer, &amp;responseBufferLength, &amp;headerIndex))
					{
						//_tprintf(_T(&quot;[%s]\n&quot;), responseBuffer);
						result = true;
					}
				}	
				else
				{
				   err = GetLastError();
				}
				InternetCloseHandle(hRequest);
			}
			InternetCloseHandle(hConnect);
		}
		InternetCloseHandle(hInternet);
	}
	return result;
}


bool searchHeaderMsg(LPTSTR msg, LPCTSTR headerName, LPTSTR msgBuffer, DWORD&amp; msgBufferLength)
{
	bool result = false;
	LPTSTR ptr;
	TCHAR CRLF[3] = {_T('\r'), _T('\n'), _T('\0')};
	int headerNameLength = 0;
	DWORD lengthCopied = 0;

	headerNameLength = _tcslen(headerName);


	ptr = &amp;msg[0];
	while (ptr != NULL)
	{
		if (_tcsnicmp(headerName, ptr, headerNameLength) == 0)
		{
			ptr += headerNameLength;
			break;
		}
		// else
		if ((ptr = _tcsstr(ptr, CRLF)) != NULL)
		{
			ptr += 2; //advance from CRLF
		}		
	}

	if (ptr != NULL)
	{
		lengthCopied = 0;
		while (! (ptr[0] == _T('\r') &amp;&amp; ptr[1] == _T('\n')) &amp;&amp; lengthCopied &lt; msgBufferLength)
		{
			msgBuffer[lengthCopied] = *ptr;
			lengthCopied ++;
			ptr ++;
		}
		msgBufferLength = lengthCopied;
		msgBuffer[msgBufferLength] = _T('\0');
		result = true;
	}
	return result;
}
	

bool crackUrl(LPCTSTR srcUrl, LPTSTR hostName, DWORD hostNameBufferSize, LPTSTR targetUrl, DWORD targetUrlLength)
{
	URL_COMPONENTS url_components;
	int len = 0;
	len = _tcslen(srcUrl) * sizeof(TCHAR);
	memset(&amp;url_components, 0, sizeof(URL_COMPONENTS));
	url_components.dwStructSize = sizeof(URL_COMPONENTS);
	
	url_components.dwUrlPathLength = targetUrlLength;
	url_components.lpszUrlPath = targetUrl;
	url_components.dwHostNameLength = hostNameBufferSize;
	url_components.lpszHostName = hostName;
	return InternetCrackUrl(srcUrl, len, ICU_DECODE, &amp;url_components) == TRUE;
}


bool sendWebRequest(LPCTSTR url, LPCTSTR parameter, LPSTR responseBuffer, DWORD responseBufferLength)
{
	bool   result = false;
	TCHAR  hostName[256];
	TCHAR  targetUrl[1024];
	TCHAR  requestBuffer[2048];
	if (crackUrl(url, hostName, 256, targetUrl, 1024))
	{
		_sntprintf(requestBuffer, 2048, _T(&quot;%s?%s&quot;), targetUrl, parameter);
		if (sendWebRequestHelper(hostName, targetUrl, requestBuffer, responseBuffer, responseBufferLength))
		{
			result = true;
			_tprintf(_T(&quot;%s&quot;), responseBuffer);
		}
	}
	return result;
}

bool sendWebRequestWithResponse(LPCTSTR url, LPCTSTR parameter, LPCTSTR responseHeaderName, LPTSTR responseBuffer, DWORD responseBufferLength)
{
	char responseMsgBuffer[2048];
	bool result = false;
	DWORD responseMsgBufferLength = 2048;

	if (sendWebRequest(url, parameter, responseMsgBuffer, responseMsgBufferLength))
	{
		if (searchHeaderMsg((LPTSTR)responseMsgBuffer, responseHeaderName, responseBuffer, responseBufferLength))
		{
			result = true;
		}
	}

	return result;
}
	
bool updateSessionInfo()
{
	LPCTSTR serverUrl= _T(&quot;http://support.datawind-s.com/provider-request/prepaidtime.jsp&quot;);
	
	TCHAR requestBuffer[2048];
	TCHAR  responseBuffer[2048];
	DWORD requestBufferLength = 2048;
	bool result = false;

	DWORD responseBufferLength = 2048;

	if (packRequest(requestBuffer, requestBufferLength))
	{
		if (sendWebRequestWithResponse(serverUrl, requestBuffer, _T(&quot;dw-message:&quot;), responseBuffer, responseBufferLength))
		{
			if (_tcsicmp(responseBuffer, _T(&quot; succeed&quot;)))
			{
				result = true;
			}
		}		
	}

	return result;
}

int main()
{
	if (updateSessionInfo())
	{
		_tprintf(_T(&quot;succeed sending\n&quot;));
	}
	return 0;
}




#if 0 
bool sendFile(LPCTSTR serverUrl, LPCTSTR fileName)
{
	HINTERNET hInternet = NULL, hConnect = NULL, hRequest = NULL;
   
	LPCTSTR acceptType[2]={_T(&quot;image/jpeg&quot;), NULL};
	INTERNET_BUFFERS inBuffer;
	HANDLE fileHandle = NULL;
	LPVOID fileBuffer = NULL;
	DWORD byteDone=0;
	bool result = false;

	if ((hInternet = InternetOpen(_T(&quot;Microsoft Internet Explorer 6.0&quot;), INTERNET_OPEN_TYPE_DIRECT,
		NULL, NULL, 0)) != NULL)
	{
		if ((hConnect = InternetConnect(hInternet, serverUrl, INTERNET_DEFAULT_HTTP_PORT,
			NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0)) != NULL)
		{
			if ((hRequest = HttpOpenRequest(hConnect, _T(&quot;POST&quot;), fileName, _T(&quot;HTTP/1.1&quot;),
				_T(&quot;directory&quot;), &amp;acceptType[0], INTERNET_FLAG_NO_UI, NULL))!= NULL)
			{
            
				if ((fileHandle=CreateFile(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, 
					OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL))!= NULL)
				{
					memset(&amp;inBuffer, 0, sizeof(INTERNET_BUFFERS));
					inBuffer.dwStructSize = sizeof(INTERNET_BUFFERS);
					if ((inBuffer.dwBufferTotal = GetFileSize(fileHandle, NULL)) 
						!= INVALID_FILE_SIZE)
					{						
						if ((fileBuffer=malloc(inBuffer.dwBufferTotal))!= NULL)
						{
							if (ReadFile(fileHandle, fileBuffer, inBuffer.dwBufferTotal, 
								&amp;byteDone, NULL) &amp;&amp; byteDone==inBuffer.dwBufferTotal)
							{			
                     
								if (HttpSendRequest(hRequest, NULL, 0, fileBuffer, inBuffer.dwBufferTotal))
								{
									result = true;
								}
							}
							free(fileBuffer);
						}						
					}
					CloseHandle(fileHandle);
				}
				InternetCloseHandle(hRequest);
			}
			InternetCloseHandle(hConnect);
		}
		InternetCloseHandle(hInternet);
	}
	return result;
}




/*
bool sendFile(LPCTSTR serverUrl, LPCTSTR fileName)
{
	HINTERNET hInternet = NULL, hConnect = NULL, hRequest = NULL;	
	LPCTSTR acceptType[2]={_T(&quot;image/jpeg&quot;), NULL};
	INTERNET_BUFFERS inBuffer, outBuffer;
	HANDLE fileHandle = NULL;
	LPVOID fileBuffer = NULL;
	DWORD byteDone=0;
	bool result = false;

	if ((hInternet = InternetOpen(_T(&quot;Microsoft Internet Explorer 6.0&quot;), INTERNET_OPEN_TYPE_DIRECT,
		NULL, NULL, 0)) != NULL)
	{
		if ((hConnect = InternetConnect(hInternet, serverUrl, INTERNET_DEFAULT_HTTP_PORT,
			NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0)) != NULL)
		{
			if ((hRequest = HttpOpenRequest(hConnect, _T(&quot;POST&quot;), fileName, _T(&quot;HTTP/1.1&quot;),
				_T(&quot;directory&quot;), &amp;acceptType[0], INTERNET_FLAG_NO_UI, NULL))!= NULL)
			{
				if ((fileHandle=CreateFile(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, 
					OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL))!= NULL)
				{
					memset(&amp;inBuffer, 0, sizeof(INTERNET_BUFFERS));
					inBuffer.dwStructSize = sizeof(INTERNET_BUFFERS);
					if ((inBuffer.dwBufferTotal = GetFileSize(fileHandle, NULL)) 
						!= INVALID_FILE_SIZE)
					{						
						if (HttpSendRequestEx(hRequest, &amp;inBuffer, &amp;outBuffer, HSR_CHUNKED, 0))
						{
							if ((fileBuffer=malloc(inBuffer.dwStructSize))!= NULL)
							{
								if (ReadFile(fileHandle, fileBuffer, inBuffer.dwStructSize, 
									&amp;byteDone, NULL) &amp;&amp; byteDone==inBuffer.dwStructSize)
								{
									if (InternetWriteFile(hRequest, fileBuffer, inBuffer.dwStructSize,
										&amp;byteDone)&amp;&amp; inBuffer.dwStructSize==byteDone)
									{
										result = true;
									}
								}
								free(fileBuffer);
							}
						}
						else
						{
							printf(&quot;error [%d]\n&quot;, GetLastError());
						}
					}
					CloseHandle(fileHandle);
				}
				InternetCloseHandle(hRequest);
			}
			InternetCloseHandle(hConnect);
		}
		InternetCloseHandle(hInternet);
	}
	return result;
}


  */



  bool getIpAddress(LPCTSTR url, LPTSTR ipAddress)
{
	DNS_STATUS result;
	PDNS_RECORD pDnsRecord = NULL;
	BYTE ipByte[4];
	bool res = false;
	if ((result = DnsQuery(url, DNS_TYPE_A, DNS_QUERY_STANDARD,
		NULL, &amp;pDnsRecord, NULL)) != 0)
	{
		printf(&quot;error of [%u]\n&quot;, result);
	}
	else
	{
		//printf(&quot;domain name [%s] dns ip4-address&quot;, pDnsRecord[0].pName);
		switch (pDnsRecord[0].wType)
		{
		case DNS_TYPE_A:
			memcpy(ipByte, &amp;pDnsRecord[0].Data.A, sizeof(DNS_A_DATA));
			_stprintf(ipAddress, _T(&quot;%u.%u.%u.%u&quot;), ipByte[0], ipByte[1], ipByte[2], ipByte[3]);
			res = true;
			printf(&quot;ipAddress =[%s]\n&quot;, ipAddress);
			break;
		case DNS_TYPE_NS:
			printf(&quot;DNS_TYPE_NS&quot;);
			break;
		case DNS_TYPE_MD:
			printf(&quot;DNS_TYPE_MD&quot;);
			break;
		case DNS_TYPE_MF:
			printf(&quot;DNS_TYPE_MF&quot;);
			break;
		case DNS_TYPE_CNAME:
			printf(&quot;DNS_TYPE_CNAME&quot;);
			_tprintf(_T(&quot;host name [%s]&quot;), pDnsRecord[0].Data.Cname.pNameHost);
			break;
		case DNS_TYPE_SOA:
			printf(&quot;DNS_TYPE_SOA&quot;);
			break;
		case DNS_TYPE_MB:
			printf(&quot;DNS_TYPE_MB&quot;);
			break;
		case DNS_TYPE_MG:
			printf(&quot;DNS_TYPE_MG&quot;);
			break;
		case DNS_TYPE_MR:
			printf(&quot;DNS_TYPE_MR&quot;);
			break;
		case DNS_TYPE_NULL:
			printf(&quot;DNS_TYPE_NULL&quot;);
			break;
		case DNS_TYPE_WKS:
			printf(&quot;DNS_TYPE_WKS&quot;);
			break;
		case DNS_TYPE_PTR:
			printf(&quot;DNS_TYPE_PTR&quot;);
			break;
		case DNS_TYPE_HINFO:
			printf(&quot;DNS_TYPE_HINFO&quot;);
			break;
		case DNS_TYPE_MINFO:
			printf(&quot;DNS_TYPE_MINFO&quot;);
			break;
		case DNS_TYPE_MX:
			printf(&quot;DNS_TYPE_MX&quot;);
			break;
		case DNS_TYPE_TEXT:
			printf(&quot;DNS_TYPE_TEXT&quot;);
			break;
		default:
			printf(&quot;nothing?&quot;);
			break;
		}
		printf(&quot;\n&quot;);
	}
	if (pDnsRecord != NULL)
	{
		DnsRecordListFree(pDnsRecord, DnsFreeRecordList);
	}
	return res;
}

#endif</pre>
<pre>　</pre>
<pre><font color="#FF0000">The following is a modifie<a name="dll"></a>d version, <a href="SendHttpRequest.rar">namely a dll version.</a></font>
	</pre>
<pre>//file : stdafx.h
#include &quot;basetsd.h&quot;
#include &quot;winsock2.h&quot;
#include &lt;windows.h&gt;
#include &lt;stdio.h&gt;
#include &lt;TCHAR.H&gt;
#include &lt;wininet.h&gt;
#include &quot;sendHttpRequest.h&quot;

#pragma comment(lib, &quot;wininet.lib&quot;)

</pre>
<pre>　</pre>
<pre>//file : stdafx.cpp
#include &quot;stdafx.h&quot;</pre>
<pre>　</pre>
<pre>　</pre>
<pre>//file : sendHttpRequest.h

#define SEND_HTTP_REQUEST_DLL




#ifdef SEND_HTTP_REQUEST_DLL
#define HTTP_API __declspec(dllexport)
#define HTTP_CALL __stdcall
#else
#define HTTP_API __declspec(dllimport)
#define HTTP_CALL __stdcall
#endif


extern &quot;C&quot;{

#define MAX_URL_LENGTH 2048


HTTP_API BOOL HTTP_CALL packParameter(LPTSTR buffer, DWORD bufferLength, LPCTSTR name1, LPCTSTR value1, ...);

HTTP_API BOOL HTTP_CALL sendWebRequestHelper(LPCTSTR serverIp, LPCTSTR targetUrl, LPCTSTR requestBuffer, LPSTR responseBuffer, DWORD&amp; responseBufferLength);

HTTP_API BOOL HTTP_CALL searchHeaderMsg(LPTSTR msg, LPCTSTR headerName, LPTSTR msgBuffer, DWORD&amp; msgBufferLength);

HTTP_API BOOL HTTP_CALL sendWebRequest(LPCTSTR url, LPCTSTR parameter, LPSTR responseBuffer, DWORD responseBufferLength);



HTTP_API BOOL HTTP_CALL sendWebRequestWithResponse(LPCTSTR url, LPCTSTR parameter, LPCTSTR responseHeaderName, LPTSTR responseBuffer, DWORD responseBufferLength);



HTTP_API BOOL HTTP_CALL updateSessionInfo(LPCTSTR serverUrl, LPCTSTR serialValue, LPCTSTR brandValue, LPCTSTR modelValue, 
					   LPCTSTR providerValue, LPCTSTR providerkeyValue, LPCTSTR actionValue, LPCTSTR timeValue);

HTTP_API BOOL HTTP_CALL sendWebSizeFile(LPCTSTR serverUrl, LPCTSTR fileName);


}; // extern &quot;C&quot;</pre>
<pre>　</pre>
<pre>//file : sendHttpRequest.cpp


//compilation tips: It needs platformsdk2003. You need to setup your directory of 
//platformsdk BEFORE original vc6 directory. i.e. &quot;tools\options\directory\include&quot; 
//must have &quot;platformsdk\include&quot; before &quot;vc98\include&quot;

#include &quot;stdafx.h&quot;

// QH : utility functions to conveniently packup parameter in http request. Usually parameter is in the format of 
// &quot;name1=value1&amp;name2=value2&amp;name3=value3...&quot;
// assumtion: user supply arbitary parameters ending with two NULL parameters. And at least one pair of &quot;name&quot; &quot;value&quot; 
// is supplied
BOOL packParameter(LPTSTR buffer, DWORD bufferLength, LPCTSTR name1, LPCTSTR value1, ...)
{
	BOOL result = FALSE;
	va_list marker;
	LPCTSTR  pName  = NULL;
	LPCTSTR  pValue = NULL;
	LPTSTR   ptr    = NULL;
	int bufferOffset = 0;

	int nameLength, valueLength;

	if (name1 == NULL || value1 == NULL){
		return FALSE;
	}

	nameLength  = _tcslen(name1);
	valueLength = _tcslen(value1);
	
	bufferOffset = (nameLength + valueLength + 1);

	pName  = name1;
	pValue = value1;

	// QH : here we check the buffer size so that we don't have to worry about buffer overflow 
	// when writing
	if (bufferOffset &lt; bufferLength - 1){
		_stprintf(buffer, _T(&quot;%s=%s&quot;), name1, value1);
	}else {
		return FALSE;
	}
	ptr = buffer + bufferOffset;

	va_start(marker, value1);     /* Initialize variable arguments. */

	do
	{	  
		pName  = va_arg(marker, LPCTSTR);
		pValue = va_arg(marker, LPCTSTR);
		if (pName == NULL || pValue == NULL)
		{
			break;
		}
		nameLength  = _tcslen(pName);
		valueLength = _tcslen(pValue);
		// QH : here I DELIBERATELY to change bufferOffset value before comparison, so that we can
		// return success or failure when function return
		bufferOffset += (nameLength + valueLength + 2); //  '&amp;' and '=' makes 2
		if (bufferOffset &gt;= bufferLength - 1){
			break;
		}
		_stprintf(ptr, _T(&quot;&amp;%s=%s&quot;), pName, pValue);
		ptr = buffer + bufferOffset;
	}
	while (TRUE);	
	va_end( marker );              /* Reset variable arguments.      */

	return (pName == NULL &amp;&amp; pValue == NULL &amp;&amp; (bufferOffset &lt; bufferLength - 1));
}


BOOL HTTP_CALL sendWebRequestHelper(LPCTSTR serverIp, LPCTSTR targetUrl, LPCTSTR requestBuffer, LPSTR responseBuffer, DWORD&amp; responseBufferLength)
{
	DWORD headerIndex = 0;
	HINTERNET hInternet   = NULL, 
		      hConnect    = NULL, 
			  hRequest    = NULL;
   	
	LPCTSTR acceptType[2] = {_T(&quot;text/html&quot;), NULL};
	DWORD requestLength   = 0;
	BOOL result = FALSE;
    int err = 0;

	if ((hInternet = InternetOpen(_T(&quot;Microsoft Internet Explorer 6.0&quot;), INTERNET_OPEN_TYPE_DIRECT,
		   NULL, NULL, 0)) != NULL)	{
		if ((hConnect = InternetConnect(hInternet, serverIp, INTERNET_DEFAULT_HTTP_PORT,
			   NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0)) != NULL){
			
			if ((hRequest = HttpOpenRequest(hConnect, _T(&quot;POST&quot;), requestBuffer, _T(&quot;HTTP/1.1&quot;),
				   NULL, &amp;acceptType[0], INTERNET_FLAG_NO_UI, NULL))!= NULL) {	            
				requestLength = _tcslen(requestBuffer) * sizeof(TCHAR);
				if (HttpSendRequest(hRequest, NULL, 0, (LPVOID)requestBuffer, requestLength))	{
					if (HttpQueryInfo(hRequest, HTTP_QUERY_RAW_HEADERS_CRLF , responseBuffer, &amp;responseBufferLength, 
                  &amp;headerIndex))	{
						//_tprintf(_T(&quot;[%s]\n&quot;), responseBuffer);
						result = TRUE;
					}
				}else{
				   err = GetLastError();
				}
				InternetCloseHandle(hRequest);
			}
			InternetCloseHandle(hConnect);
		}
		InternetCloseHandle(hInternet);
	}
	return result;
}


// QH: utility function to help retrieve target &quot;header&quot; contents from response buffer of request-sending
// param: 
//  msg(in):         the response raw data
//  headerName(in) : the target header name
//  msgBuffer(out):   the buffer to hold retrieved contents of &quot;headerName&quot;
//  msgBufferLength(in/out): the buffer length of &quot;msgBuffer&quot; and return writen characters
BOOL HTTP_CALL searchHeaderMsg(LPTSTR msg, LPCTSTR headerName, LPTSTR msgBuffer, DWORD&amp; msgBufferLength)
{
	BOOL result = FALSE;
	LPTSTR ptr;
	TCHAR CRLF[3] = {_T('\r'), _T('\n'), _T('\0')};
	int headerNameLength = 0;
	DWORD lengthCopied = 0;

	headerNameLength = _tcslen(headerName);

	ptr = &amp;msg[0];
	while (ptr != NULL)
	{
		if (_tcsnicmp(headerName, ptr, headerNameLength) == 0){
			// QH: found target and move to pointing data
			ptr += headerNameLength;
			break;
		}
		// else
		if ((ptr = _tcsstr(ptr, CRLF)) != NULL){
			ptr += 2; //advance from CRLF
		}		
	}

	if (ptr != NULL)	{
		lengthCopied = 0;
		// copy data until CRLF
		while (! (ptr[0] == _T('\r') &amp;&amp; ptr[1] == _T('\n')) &amp;&amp; lengthCopied &lt; msgBufferLength)
		{
			msgBuffer[lengthCopied] = *ptr;
			lengthCopied ++;
			ptr ++;
		}
		msgBufferLength = lengthCopied;
		msgBuffer[msgBufferLength] = _T('\0');
		result = TRUE;
	}
	return result;
}
	

// QH: we need retrieve host name and checking if it is http protocol
// params:
// srcUrl(in) : the url to be cracked
// hostName(out): buffer to receive cracked host name
// hostNameBufferSize(in) : size of buffer of &quot;hostName&quot;
// targetUrl(out): the relative &quot;path&quot; to hostName. i.e. jsp object or any server object
// targetUrlLength(in): the size of buffer to hold &quot;targetUrl&quot;
BOOL HTTP_CALL crackUrl(LPCTSTR srcUrl, LPTSTR hostName, DWORD hostNameBufferSize, LPTSTR targetUrl, DWORD targetUrlLength)
{
	BOOL result = FALSE;
	URL_COMPONENTS url_components;
	int len = 0;

	len = _tcslen(srcUrl) * sizeof(TCHAR);
	memset(&amp;url_components, 0, sizeof(URL_COMPONENTS));
	url_components.dwStructSize = sizeof(URL_COMPONENTS);
	
	url_components.dwUrlPathLength = targetUrlLength;
	url_components.lpszUrlPath = targetUrl;
	url_components.dwHostNameLength = hostNameBufferSize;
	url_components.lpszHostName = hostName;
	
	if (InternetCrackUrl(srcUrl, len, ICU_DECODE, &amp;url_components)) {
		if (url_components.nScheme == INTERNET_SCHEME_HTTP ) {
			result = TRUE;
		}
	}
	return result;
}

// assume parameter is already in format of &quot;name1=value1&amp;name2=value2...&quot;
// responseBuffer is to receive raw response text data
BOOL HTTP_CALL sendWebRequest(LPCTSTR url, LPCTSTR parameter, LPSTR responseBuffer, DWORD responseBufferLength)
{
	BOOL   result = FALSE;
	TCHAR  hostName[MAX_URL_LENGTH];
	TCHAR  targetUrl[MAX_URL_LENGTH];
	TCHAR  requestBuffer[MAX_URL_LENGTH];
	if (crackUrl(url, hostName, MAX_URL_LENGTH, targetUrl, MAX_URL_LENGTH))	{
		_sntprintf(requestBuffer, MAX_URL_LENGTH, _T(&quot;%s?%s&quot;), targetUrl, parameter);
		if (sendWebRequestHelper(hostName, targetUrl, requestBuffer, responseBuffer, responseBufferLength)){
			result = TRUE;
			//_tprintf(_T(&quot;%s&quot;), responseBuffer);
		}
	}
	return result;
}

// send request and also retrieve interested &quot;header&quot; contents if succeeded
BOOL HTTP_CALL sendWebRequestWithResponse(LPCTSTR url, LPCTSTR parameter, LPCTSTR responseHeaderName, LPTSTR responseBuffer, DWORD responseBufferLength)
{
	char responseMsgBuffer[MAX_URL_LENGTH];
	BOOL result = FALSE;
	DWORD responseMsgBufferLength = MAX_URL_LENGTH;

	if (sendWebRequest(url, parameter, responseMsgBuffer, responseMsgBufferLength))	{
		if (searchHeaderMsg((LPTSTR)responseMsgBuffer, responseHeaderName, responseBuffer, responseBufferLength))		{
			result = TRUE;
		}
	}

	return result;
}
	

BOOL HTTP_CALL updateSessionInfo(LPCTSTR serverUrl, LPCTSTR serialValue, LPCTSTR brandValue, LPCTSTR modelValue, 
					   LPCTSTR providerValue, LPCTSTR providerkeyValue, LPCTSTR actionValue, LPCTSTR timeValue)
{
	
const int SessionItemCount = 7;

LPCTSTR SessionItemName[SessionItemCount] =
{
   _T(&quot;serial&quot;), _T(&quot;brand&quot;), _T(&quot;model&quot;), _T(&quot;provider&quot;), _T(&quot;providerkey&quot;), _T(&quot;action&quot;),  _T(&quot;time&quot;)
};


	TCHAR requestBuffer[MAX_URL_LENGTH];
	TCHAR  responseBuffer[2048];
	DWORD requestBufferLength = MAX_URL_LENGTH;
	BOOL result = FALSE;

	DWORD responseBufferLength = MAX_URL_LENGTH;

	if (packParameter(requestBuffer, requestBufferLength, SessionItemName[0], serialValue, SessionItemName[1], brandValue,
		SessionItemName[2], modelValue, SessionItemName[3], providerValue, SessionItemName[4], providerkeyValue, 
		SessionItemName[5], actionValue, SessionItemName[6], timeValue, NULL, NULL)){

		if (sendWebRequestWithResponse(serverUrl, requestBuffer, _T(&quot;dw-message:&quot;), responseBuffer, responseBufferLength)){
			if (_tcsicmp(responseBuffer, _T(&quot; succeed&quot;))){
				result = TRUE;
			}
		}		
	}

	return result;
}



BOOL HTTP_CALL sendWebSizeFile(LPCTSTR serverUrl, LPCTSTR fileName)
{
	HINTERNET hInternet = NULL, hConnect = NULL, hRequest = NULL;
   
	//LPCTSTR acceptType[2] = {_T(&quot;image/jpeg&quot;), NULL};
	LPCTSTR acceptType[2] = {_T(&quot;*/*&quot;), NULL};
	
	HANDLE fileHandle     = NULL;
	LPVOID fileBuffer     = NULL;
	DWORD byteDone        = 0;
	DWORD bufferSize      = 0;
	BOOL   result         = FALSE;

	if ((hInternet = InternetOpen(_T(&quot;Microsoft Internet Explorer 6.0&quot;), INTERNET_OPEN_TYPE_DIRECT,
		NULL, NULL, 0)) != NULL)
	{
		if ((hConnect = InternetConnect(hInternet, serverUrl, INTERNET_DEFAULT_HTTP_PORT,
			NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0)) != NULL)
		{
			if ((hRequest = HttpOpenRequest(hConnect, _T(&quot;POST&quot;), fileName, _T(&quot;HTTP/1.1&quot;),
				_T(&quot;directory&quot;), &amp;acceptType[0], INTERNET_FLAG_NO_UI, NULL))!= NULL)
			{
            
				if ((fileHandle = CreateFile(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, 
					OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL))!= INVALID_HANDLE_VALUE)
				{
					
					if ((bufferSize = GetFileSize(fileHandle, NULL)) != INVALID_FILE_SIZE)
					{						
						if ((fileBuffer = malloc(bufferSize))!= NULL)
						{
							if (ReadFile(fileHandle, fileBuffer, bufferSize, &amp;byteDone, NULL) &amp;&amp; byteDone == bufferSize)
							{	                     
								if (HttpSendRequest(hRequest, NULL, 0, fileBuffer, bufferSize))
								{
									result = TRUE;
								}
							}
							free(fileBuffer);
						}						
					}
					CloseHandle(fileHandle);
				}
				InternetCloseHandle(hRequest);
			}
			InternetCloseHandle(hConnect);
		}
		InternetCloseHandle(hInternet);
	}
	return result;
}


</pre>
<pre>　</pre>
<pre>　</pre>
<pre>　</pre>
<pre><font color="#FF0000">The following is a simple caller for testing.</font></pre>
<pre>　</pre>
<pre>// stdafx.h : include file for standard system include files,
//  or project specific include files that are used frequently, but
//      are changed infrequently
//

#if !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
#define AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_

#if _MSC_VER &gt; 1000
#pragma once
#endif // _MSC_VER &gt; 1000

#define WIN32_LEAN_AND_MEAN		// Exclude rarely-used stuff from Windows headers

#include &lt;windows.h&gt;
#include &lt;stdio.h&gt;
#include &lt;TCHAR.H&gt;
#include &quot;..\\sendHttpRequest.h&quot;

#pragma comment(lib, &quot;sendHttpRequest.lib&quot;)

// TODO: reference additional headers your program requires here

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)</pre>
<pre>　</pre>
<pre>　</pre>
<pre>　</pre>
<pre>// stdafx.cpp : source file that includes just the standard includes
//	SendRequestTest.pch will be the pre-compiled header
//	stdafx.obj will contain the pre-compiled type information

#include &quot;stdafx.h&quot;

// TODO: reference any additional headers you need in STDAFX.H
// and not in this file</pre>
<pre>　</pre>
<pre>　</pre>
<pre>// SendRequestTest.cpp : Defines the entry point for the application.
//

#include &quot;stdafx.h&quot;

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
LPCTSTR serverUrl= _T(&quot;http://support.datawind-s.com/provider-request/prepaidtime.jsp&quot;);

LPCTSTR SessionItemValue[7] = 
{
   _T(&quot;EmuNick&quot;), _T(&quot;GLO.ng&quot;), _T(&quot;datawind.uk&quot;), _T(&quot;PDA_SERVER&quot;), _T(&quot;PREPAID_UPDATE&quot;), _T(&quot;update&quot;), _T(&quot;1000&quot;)
};



	if (updateSessionInfo(serverUrl, SessionItemValue[0], SessionItemValue[1], SessionItemValue[2], 
		SessionItemValue[3], SessionItemValue[4], SessionItemValue[5], SessionItemValue[6])) {
		MessageBox(NULL, _T(&quot;succeed!&quot;), _T(&quot;message&quot;), MB_OK);
	}


	return 0;
}


</pre>
<pre>　</pre>
<pre>　</pre>
<pre></pre>
<pre>			
</pre>

<pre></pre>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp; <a href="WhoAmI.htm">                  







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="index.htm"><img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                         
<img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</p>

</body>

</html>