• HttpWorker


    #include "http://www.cnblogs.com/src/network/http.h"
    
    
    void WinHttpWorker::HttpCreateSession(LPCWSTR agent)
    {
        _hSession=WinHttpOpen(agent,WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,WINHTTP_NO_PROXY_NAME,WINHTTP_NO_PROXY_BYPASS,0);
    
        if(_hSession)
        {
            printf("%s", "_hSession\n");
        }
    }
    
    void WinHttpWorker::HttpCreateConnect(LPCWSTR ipaddr, int serverPort)
    {
        _hConnect=WinHttpConnect(_hSession,ipaddr,serverPort,0);
    
            if(_hConnect)
        {
            printf("%s", "_hConnect\n");
        }
    }
    
    void WinHttpWorker::HttpCreateRequest(LPCWSTR verb,LPCWSTR object)
    {
        _hRequest=WinHttpOpenRequest(_hConnect,verb,object,L"HTTP/1.1", WINHTTP_NO_REFERER,WINHTTP_DEFAULT_ACCEPT_TYPES,0);
    
                if(_hRequest)
        {
            printf("%s", "_hRequest\n");
        }
    }
    
    void WinHttpWorker::HttpAddRequestHeaders(LPCWSTR header)
    {
        SIZE_T length = lstrlenW(header);
    
        WinHttpAddRequestHeaders(_hRequest,header,DWORD(length), WINHTTP_ADDREQ_FLAG_ADD);
    
        if(WinHttpAddRequestHeaders)
        {
            printf("%s", "WinHttpAddRequestHeaders\n");
        }
    }
    
    void WinHttpWorker::HttpSendRequest(const void *data, int length)
    {
                
            _bResult=WinHttpSendRequest(_hRequest, 0, 0,const_cast<void *>(data),length, length, 0 );
    
    
            if(_bResult)
        {
            printf("%s", "WinHttpSendRequest\n");
        }
    }
    
    void WinHttpWorker::HttpReceiveResponse()
    {
        _bResult=WinHttpReceiveResponse(_hRequest,NULL);
    
                if(_bResult)
        {
            printf("%s", "WinHttpReceiveResponse\n");
        }
    }
    
    void WinHttpWorker:: HttpReadData(void *buffer, DWORD length, DWORD *read)
    {
        WinHttpReadData(_hRequest, buffer, length, read);
    }
    
    void WinHttpWorker::HttpQueryData(DWORD *size)
    {    
        _bResult=WinHttpQueryDataAvailable(_hRequest, size);
    
        if(_bResult)
        {
            printf("%s", "WinHttpQueryDataAvailable\n");
        }
    
        else
        {
    printf("error\n");
        }
    }
    
    WinHttpWorker::~WinHttpWorker()
    {
        if (_hRequest) WinHttpCloseHandle(_hRequest);
        if (_hConnect) WinHttpCloseHandle(_hConnect);
        if (_hSession) WinHttpCloseHandle(_hSession);
    }
                            WinHttpWorker * http=new WinHttpWorker();
    
                            http->HttpCreateSession(L"User-Agent");
    
                            http->HttpCreateConnect(L"192.168.0.8",80);
    
                            http->HttpCreateRequest(L"POST",L"login.html");
    
                            http->HttpAddRequestHeaders(L"Content-type:application/x-www-form-urlencoded/r/n");
    
                            std::string data="name=xx11sad&sign=xx11sad";
    
                            http->HttpSendRequest((const char *)data.c_str(),data.length());
    
                            http->HttpReceiveResponse();
    
                            LPSTR buffer;
    
                            DWORD dwSize = 0;
    
                            http->HttpQueryData(&dwSize);
    
                            printf("%i",dwSize);
    
                            buffer = new char[dwSize+1];
                            
                            ZeroMemory(buffer,dwSize+1);
    
                            http->HttpReadData(buffer,dwSize,&dwSize);
    
                            
                             printf("%s", buffer);
    
                            delete [] buffer;
    
    
                             system("pause");
  • 相关阅读:
    Windows Server2012R2 添加Microsoft .NET Framework 3.5 功能失败的解决方法
    Windows Server2012R2 安装 SharePoint 2013 的必备组件
    pig加载两个不同字段个数的文件?load file with different items(f1有42列,f2有43列读到一个对象中)
    正则表达式的子模式详解
    PHP 递归函数的理解
    仿照美食杰tab选项卡
    tab简单选项卡
    PHP字符串变驼峰方法
    PHP笔试题
    PHP面试题集
  • 原文地址:https://www.cnblogs.com/ytjjyy/p/2486972.html
Copyright © 2020-2023  润新知