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");
posted @ 2012-05-07 10:04  大约  阅读(1054)  评论(0编辑  收藏  举报