• 大小: 28KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-06
  • 语言: C/C++
  • 标签: winhttp  异步  c++库  

资源简介

包装winhttp的c++类库,有vs的例子代码,经测试导入vs2015可以正常编译运行

资源截图

代码片段和文件信息

#include “AsyncWinHttp.h“

AsyncWinHttp::AsyncWinHttp()
: session_(NULL)
 connect_(NULL)
 request_(NULL)
 size_(0)
 totalSize_(0)
 lpBuffer_(NULL)
 cb_(NULL)
 response_raw_(NULL)
{
memset(memo_ 0 sizeof(memo_));
}

AsyncWinHttp::~AsyncWinHttp()
{
Cleanup();

if (response_raw_ != NULL)
{
delete response_raw_;
response_raw_ = NULL;
}
}

bool AsyncWinHttp::Initialize(ASYNC_WINHTTP_CALLBACK cb)
{
cb_ = cb;

session_ = WinHttpOpen( L“Asynchronous WinHTTP/1.0“ 
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY
WINHTTP_NO_PROXY_NAME
WINHTTP_NO_PROXY_BYPASS
WINHTTP_FLAG_ASYNC);

return (session_ != NULL);
}

bool AsyncWinHttp::SendRequest(LPWSTR szURL)
{
WCHAR szHost[256];
DWORD dwOpenRequestFlag = 0;
URL_COMPONENTS urlComp;
bool fRet = false;

// Initialize URL_COMPONENTS structure.
ZeroMemory(&urlComp sizeof(urlComp));
urlComp.dwStructSize = sizeof(urlComp);

// Use allocated buffer to store the Host Name.
urlComp.lpszHostName        = szHost;
urlComp.dwHostNameLength    = sizeof(szHost) / sizeof(szHost[0]);

// Set non-zero lengths to obtain pointer a to the URL Path.
// NOTE: If we threat this pointer as a null-terminated string
//       it gives us access to additional information as well. 
urlComp.dwUrlPathLength = -1;

// Crack HTTP scheme.
urlComp.dwSchemeLength = -1;

// Set the szMemo string.
swprintf_s( memo_ L“WinHttpCrackURL“);

// Crack the URL.
if (!WinHttpCrackUrl(szURL 0 0 &urlComp))
{
goto cleanup;
}

// Set the szMemo string.
swprintf_s( memo_ L“WinHttpConnect“);

// Open an HTTP session.
connect_ = WinHttpConnect(session_ szHost 
urlComp.nPort 0);
if (NULL == connect_)
{
goto cleanup;
}

// Prepare OpenRequest flag
dwOpenRequestFlag = (INTERNET_SCHEME_HTTPS == urlComp.nScheme) ? WINHTTP_FLAG_SECURE : 0;

// Set the szMemo string.
swprintf_s( memo_ L“WinHttpOpenRequest“);

// Open a “GET“ request.
request_ = WinHttpOpenRequest(connect_ 
L“GET“ urlComp.lpszUrlPath
NULL WINHTTP_NO_REFERER 
WINHTTP_DEFAULT_ACCEPT_TYPES
dwOpenRequestFlag);

if (request_ == 0)
{
goto cleanup;
}

// Set the szMemo string.
swprintf_s( memo_ L“WinHttpSetStatusCallback“);

// Install the status callback function.
WINHTTP_STATUS_CALLBACK pCallback = WinHttpSetStatusCallback(request_
(WINHTTP_STATUS_CALLBACK)AsyncCallback
WINHTTP_CALLBACK_FLAG_ALL_COMPLETIONS | 
WINHTTP_CALLBACK_FLAG_REDIRECT    
NULL);

// note: On success WinHttpSetStatusCallback returns the previously defined callback function.
// Here it should be NULL
if (pCallback != NULL)
{
goto cleanup;
}

// Set the szMemo string.
swprintf_s( memo_ L“WinHttpSendRequest“);

// Send the request.
if (!WinHttpSendRequest(request_ 
WINHTTP_NO_ADDITIONAL_HEADERS 0 
WINHTTP_NO_REQUEST_DATA 0 0 
(DWORD_PTR)this))
{
goto cleanup;
}

fRet = true;

cleanup

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2009-12-11 15:57  winhttp_async\
     目录           0  2009-12-11 15:57  winhttp_async\Debug\
     文件       51200  2009-12-11 16:18  winhttp_async\Debug\winhttp_async.exe
     目录           0  2009-12-11 16:18  winhttp_async\winhttp_async\
     文件       11848  2009-12-11 15:31  winhttp_async\winhttp_async\AsyncWinHttp.cpp
     文件        1868  2009-12-11 10:47  winhttp_async\winhttp_async\AsyncWinHttp.h
     文件        1338  2009-12-10 16:20  winhttp_async\winhttp_async\ReadMe.txt
     文件         300  2009-12-10 16:20  winhttp_async\winhttp_async\stdafx.cpp
     文件         294  2009-12-11 15:19  winhttp_async\winhttp_async\stdafx.h
     文件         830  2009-12-11 16:18  winhttp_async\winhttp_async\test.cpp
     文件        4643  2009-12-11 15:57  winhttp_async\winhttp_async\winhttp_async.vcproj
     文件         905  2009-12-10 16:20  winhttp_async\winhttp_async.sln
     文件       23040  2009-12-11 16:18  winhttp_async\winhttp_async.suo

评论

共有 条评论