Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19162
  • 博文数量: 6
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-01 10:36
文章分类
文章存档

2013年(6)

我的朋友

分类: C/C++

2013-04-25 00:20:11

BwWinINet.h

#pragma once
#pragma comment(lib, "Wininet.lib")
#pragma comment(lib, "strsafe.lib")

#include <Windows.h>
#include <WinInet.h>
#include <stdio.h>
#include <strsafe.h>

/*********************************
    WinINet 封装类
*********************************/


namespace BW_Network{

    class BwWinINet
    {
    public:
        BwWinINet(void);
        virtual ~BwWinINet(void);

        UINT OpenInternet();
        
        UINT ConnectInternet(LPCSTR host, UINT port);

        UINT OpenRequest(LPCSTR reqMethod, LPCSTR reqPath);

        BOOL SendRequest(LPCTSTR heads, DWORD headLen, LPVOID optional, DWORD optLen);

        UINT GetHeadAllLength();

        UINT GetHeadSingleLength(LPSTR pHead);

        BOOL QueryHeadAll(LPSTR dest, LPDWORD length);

        BOOL QueryHeadSingle(LPSTR dest, LPCSTR headname, size_t length);

        DWORD GetDataSize();

        BOOL AcquireData(LPVOID pData, DWORD size);

        BOOL SaveFile(LPCSTR path, LPSTR buf, UINT length );

        DWORD GetLastError();

    private:
        //Internet对象指针

        HINTERNET hInternet;
    };

}



BwWinINet.cpp:

#include "BwWinINet.h"

BW_Network::BwWinINet::BwWinINet(void)
{
    hInternet = NULL;
}

BW_Network::BwWinINet::~BwWinINet(void)
{

}

UINT BW_Network::BwWinINet::OpenInternet()
{
    hInternet = ::InternetOpenA(
        "",
        INTERNET_OPEN_TYPE_DIRECT,
        NULL,
        NULL,
        0);
    return ::GetLastError();
}

UINT BW_Network::BwWinINet::ConnectInternet( LPCSTR host, UINT port )
{
    hInternet = ::InternetConnect(
        hInternet,
        host,
        port,
        "",
        "",
        INTERNET_SERVICE_HTTP,
        0,
        0);
    return ::GetLastError();
}

UINT BW_Network::BwWinINet::OpenRequest( LPCSTR reqMethod, LPCSTR reqPath )
{
    hInternet = ::HttpOpenRequest(
        hInternet,
        reqMethod,
        reqPath,
        "HTTP/1.1",
        "",
        NULL,
        0,
        0);
    return ::GetLastError();
}

BOOL BW_Network::BwWinINet::SendRequest( LPCTSTR heads, DWORD headLen, LPVOID optional, DWORD optLen )
{
    BOOL rs = ::HttpSendRequest(
        hInternet,
        heads,
        headLen,
        optional,
        optLen);
    int err = ::GetLastError();
    return rs;
}

UINT BW_Network::BwWinINet::GetHeadAllLength()
{
    DWORD len = 0;
    BOOL rs = ::HttpQueryInfo(
        hInternet,
        HTTP_QUERY_RAW_HEADERS_CRLF ,
        NULL,
        &len,
        NULL);
    return !rs && ERROR_INSUFFICIENT_BUFFER == GetLastError() ? len : -1;
}

UINT BW_Network::BwWinINet::GetHeadSingleLength( LPSTR pHead)
{
    DWORD len = 0;
    BOOL rs = ::HttpQueryInfo(
        hInternet,
        HTTP_QUERY_CUSTOM ,
        pHead,
        &len,
        NULL);
    return !rs && ERROR_INSUFFICIENT_BUFFER == GetLastError() ? len : -1;
}

BOOL BW_Network::BwWinINet::QueryHeadAll( LPSTR dest, LPDWORD length )
{
    return ::HttpQueryInfo(
        hInternet,
        HTTP_QUERY_RAW_HEADERS_CRLF ,
        dest,
        length,
        NULL);
}

BOOL BW_Network::BwWinINet::QueryHeadSingle( LPSTR dest, LPCSTR headname, size_t length )
{
    StringCchPrintf((LPSTR)dest, length, headname);
    return ::HttpQueryInfo(
        hInternet,
        HTTP_QUERY_CUSTOM ,
        dest,
        (LPDWORD)&length,
        NULL);
}

DWORD BW_Network::BwWinINet::GetDataSize()
{
    DWORD byteNums;
    ::InternetQueryDataAvailable(
        hInternet,
        &byteNums,
        0,
        0);
    return byteNums;
}

BOOL BW_Network::BwWinINet::AcquireData(LPVOID pData, DWORD size)
{
    DWORD toRead = 0;
    return ::InternetReadFile(
        hInternet,
        pData,
        size,
        &toRead);
}

BOOL BW_Network::BwWinINet::SaveFile( LPCSTR path, LPSTR buf, UINT length )
{
    FILE *pf = NULL;
    errno_t err = ::fopen_s(&pf, path,"wb");
    int wLen = fwrite(buf, sizeof(char), length, pf);
    ::fclose(pf);
    return wLen == length;
}

DWORD BW_Network::BwWinINet::GetLastError()
{
    return ::GetLastError();
}


测试:

#pragma once

#include "BwWinINet.h"

using namespace BW_Network;

int main(int argc, char* argv[])
{
    BwWinINet internet;
    internet.OpenInternet();
    
    int error;
    error = internet.ConnectInternet("127.0.0.1", 1032);

    error = internet.OpenRequest("GET", "/Data/test.rar");    ///Data/test.rar


    error = internet.SendRequest(NULL, 0, NULL, 0);

    //Query all heads
    DWORD headsLen = internet.GetHeadAllLength();
    LPSTR pHeads = new CHAR[headsLen];
    internet.QueryHeadAll(pHeads, &headsLen);
    printf("Head List:%s\n", pHeads);
    
    //Query specific head
    DWORD hLen = internet.GetHeadSingleLength("Content-Type");
    LPSTR ph = new CHAR[hLen];
    internet.QueryHeadSingle(ph, "Content-Type", hLen);
    printf("Content-Type:%s\n", ph);

    //Download file
    DWORD size = internet.GetDataSize();
    LPVOID pData = new CHAR[size];
    error = internet.AcquireData(pData, size);
    error = internet.SaveFile("T:/test.rar", (LPSTR)pData, size );

    return 0;
}


阅读(1479) | 评论(0) | 转发(0) |
0

上一篇:数据库监控的八条原则

下一篇:没有了

给主人留下些什么吧!~~