zoukankan      html  css  js  c++  java
  • 使用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 ", 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 ", 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;
    }


    阅读(1610) | 评论(0) | 转发(1) |
    给主人留下些什么吧!~~
    评论热议
  • 相关阅读:
    hdoj 2803 The MAX【简单规律题】
    hdoj 2579 Dating with girls(2)【三重数组标记去重】
    hdoj 1495 非常可乐【bfs隐式图】
    poj 1149 PIGS【最大流经典建图】
    poj 3281 Dining【拆点网络流】
    hdoj 3572 Task Schedule【建立超级源点超级汇点】
    hdoj 1532 Drainage Ditches【最大流模板题】
    poj 1459 Power Network【建立超级源点,超级汇点】
    hdoj 3861 The King’s Problem【强连通缩点建图&&最小路径覆盖】
    hdoj 1012 u Calculate e
  • 原文地址:https://www.cnblogs.com/black/p/5171954.html
Copyright © 2011-2022 走看看