使用WinINet发送HTTP请求并获取数据[原]

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;
}


<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(1610) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~
评论热议
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值