Windows mobile http编程

http.h

typedef struct S_HTTP_Res_DATA
{
    unsigned char *data;
    unsigned long dataLen;
}HTTP_Res_DATA;

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

    static int HttpConnectUrl(CString url, HTTP_Res_DATA *hData);
    static int HttpConnectUrl(CString url, CString filePath);
    static int ReleaseHttpData(HTTP_Res_DATA *hData);
    static int HttpDownloadFile(CString url, CString filePath);
    static int WInetHttpDownloadFile(CString url, CString filePath);
};

 

http.cpp

#include "StdAfx.h"
#include "Http.h"

CHttp::CHttp(void)
{
}

CHttp::~CHttp(void)
{
}

#include <afxinet.h>
void HttpExceptionHandle(CInternetException *e)
{
    TCHAR buf[255];
    memset(buf, 0, sizeof(buf));

    e->GetErrorMessage(buf, 255);
    TRACE(_T("Exception message: %s/n"), buf);
}
/************************************************************************/
/*Function Name: HttpConnectUrl
/*Input parameters:
/*    url: specify the url to connect.
/*Output parameters:
/*    hData: store the response data returned from url.
/*Return values:
/*    0: success.
/*    -1: got some error.
/*Remarks:
/*    The method ReleaseHttpData must be called after call to this method
/*    to release the memory resources.
/************************************************************************/
int CHttp::HttpConnectUrl(CString url, HTTP_Res_DATA *hData)
{
    CInternetSession inetSess;
    CHttpFile *httpFile = NULL;

    try
    {
        httpFile = (CHttpFile *)inetSess.OpenURL(url);
    }
    catch(CInternetException *e)
    {
        TRACE(_T("---InternetException in HttpConnectUrl---/n"));
        HttpExceptionHandle(e);
        e->Delete();
        inetSess.Close();
        delete httpFile;
return -1;
    }

    ULONGLONG fileSize = httpFile->GetLength();
    hData->data = (UCHAR *)malloc((size_t)(fileSize + 1));
    memset(hData->data, 0, fileSize + 1);
    hData->dataLen = fileSize;

    UCHAR tempBuf[1024];
    int rLen, i = 0;
    while(rLen = httpFile->Read(tempBuf, 1024))
    {
        memcpy(hData->data + i, tempBuf, rLen);
        i += rLen;
    }
    hData->data[i] = '/0';

    delete httpFile;
    inetSess.Close();

return 0;
}
/************************************************************************/
/*Function Name: HttpConnectUrl
/*Input parameters:
/*    url: specify the url to connect.
/*    filePath: specify where to store the xml data.
/*Return values:
/*    0: success.
/*    -1: got some error.
/*Remarks:
/*    The method ReleaseHttpData must be called after call to this method
/*    to release the memory resources.
/************************************************************************/
int CHttp::HttpConnectUrl(CString url, CString filePath)
{
    CInternetSession inetSess;
    CHttpFile *httpFile = NULL;

    try
    {
        httpFile = (CHttpFile *)inetSess.OpenURL(url);
    }
    catch(CInternetException *e)
    {
        TRACE(_T("---InternetException in HttpConnectUrl---/n"));
        HttpExceptionHandle(e);
        e->Delete();
        inetSess.Close();
        delete httpFile;
        return -1;
    }

    CFile file;
    file.Open(filePath, CFile::modeCreate | CFile::modeWrite);

#define RW_BLOCK_SIZE 1024

    unsigned char buf[RW_BLOCK_SIZE];
    memset(buf, 0, sizeof(buf));
    int rLen;
    while(rLen = httpFile->Read(buf, RW_BLOCK_SIZE))
    {
        file.Write(buf, rLen);
        memset(buf, 0, sizeof(buf));
    }
    file.Close();

    delete httpFile;
    inetSess.Close();

    return 0;
}
/************************************************************************/
/*Function name: ReleaseHttpData
/*Input parameters:
/*
/*Output parameters:
/*    hData: specify the pointer to the useless HTTP_Res_DATA.
/************************************************************************/
int CHttp::ReleaseHttpData(HTTP_Res_DATA *hData)
{
    free(hData->data);

return 0;
}

#define RW_BLOCK_SIZE 1024
/************************************************************************/
/*Function Name: HttpDownloadFile
/*Input parameters:
/*    url: specify which file to download.
/*    filePath: specify store the file downloaded to where.
/*Output parameters:
/*Return values:
/*    0: success.
/*    -1: got some error.
/*Remarks:
/************************************************************************/
int CHttp::HttpDownloadFile(CString url, CString filePath)
{
    CInternetSession inetSess;
    CHttpFile *httpFile = NULL;

    try
    {
        httpFile = (CHttpFile *)inetSess.OpenURL(url, 1, INTERNET_FLAG_TRANSFER_BINARY);
        //httpFile = (CHttpFile *)inetSess.OpenURL(url, 1, INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_DONT_CACHE);
    }
    catch (CInternetException *e)
    {
        TRACE(_T("---InternetException in HttpDownloadFile---/n"));
        HttpExceptionHandle(e);
        e->Delete();
        inetSess.Close();
        delete httpFile;
return -1;
    }
   
    CFile file;
    file.Open(filePath, CFile::modeCreate | CFile::modeWrite);

    unsigned char buf[RW_BLOCK_SIZE];
    memset(buf, 0, sizeof(buf));
    int rLen;
    while(rLen = httpFile->Read(buf, RW_BLOCK_SIZE))
    {
        file.Write(buf, rLen);
        memset(buf, 0, sizeof(buf));
    }
    file.Close();

    delete httpFile;
    inetSess.Close();

return 0;
}
/************************************************************************/
/*Function Name: WInetHttpDownloadFile
/*Input parameters:
/*    url: specify which file to download.
/*    filePath: specify store the file downloaded to where.
/*Output parameters:
/*Return values:
/*    0: success.
/*    -1: got some error.
/*Remarks: GPRS、WIFI
/************************************************************************/
#include <WinInet.h>
int CHttp::WInetHttpDownloadFile(CString url, CString filePath)
{
    //Tells the Internet DLL to initialize internal data structures.
    HINTERNET hInternet = InternetOpen(_T("THTTP"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, NULL);
    if(!hInternet)
    {
return -1;
    }
    //Retrieve data corresponding to a URL.
    HINTERNET hRequest = InternetOpenUrl(hInternet, url, NULL, 0, INTERNET_FLAG_EXISTING_CONNECT, 0);
    if(!hRequest)
    {
        InternetCloseHandle(hInternet);
return -1;
    }

    CFile file;
    file.Open(filePath, CFile::modeCreate | CFile::modeWrite);
    wchar_t wbuf[RW_BLOCK_SIZE] = {0};
    DWORD dwRead = 0;
    while(InternetReadFile(hRequest, wbuf, RW_BLOCK_SIZE, &dwRead))
    {
        file.Write(wbuf, dwRead);
        memset(wbuf, 0, sizeof(wbuf));
        if(dwRead < RW_BLOCK_SIZE)
        {
    break;
        }
    }
    file.Flush();
    file.Close();

    InternetCloseHandle(hRequest);
    InternetCloseHandle(hInternet);

return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值