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

阅读更多
个人分类: Study
想对作者说点什么? 我来说一句

windows mobile编程

2007年12月27日 3.22MB 下载

Windows Mobile编程起步.zip

2009年04月15日 304KB 下载

Windows Mobile编程杂项笔记

2010年08月26日 30KB 下载

ppcclient windows mobile 送人了

2009年05月15日 555KB 下载

Windows mobile 6.5常用软件

2014年04月08日 1.84MB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭