Windows mobile http编程

原创 2011年01月20日 10:23:00

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

相关文章推荐

Connecting through Http or Https for CE/Windows Mobile c++/vc++方法一

一:头文件#include #include #include "Afxinet.h"#include 二:.h文件public: CString sReturnXmlString; CString ...

关于windows mobile中Http 开发

1. WinInet API 是 MS 在Windows平台封装的一套 API,用来实现 HTTP,FTP 以及类似的高层协议的封装,在Winmobile平台也能使用,只是细微地方略有差别2. 一个推...

Windows Mobile编程起步.doc

  • 2011年05月11日 11:47
  • 335KB
  • 下载

Windows Mobile 6 中为开发人员提供的新功能

摘要 Windows Mobile® 6 开发人员平台版本侧重于几个核心目标:简化行业 (LOB) 应用程序开发和部署、安排构建单个应用程序的任务(该应用程序更易于在 Windows Mobil...

Windows Mobile开源地图Guidebee开发方案

开源地图Guid GitHub 地址 为 https://github.com/guidebee/  下 guidebeemap      Java库源码 guidebeemap...
  • boonya
  • boonya
  • 2014年07月30日 15:32
  • 1402

Windows Mobile编程起步

  • 2011年04月16日 14:05
  • 304KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Windows mobile http编程
举报原因:
原因补充:

(最多只允许输入30个字)