/**
* request server - rimke(466814195)
* @param LPCTSTR url request url
* @param LPCTSTR data data for request
* @param UINT httpRequest request type,eg HTTP_REQUEST_GET or HTTP_REQUEST_POST
* @return LPCTSTR returns reponse message if successful, otherwise reutrns null
*/
LPCTSTR CEtHTTP::request(LPCTSTR url, LPCTSTR data, UINT httpRequest)
{
if( _tcslen(url) < 1){ return NULL; }
HINTERNET session = InternetOpen(_T("Http-connect"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL);
if(NULL == session){ return NULL; }
INTERNET_PORT port = INTERNET_DEFAULT_HTTP_PORT;
TCHAR lpHostName[255]; ZeroMemory(lpHostName, sizeof(lpHostName));
this->getHostName(url, lpHostName);
port=this->getPort(url);
port=port?port:INTERNET_DEFAULT_HTTP_PORT;
HINTERNET connect = InternetConnect(session, lpHostName, port, NULL, NULL, INTERNET_SERVICE_HTTP, NULL, NULL);
if( NULL == connect ){ return NULL; }
LPCTSTR lpszVerb = _T("GET");
if ( httpRequest == HTTP_REQUEST_POST){ lpszVerb = _T("POST"); }
LPCTSTR lpszVersion = _T("HTTP/1.1");
LPCTSTR lpszReferrer = NULL;
LPCTSTR lplpszAcceptType= NULL;
int dwFlags = INTERNET_FLAG_RELOAD;
DWORD dwContext = NULL;
TCHAR lpszObjectName[255]; ZeroMemory(lpszObjectName, sizeof(lpszObjectName));
HINTERNET _rqt= HttpOpenRequest(connect, lpszVerb, (LPCTSTR)&lpszObjectName, lpszVersion, lpszReferrer, &lplpszAcceptType, dwFlags, dwContext );
if(NULL == _rqt){return NULL;}
BOOL bRet = FALSE;
char header[255]; ZeroMemory(header, sizeof(header));
if(httpRequest==HTTP_REQUEST_GET){
bRet = HttpSendRequestA(_rqt, (LPCSTR)&header, (DWORD)strlen(header), NULL, 0);
}else{
bRet = HttpSendRequestA(_rqt, (LPCSTR)&header, (DWORD)strlen(header), (LPVOID)data, (DWORD)_tcslen(data));
}
if (!bRet){ return NULL;}
DWORD dwReadSize = 1024;
char buffer[1025] = {0};
if ( !HttpQueryInfoA(_rqt, HTTP_QUERY_RAW_HEADERS, buffer, &dwReadSize, NULL) ){ return NULL; }
if( !strstr(buffer, "200 OK") ){ return NULL;}
DWORD dwNumberOfBytesToRead = sizeof(buffer);
DWORD lpdwNumberOfBytesRead = 0;
DWORD count = 0;
LPSTR content = NULL;
LPSTR lpTmp = NULL;
while(TRUE){
ZeroMemory(buffer, dwNumberOfBytesToRead);
bRet = InternetReadFile(_rqt, buffer, dwNumberOfBytesToRead-1, &lpdwNumberOfBytesRead );
if ( !bRet || (0 == lpdwNumberOfBytesRead) ){ break; }
count += lpdwNumberOfBytesRead+10;
lpTmp = content;
content = (LPSTR)malloc( count );ZeroMemory(content, count );
if( NULL != lpTmp ){
sprintf_s(content, count-1, "%s%s", lpTmp, buffer);
}else{
sprintf_s(content, count-1, "%s", buffer);
}
if(NULL !=lpTmp){ free(lpTmp); lpTmp=NULL;}
}
if(NULL !=lpTmp){ free(lpTmp); lpTmp=NULL;}
if( NULL == content ){ return NULL; }
LPTSTR reval = (LPTSTR)malloc(count * sizeof(TCHAR));
this->char2Tchar( content, reval );
free(content);
return reval;
}