|
static void MyGroupMyCommand1 () {
HINTERNET hSession = NULL;
HINTERNET hConnect = NULL;
HINTERNET hRequest = NULL;
//1. 初始化一个WinHTTP-session句柄,参数1为此句柄的名称
hSession = WinHttpOpen(L"WinHTTP Example/1.0",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0);
if (hSession == NULL) {
cout << "Error:Open session failed: " << GetLastError() << endl;
exit(0);
}
//2. 通过上述句柄连接到服务器,需要指定服务器IP和端口号 INTERNET_DEFAULT_HTTP_PORT:80。若连接成功,返回的hConnect句柄不为NULL
hConnect = WinHttpConnect(hSession, L"www.citext.cn", INTERNET_DEFAULT_HTTP_PORT, 0);
if (hConnect == NULL) {
cout << "Error:Connect failed: " << GetLastError() << endl;
exit(0);
}
//3. 通过hConnect句柄创建一个hRequest句柄,用于发送数据与读取从服务器返回的数据。
hRequest = WinHttpOpenRequest(hConnect, L"GET", L"GetTime.php", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
//其中参数2表示请求方式,此处为Get;参数3:给定Get的具体地址,如这里的具体地址为[url]https://www.citext.cn/GetTime.php[/url]
if (hRequest == NULL) {
cout << "Error:OpenRequest failed: " << GetLastError() << endl;
exit(0);
}
BOOL bResults;
bResults = WinHttpSendRequest(hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS,
0, WINHTTP_NO_REQUEST_DATA, 0,
0, 0);
if (!bResults){
cout << "Error:SendRequest failed: " << GetLastError() << endl;
exit(0);
}
else{
//(3) 发送请求成功则准备接受服务器的response。注意:在使用 WinHttpQueryDataAvailable和WinHttpReadData前必须使用WinHttpReceiveResponse才能access服务器返回的数据
bResults = WinHttpReceiveResponse(hRequest, NULL);
}
LPVOID lpHeaderBuffer = NULL;
DWORD dwSize = 0;
//4-3. 获取服务器返回数据
LPSTR pszOutBuffer = NULL;
DWORD dwDownloaded = 0; //实际收取的字符数
wchar_t *pwText = NULL;
if (bResults)
{
do
{
//(1) 获取返回数据的大小(以字节为单位)
dwSize = 0;
if (!WinHttpQueryDataAvailable(hRequest, &dwSize)){
cout << "Error:WinHttpQueryDataAvailable failed:" << GetLastError() << endl;
break;
}
if (!dwSize) break; //数据大小为0
//(2) 根据返回数据的长度为buffer申请内存空间
pszOutBuffer = new char[dwSize + 1];
if (!pszOutBuffer){
cout << "Out of memory." << endl;
break;
}
ZeroMemory(pszOutBuffer, dwSize + 1); //将buffer置0
//(3) 通过WinHttpReadData读取服务器的返回数据
if (!WinHttpReadData(hRequest, pszOutBuffer, dwSize, &dwDownloaded)){
cout << "Error:WinHttpQueryDataAvailable failed:" << GetLastError() << endl;
}
if (!dwDownloaded)
break;
} while (dwSize > 0);
//4-4. 将返回数据转换成UTF8
DWORD dwNum = MultiByteToWideChar(CP_ACP, 0, pszOutBuffer, -1, NULL, 0); //返回原始ASCII码的字符数目
pwText = new wchar_t[dwNum]; //根据ASCII码的字符数分配UTF8的空间
MultiByteToWideChar(CP_UTF8, 0, pszOutBuffer, -1, pwText, dwNum); //将ASCII码转换成UTF8
acutPrintf(_T("\n返回值all: %s \n\n"), pwText);
std::string strs;
Wchar_tToString(strs,pwText);
for ( int i = 0; i < strs.size(); i++)
{
acutPrintf(_T("\n返回值: %s \n\n"), strs.at(i));
}
}
//5. 依次关闭request,connect,session句柄
if (hRequest) WinHttpCloseHandle(hRequest);
if (hConnect) WinHttpCloseHandle(hConnect);
if (hSession) WinHttpCloseHandle(hSession);
/*return 0;*/
}
static void Wchar_tToString(std::string& szDst, wchar_t *wchar)
{
wchar_t * wText = wchar;
DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,NULL,0,NULL,FALSE);// WideCharToMultiByte的运用
char *psText; // psText为char*的临时数组,作为赋值给std::string的中间变量
psText = new char[dwNum];
WideCharToMultiByte (CP_OEMCP,NULL,wText,-1,psText,dwNum,NULL,FALSE);// WideCharToMultiByte的再次运用
szDst = psText;// std::string赋值
delete []psText;// psText的清除
} |
|