目前共有2篇帖子。 內容轉換:不轉換▼
 
點擊 回復
363 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)
 

本帖信息

點擊數:363 回複數:1
評論數: ?
作者:巨大八爪鱼
最後回復:巨大八爪鱼
最後回復時間:2016-6-3 22:38
 
©2010-2024 Arslanbar Ver2.0
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。