wininet异步

#include<windows.h>
#include<wininet.h>
#include<iostream>

DWORD dwNumKSent;
DWORD dwNumKToSend;
DWORD dwNumBytesComplete = 0;
char lpOutBuf[1024];
HANDLE hConnectedEvent, hRequestCompleteEvent;
HINTERNET hInstance, hConnect, hRequest;
char *lpszUrl, *lpszServer;

BOOL bAllDone = FALSE;

using namespace std;
#pragma comment(lib,"Wininet.lib")

void __stdcall Callback(HINTERNET hInternet,
						DWORD dwContext,
						DWORD dwInternetStatus,
						LPVOID lpStatusInfo,
						DWORD dwStatusInfoLen);

void main(int argc, char *argv[])
{
// 	if (argc != 4)
// 	{
// 		cout << "Usage: sendreqexasync <server> <url> <size in kilobytes>" << endl;
// 		cout << "   Example: sendreqexasync www.foo.com /postfolder/upload.exe 256" << endl;
// 		return;
// 	}

	lpszServer = "www.baidu.com";
	lpszUrl = "index.html";
	dwNumKToSend = 0;

	FillMemory(lpOutBuf, 1024, 'A');
	hConnectedEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
	hRequestCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

	hInstance = InternetOpen("sendreqexasync", 
		INTERNET_OPEN_TYPE_PRECONFIG,
		NULL,
		NULL,
		INTERNET_FLAG_ASYNC);

	if (hInstance == NULL)
	{
		cout << "InternetOpen failed, error " << GetLastError();
		return;
	}

	if (InternetSetStatusCallback(hInstance,
		(INTERNET_STATUS_CALLBACK)&Callback) == INTERNET_INVALID_STATUS_CALLBACK)
	{
		cout << "InternetSetStatusCallback failed, error " << GetLastError();
		return;
	}

	hConnect = InternetConnect(hInstance, 
		lpszServer, 
		INTERNET_DEFAULT_HTTP_PORT,
		NULL,
		NULL,
		INTERNET_SERVICE_HTTP,
		0,
		1);
	if (hConnect == NULL)
	{
		if (GetLastError() != ERROR_IO_PENDING)
		{
			cout << "InternetConnect failed, error " << GetLastError();
			return;
		}
		WaitForSingleObject(hConnectedEvent, INFINITE);
	}

	hRequest = HttpOpenRequest(hConnect, 
		"POST", 
		lpszUrl,
		NULL,
		NULL,
		NULL,
		INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_NO_COOKIES,
		2);
	if (hRequest == NULL)
	{
		if (GetLastError() != ERROR_IO_PENDING)
		{
			cout << "HttpOpenRequest failed, error " << GetLastError();
			return;
		}
		WaitForSingleObject(hRequestCompleteEvent, INFINITE);
	}

	INTERNET_BUFFERS IntBuff;

	FillMemory(&IntBuff, sizeof(IntBuff), 0);
	IntBuff.dwStructSize= sizeof(IntBuff);
	IntBuff.dwBufferTotal = 1024*dwNumKToSend;
	IntBuff.lpcszHeader = "Content-Type: text/text/r/n";
	IntBuff.dwHeadersLength = lstrlen(IntBuff.lpcszHeader);

	if (!HttpSendRequestEx(hRequest, 
		&IntBuff, 
		NULL, 
		0,
		2))
	{
		if (GetLastError() != ERROR_IO_PENDING)
		{
			cout << "HttpSendRequestEx failed, error " << GetLastError();
			return;
		}
		cout << "HttpSendRequestEx called successfully" << endl;
		cout.flush();

		WaitForSingleObject(hRequestCompleteEvent, INFINITE);
	}

	for (dwNumKSent = 0; dwNumKSent < dwNumKToSend; dwNumKSent++)
	{
		DWORD dwBytesWritten;

		if(!InternetWriteFile(hRequest,
			lpOutBuf,
			1024,
			&dwBytesWritten))
		{
			if (GetLastError() != ERROR_IO_PENDING)
			{
				cout << "InternetWriteFile failed, error " << GetLastError();
				return;
			}
			else
			{
				cout << "InternetWriteFile completing asynchronously" << endl;
				cout.flush();
				WaitForSingleObject(hRequestCompleteEvent, INFINITE);
			}
		}
	}

	cout << "Calling HttpEndRequest" << endl;
	cout.flush();
	if (!HttpEndRequest(hRequest, NULL, HSR_INITIATE, 2))
	{
		if (GetLastError() == ERROR_IO_PENDING)
		{
			cout << "HttpEndRequest called" << endl;
			cout.flush();
			WaitForSingleObject(hRequestCompleteEvent, INFINITE);
		}
		else
		{
			cout << "HttpEndRequest failed, error " << GetLastError() << endl;
			return;
		}
	}


	cout << "------------------- Read the response -------------------" << endl;
	char lpReadBuff[256];

	do
	{
		INTERNET_BUFFERS InetBuff;
		FillMemory(&InetBuff, sizeof(InetBuff), 0);
		InetBuff.dwStructSize = sizeof(InetBuff);
		InetBuff.lpvBuffer = lpReadBuff;
		InetBuff.dwBufferLength = sizeof(lpReadBuff) - 1;

		cout << "Calling InternetReadFileEx" << endl;
		cout.flush();

		if (!InternetReadFileEx(hRequest,
			&InetBuff,
			0, 2))
		{
			if (GetLastError() == ERROR_IO_PENDING)
			{
				cout << "Waiting for InternetReadFile to complete" << endl;
				cout.flush();
				WaitForSingleObject(hRequestCompleteEvent, INFINITE);
			}
			else
			{
				cout << "InternetReadFileEx failed, error " << GetLastError();
				cout.flush();
				return;
			}
		}

		lpReadBuff[InetBuff.dwBufferLength] = 0;
		cout << lpReadBuff;
		cout.flush();

		if (InetBuff.dwBufferLength == 0) 
			bAllDone = TRUE;

	} while (bAllDone == FALSE);

	cout << endl << endl << "------------------- Request Complete ----------------" << endl;

}

void __stdcall Callback(HINTERNET hInternet,
						DWORD dwContext,
						DWORD dwInternetStatus,
						LPVOID lpStatusInfo,
						DWORD dwStatusInfoLen)
{
	cout << "Callback dwInternetStatus: " << dwInternetStatus << " Context: " << dwContext << endl;
	cout.flush();

	switch(dwContext)
	{
	case 1: // Connection handle
		if (dwInternetStatus == INTERNET_STATUS_HANDLE_CREATED)
		{
			INTERNET_ASYNC_RESULT *pRes = (INTERNET_ASYNC_RESULT *)lpStatusInfo;
			hConnect = (HINTERNET)pRes->dwResult;
			cout << "Connect handle created" << endl;
			cout.flush();
			SetEvent(hConnectedEvent);
		}
		break;
	case 2: // Request handle
		switch(dwInternetStatus)
		{
		case INTERNET_STATUS_HANDLE_CREATED:
			{
				INTERNET_ASYNC_RESULT *pRes = (INTERNET_ASYNC_RESULT *)lpStatusInfo;
				hRequest = (HINTERNET)pRes->dwResult;
				cout << "Request handle created" << endl;
				cout.flush();
			}
			break;
		case INTERNET_STATUS_REQUEST_SENT:
			{
				DWORD *lpBytesSent = (DWORD*)lpStatusInfo;
				cout << "Bytes Sent: " << *lpBytesSent << endl;
				dwNumBytesComplete += *lpBytesSent;
			}
			break;
		case INTERNET_STATUS_REQUEST_COMPLETE:
			{
				INTERNET_ASYNC_RESULT *pAsyncRes = (INTERNET_ASYNC_RESULT *)lpStatusInfo;
				cout << "Function call finished" << endl;
				cout << "dwResult: " << pAsyncRes->dwResult << endl;
				cout << "dwError:  " << pAsyncRes->dwError << endl;
				cout.flush();
				SetEvent(hRequestCompleteEvent);
			}
			break;
		case INTERNET_STATUS_RECEIVING_RESPONSE:
			cout << "Receiving Response" << endl;
			cout.flush();
			break;
		case INTERNET_STATUS_RESPONSE_RECEIVED:
			{
				DWORD *dwBytesReceived = (DWORD*)lpStatusInfo;
				cout << "Received " << *dwBytesReceived << endl;
				cout.flush();
			}

		}

	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
此示例演示在 Internet Explorer 4.0 WinInet.dll 中引入并记录在 Internet 客户端 SDK 中的 HttpSendRequestEx 函数的正确用法。 原始的 HttpSendRequest 函数有这样一个重大限制: 所有请求的数据都有一个缓冲区时调用该函数时将提供。这是通常不方便、 导致在某些客户端应用程序中,性能较差,可能会无法上载大量数据从客户端计算机使用有限的内存。新的 HttpSendRequestEx 函数允许启动请求,发送出数据分小段为可用,然后结束后已发送的所有数据的请求的程序。为了使此函数以处理计算机上必须安装 Internet Explorer 4.0。下列文件已可从 Microsoft 下载中心下载: Hsrex.exe 有关如何下载 Microsoft 支持文件的其他信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章: 119591 如何从在线服务获得 Microsoft 支持文件微软已对此文件进行病毒扫描。Microsoft 使用该文件投递日期时可用的最新的病毒检测软件。该文件存储在安全增强型服务器上,以防止对文件进行任何未经授权的更改。 Hsrex.exe 是自解压的存档文件,其中包含 BigPost.cpp (演示程序代码) 和 Readall.asp,一个 Active Server Pages (ASP) 脚本将读取所有发送 POST 请求中的数据。Readall.asp 是 BigPost,可以使用 Microsoft 互联网信息服务器 (IIS) 版本的支持 ASP 作为示例目标提供。对于其他 Web 服务器,您需要提供相应的服务器脚本来读取数据。 若要编译此程序包含在 Microsoft Visual C++ 5.0,请执行以下步骤: 1.运行 Visual C++ 和创建一个新的 Win32 控制台应用程序调用"BigPost"。 2.在目录中创建项目的位置,运行 Hsrex.exe。 3.将 BigPost.cpp 添加到 BigPost 项目。 4.转到项目设置对话框中,单击链接选项卡,然后添加到 WinInet.lib"对象/库模块:"字段。 5.请确保配置 Visual C++,以便编译器和链接器将使用 Wininet.h 和 Wininet.lib 从 Internet 客户端 SDK。如果未能做到这一点,将导致编译器或链接器错误。原型和 HttpSendRequestEx 的导出不包含 Visual C++ 中包含的包括和库文件。 6.生成项目。它将创建 BigPost.exe。 在程序运行,如下所示: BigPost < 大小 >< 服务器 >< 路径 > 例如,以下将开机自检 1 兆字节 (1024 KB) 到 http://yourserver/scripts/ReadAll.asp: 您的服务器 /scripts/ReadAll.asp BigPost 1024

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值