前几天领导让统计用户活跃量,想着加一个主动上报的进程,在服务启动的时候调用。
server端是一个页面
http://s.co.com/analysis.htm
Clinet端访问的时候在后面加上参数即可,ID=XXXX&IP=XXXX,然后对后面的集合进行BASE64编码,整合之后Client端要发送的消息就是
Get http://s.co.com/analysis.htm?Sk1fVkVSPTIwMTMmSk1fSUQ9NTcwNmUzZDQtMTNmZi00Y2FkLWJlYWQtOGU3ZTIyNTk0ZGJi
照道理讲,很简单的一个功能啊,结果卖萌如我,竟然在这上面耗费了一天,以后请叫我糊涂蛋
程序中有一个上报模块Upload.dll,所需要的做的只是实现一个导出函数UploadXInfo,在这个函数里实现访问页面的功能。
采用的是WinInet函数族。
根据上面的代码,可以看出是如何分离URL的,以http://www.example.com?xx=yy&mm=nn为例
InternetOpen中要传入的是Host,而HttpSendRequest要传入的是参数,而且不要多加上协议、\之类的多余部分。
void CALLBACK UploadXInfo(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow) { WCHAR szClient[100] = {0}; DWORD dwLen = 100; ReadComputerUUID( szClient, dwLen );//按照一定规则生成客户端唯一标志 CString strVer = CConfigReader::GetInstance().ProductVersion(); CString strUrl = _T("http://s.jiangmin.com/"); CString temp = _T("VER="); CString tmp = _T("/analysis.htm?"); temp += strVer; temp += _T("&ID="); temp += (LPCTSTR)szClient; //BASE64编码 CW2A wsz(temp,CP_UTF8); LPCSTR pSrc = wsz; size_t iLen = strlen(pSrc); int iEncodeLen = Base64EncodeGetRequiredLength( (int)iLen*sizeof(char) ) + 1; char *pEncode = new char[iEncodeLen]; if(pEncode == NULL) { return; } ZeroMemory(pEncode, iEncodeLen); Base64Encode((const BYTE *)pSrc, (int)iLen, pEncode, &iEncodeLen, ATL_BASE64_FLAG_NOCRLF); tmp.Append(CA2W(pEncode)); tmp.Append(L"\r\n"); /*CUrl url; if(!url.CrackUrl(strUrl)) return;*/ BOOL bRet = FALSE; HINTERNET hInetOpen = NULL, hInetConnect = NULL, hInetRequest = NULL; //DWORD dwStatus, dwIndex, dwSize; hInetOpen = InternetOpen( _T("HttpTest"), INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY, NULL, NULL, 0 ); if(hInetOpen == NULL) { goto QUIT; } hInetConnect = InternetConnect(hInetOpen, _T("s.jiangmin.com"), //就是在这里,http://s.jiangmin.com/ s.jiangmin.com/都会报错 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0); if(hInetConnect == NULL) { goto QUIT; } //上传数据或文件 hInetRequest = HttpOpenRequest(hInetConnect, _T("GET"), tmp, _T("HTTP/1.1"), NULL, 0, INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_AUTO_REDIRECT | INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_NO_COOKIES, 0 ); if(hInetRequest == NULL) { ATLTRACE( "HttpOpenRequest return failed\n "); DWORD dwRet = GetLastError(); goto QUIT; } bRet = HttpSendRequest(hInetRequest,NULL,0,NULL,0); DWORD dwRet1 = 0; dwRet1 = ::GetLastError(); if(FALSE == bRet) { ATLTRACE( "HttpSendRequest return failed\n "); goto QUIT; } QUIT: if(NULL != pEncode) { delete[] pEncode; pEncode = NULL; } if(hInetRequest != NULL) InternetCloseHandle( hInetRequest ); if(hInetConnect != NULL) InternetCloseHandle( hInetConnect ); if(hInetOpen != NULL) InternetCloseHandle( hInetOpen ); return; }