⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 internetconnect.cpp

📁 利用Winet测试internet连接网络
💻 CPP
字号:
//InternetConnect.cpp
//
/*#include "stdafx.h"*/
#include "InternetConnect.h"

/*extern BOOL g_bThreadExit;*/

CInternetConnect::CInternetConnect()
{
	hSession = NULL;
	hConnect = NULL;
	hRequest = NULL; 
}

CInternetConnect::~CInternetConnect()
{
	if (hRequest != NULL)
	{
		InternetCloseHandle(hRequest);
		hRequest = NULL;
	}
	if (hConnect != NULL)
	{
		InternetCloseHandle(hConnect);
		hConnect = NULL;
	}
	if (hSession != NULL)
	{
		InternetCloseHandle(hSession);
		hSession = NULL;
	}
}

int CInternetConnect::ConnectToServer(wchar_t* lpszServerName, int nServerPort, wchar_t* lpszURL)	//连接Internet
{
	//初始化 WinInet.dll
	hSession = InternetOpen( TEXT("mapabc Search POI"), INTERNET_OPEN_TYPE_PRECONFIG/*LOCAL_INTERNET_ACCESS*/, \
		NULL, INTERNET_INVALID_PORT_NUMBER, 0 );

	if (!hSession)
		return (UINT)INTERNET_OPEN_FALIED;

	//连接服务器
	hConnect = InternetConnect(hSession, lpszServerName, nServerPort, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
	
	if (!hConnect)
		return (UINT)INTERNET_CONNECT_FAILED;

	//打开一个 HTTP 请求的句柄
	hRequest = HttpOpenRequest(hConnect, TEXT("GET"), lpszURL, HTTP_VERSION, NULL, NULL, INTERNET_FLAG_RELOAD, 0 );	
	
	if (!hRequest)
		return (UINT)REQUEST_HTTP_FAILED;

	//向 HTTP 服务器发送指定的请求
	BOOL bSend = FALSE;
	
	for( int i = 0; i < 10; i++ )
	{
		// 停止搜索
// 		if (g_bThreadExit)
// 		{
// 			break;
// 		}
		
		bSend = HttpSendRequest( hRequest, NULL, 0, NULL, 0 );
		if( !bSend)
			Sleep( 50 );
		else
			break;
	}
	
	if (!bSend)
		return (UINT)SEND_REQUEST_FAILED;

	return (UINT)CONNECT_SERVER_SUCCESS;

}

int CInternetConnect::ReceiveFromServer(char *lpszReceiveBuffer, int dwNumberOfBytesToRead)	//接收数据
{
	//从一个打开的句柄读取数据
	BOOL bRead = FALSE;
	DWORD dwBytesRead = 0;
	
	bRead = InternetReadFile(hRequest, lpszReceiveBuffer, (dwNumberOfBytesToRead - 1), &dwBytesRead);
	
	if (!bRead || dwBytesRead == 0)
		return (UINT)READ_DATA_FAILED;

	return (UINT)READ_DATA_SUCCESS;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -