作者共发了2篇帖子。 内容转换:不转换▼
 
点击 回复
372 1
【备份】HttpContent类
一派护法 十九级
1楼 发表于:2016-6-3 22:38
#include "stdafx.h"
#include "HttpContent.h"

LPTSTR HttpContent::lpszDefaultAgent = TEXT("Arslanbar Client");
LPTSTR HttpContent::lpszDefaultServer = TEXT("zh.arslanbar.net");
INTERNET_PORT HttpContent::nDefaultPort = INTERNET_DEFAULT_HTTP_PORT;

HttpContent::HttpContent(void)
{
    lpszAgent = lpszDefaultAgent;
    lpszServer =lpszDefaultServer;
    nPort = nDefaultPort;

    lpData = NULL;
    dwSize = 0;
}


HttpContent::~HttpContent(void)
{
    Free();
}

/* This function can be called a few times to combine several resources into one block of memory */
void HttpContent::Download(bool bTerminate)
{
    HINTERNET hInternet = InternetOpen(lpszAgent, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, NULL);
    HINTERNET hConnect = InternetConnect(hInternet, lpszServer, nPort, NULL, NULL, INTERNET_SERVICE_HTTP, NULL, NULL);
    HINTERNET hRequest = HttpOpenRequest(hConnect, NULL, lpszPage, NULL, NULL, NULL, NULL, NULL);
    HttpSendRequest(hRequest, NULL, NULL, NULL, NULL);

    DWORD dwAvailableSize;
    DWORD dwRead = 0;
    HANDLE hHeap = GetProcessHeap();
    while (InternetQueryDataAvailable(hRequest, &dwAvailableSize, NULL, NULL), dwAvailableSize > 0)
    {
        if (lpData == NULL)
            lpData = (LPBYTE)HeapAlloc(hHeap, NULL, dwAvailableSize + bTerminate);
        else
            lpData = (LPBYTE)HeapReAlloc(hHeap, NULL, lpData, dwSize + dwAvailableSize + bTerminate);
        InternetReadFile(hRequest, lpData + dwSize, dwAvailableSize, &dwRead);
        dwSize += dwAvailableSize;
    }
    if (bTerminate)
        *(lpData + dwSize) = '\0'; // dwSize doesn't include the terminating NULL
    
    InternetCloseHandle(hRequest);
    InternetCloseHandle(hConnect);
    InternetCloseHandle(hInternet);
}

/* Delete previously downloaded data */
bool HttpContent::Free(void)
{
    if (lpData == NULL)
        return false;

    HeapFree(GetProcessHeap(), NULL, lpData);
    lpData = NULL;
    dwSize = 0;
    return true;
}

LPBYTE HttpContent::GetData(void)
{
    return lpData;
}

DWORD HttpContent::GetSize(void)
{
    return dwSize;
}

/* Save the buffer to a file */
void HttpContent::Save(LPTSTR lpszFileName)
{
    FILE *fp;
    _tfopen_s(&fp, lpszFileName, TEXT("wb"));
    if (lpData != NULL)
        fwrite(lpData, dwSize, 1, fp);
    fclose(fp);
}
一派护法 十九级
2楼 发表于:2016-6-3 22:38
#pragma once
class HttpContent
{
public:
    HttpContent(void);
    ~HttpContent(void);

    static LPTSTR lpszDefaultAgent;
    static LPTSTR lpszDefaultServer;
    static INTERNET_PORT nDefaultPort;

    LPTSTR lpszAgent;
    LPTSTR lpszServer;
    INTERNET_PORT nPort;

    LPTSTR lpszPage;
    void Download(bool bTerminate = false);
    bool Free(void);
    LPBYTE GetData(void);
    DWORD GetSize(void);
    void Save(LPTSTR lpszFileName);

protected:
    LPBYTE lpData;
    DWORD dwSize;
};

回复帖子

内容:
用户名: 您目前是匿名发表
验证码:
(快捷键:Ctrl+Enter)
 

本帖信息

点击数:372 回复数:1
评论数: ?
作者:巨大八爪鱼
最后回复:巨大八爪鱼
最后回复时间:2016-6-3 22:38
 
©2010-2024 Arslanbar Ver2.0
除非另有声明,本站采用知识共享署名-相同方式共享 3.0 Unported许可协议进行许可。