1

我正在通过 eVC++ 为 windows mobile 6 编写应用程序。

我使用以下类将我的数据发送到 Web 服务。

我的问题是当我想发送数据时,首先我必须运行 Internet Explorer,在 GPRS 连接开始后,我可以使用以下类发送数据。

我想知道如何以编程方式启动 GPRS 连接。

我应该在我的代码中添加什么?

谢谢,

// GPRSConnection.cpp: implementation of the GPRSConnection class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "visitor.h"
#include "GPRSConnection.h"

#include <windows.h>
#include <commctrl.h>
#include "connmgr.h"


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

GPRSConnection::GPRSConnection()
{

}

GPRSConnection::~GPRSConnection()
{

}

void GPRSConnection::ThrowHttpException(int nCode)
{
    CInternetException* pEx = new CInternetException(nCode);
    THROW(pEx);
}

int GPRSConnection::InvokeWebServiceMethod(LPTSTR szAddress, LPTSTR strMethodName,
                                CArray<WebServiceParameter, WebServiceParameter>& parameters,
                                CString* DownloadedXML, LPTSTR* Message, LPVOID lpParameter)
{
    int   nRetCode = 0;

    *DownloadedXML = _T("");

    DWORD dwAccessType = PRE_CONFIG_INTERNET_ACCESS;
    const TCHAR szHeaders[] = _T("Accept: text/*\r\nUser-Agent: MFC_Http_Sample\r\n");
    DWORD dwHttpRequestFlags = INTERNET_FLAG_NO_AUTO_REDIRECT;
    BOOL  bSuccess = TRUE;

    CString webAddress = szAddress;
    webAddress = webAddress + _T("/") + strMethodName + _T("?");
    for(int i=0; i< parameters.GetSize(); i++)
    {
        webAddress = webAddress + parameters[i].Name + _T("=");
        webAddress = webAddress + parameters[i].Value + _T("&");
    }
    webAddress = webAddress.Left(webAddress.GetLength()-1);

    CMyHttpSession session(CString((LPCTSTR)IDS_HTTPAPP), (DWORD)lpParameter, dwAccessType);
    CHttpConnection* pServer = NULL;
    CHttpFile* pFile = NULL;
    TRY
    {
        // check to see if this is a reasonable URL
        CString strServerName;
        CString strObject;
        INTERNET_PORT nPort;
        DWORD dwServiceType;

        if (!AfxParseURL(webAddress, dwServiceType, strServerName, strObject, nPort) || 
            dwServiceType != INTERNET_SERVICE_HTTP)
        {
            *Message = _T("Error: can only use URLs beginning with http://");
            nRetCode = 100;
            ThrowHttpException(kError1);
        }

        session.EnableStatusCallback(TRUE);

        pServer = session.GetHttpConnection(strServerName, nPort);

        pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject, NULL, (DWORD)lpParameter, NULL, NULL, dwHttpRequestFlags);
        pFile->AddRequestHeaders(szHeaders);
        pFile->SendRequest();

        DWORD dwRet;
        pFile->QueryInfoStatusCode(dwRet);

        if (dwRet == HTTP_STATUS_DENIED)
        {
            nRetCode = 100;
            *Message = _T("Access to the secured http site is denied!");
             // now an error page should be downloaded      
        }

        CString strNewLocation;
        pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewLocation);

        // were we redirected?
        // these response status codes come from WININET.H

        if (dwRet == HTTP_STATUS_MOVED ||
            dwRet == HTTP_STATUS_REDIRECT ||
            dwRet == HTTP_STATUS_REDIRECT_METHOD)
        {
            CString strNewLocation;
            pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewLocation);

            int nPlace = strNewLocation.Find(_T("Location: "));
            if (nPlace == -1)
            {
                *Message = _T("Error: Site redirects with no new location");
                nRetCode = 100;
                ThrowHttpException(kError2);
            }

            strNewLocation = strNewLocation.Mid(nPlace + 10);
            nPlace = strNewLocation.Find('\n');
            if (nPlace > 0)
                strNewLocation = strNewLocation.Left(nPlace);

            // close up the redirected site

            pFile->Close();
            delete pFile;
            pServer->Close();
            delete pServer;

            // figure out what the old place was
            if (!AfxParseURL(strNewLocation, dwServiceType, strServerName, strObject, nPort))
            {
                *Message = _T("Error: the redirected URL could not be parsed");
                nRetCode = 100;
                ThrowHttpException(kError2);
            }

            if (dwServiceType != INTERNET_SERVICE_HTTP)
            {
                *Message = _T("Error: the redirected URL does not reference a HTTP resource");
                nRetCode = 100;
                ThrowHttpException(kError2);
            }

            // try again at the new location
            pServer = session.GetHttpConnection(strServerName, nPort);
            pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET,
                strObject, NULL, (DWORD)lpParameter, NULL, NULL, dwHttpRequestFlags);
            pFile->AddRequestHeaders(szHeaders);
            pFile->SendRequest();

            pFile->QueryInfoStatusCode(dwRet);
            if (dwRet != HTTP_STATUS_OK)
                ThrowHttpException(kError2);
        }

        TCHAR* szWEBPage = new TCHAR[MAX_WEBPAGE_SIZE+1];
        if(szWEBPage)
        {
            szWEBPage[0] = L'\0';

            TCHAR* sz     = new TCHAR[BUFFER_SIZE+1];
            TCHAR* szwBuf = new TCHAR[(BUFFER_SIZE+1)*2];

            sz[0] = L'\0';
            szwBuf[0] = L'\0';
            int n = 0;
            pFile->SetReadBufferSize(BUFFER_SIZE*2);
            while (pFile->ReadString(sz, BUFFER_SIZE))
            {

                //-----------------------------
                /*
                CString print;
                //print.Format(_T("%d "), szwBuf[124]);
                int adad = (int)sz[0];
                print.Format(_T("%d"), adad );
                //print.SetAt(0, sz[62]);
                MsgBox( NULL, print, B_OK, I_INFORMATION );

                    //MsgBox( NULL, str, B_OK, I_INFORMATION );
                    */
                //-----------------------------

                wce_AsciiToWide(szwBuf, (const char*)sz);           

                n += _tcslen(szwBuf);
                if(n >= MAX_WEBPAGE_SIZE)
                    break;
                _tcscat(szWEBPage, szwBuf);             
            }
            delete [] sz;
            delete [] szwBuf;
            //SetDlgItemText(IDC_EDIT1, szWEBPage);
            *DownloadedXML = szWEBPage;
        }

        /*
        CString aaa;
        aaa.Format( _T("%d"),   (*DownloadedXML).GetLength());
        MsgBox( NULL, aaa, B_OK, I_INFORMATION );
        */

        delete [] szWEBPage;
        pFile->Close();
        pServer->Close();
    }
    CATCH (CInternetException,  pEx)
    {
        // catch things wrong with parameters, etc
        if (pEx->m_dwError < kErrorMin)
        {
            TCHAR szError[MAX_PATH]=TEXT("\0");
            pEx->GetErrorMessage(szError,MAX_PATH,NULL);
            //SetDlgItemText(IDC_EDIT_PAGE, szError);
            nRetCode = 100;
            CString err = szError;
            *Message = err.GetBuffer(0);
        }
        bSuccess = FALSE;
    }
    AND_CATCH (CMemoryException,  pMemory)
    {
        // catch things wrong with memory
        //SetDlgItemText(IDC_EDIT_PAGE,CString((LPCTSTR)IDS_MEMORYEXCEPTION));
        *Message = _T("Not enough memory to download http page");
        nRetCode = 100;
        pMemory->Delete();
        bSuccess = FALSE;
    }
    END_CATCH_ALL

    if (pFile != NULL)
        delete pFile;
    if (pServer != NULL)
        delete pServer;
    session.Close();

    if(bSuccess)
    {
        *Message = _T("Completed successfully!");
    }

    return nRetCode;
}

/////////////////////////////////////////////////////////////////////////////
// CHttpSession object

// Http wants to use its own derivative of the CHttpSession class
// just so it can implement an OnStatusCallback() override.


CMyHttpSession::CMyHttpSession(LPCTSTR pszAppName, DWORD dwContext, int nMethod)
    : CInternetSession(pszAppName, dwContext, nMethod)
{
}

void CMyHttpSession::OnStatusCallback(DWORD dwContext, DWORD dwInternetStatus,
    LPVOID /* lpvStatusInfomration */, DWORD /* dwStatusInformationLen */)
{
    //CMainPage* pDlg = (CMainPage*)dwContext;
    //if(!pDlg)
        return;
/*
    switch(dwInternetStatus)
    {
        case INTERNET_STATUS_RESOLVING_NAME: 
            pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_IPLOOKING));
            break;
        case INTERNET_STATUS_NAME_RESOLVED: 
            pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_IPFOUND));
            break;
        case INTERNET_STATUS_CONNECTING_TO_SERVER: 
            pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_CONNECTING));
            break;
        case INTERNET_STATUS_CONNECTED_TO_SERVER: 
            pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_CONNECTED));
            break;
        case INTERNET_STATUS_SENDING_REQUEST: 
            pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_REQUEST));
            break;
        case INTERNET_STATUS_REQUEST_SENT: 
            pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_SENT));
            break;
        case INTERNET_STATUS_RECEIVING_RESPONSE: 
            pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_WAITING));
            break;
        case INTERNET_STATUS_RESPONSE_RECEIVED: 
            pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_RECEIVED));
            break;
        case INTERNET_STATUS_CLOSING_CONNECTION: 
            pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_CLOSING));
            break;
        case INTERNET_STATUS_CONNECTION_CLOSED: 
            pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_CLOSED));
            break;
        case INTERNET_STATUS_HANDLE_CREATED: 
            pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_CONNECTED2));
            break;
        case INTERNET_STATUS_HANDLE_CLOSING: 
            pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_DISCONNECTED));
            break;
        case INTERNET_STATUS_REQUEST_COMPLETE: 
            pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_COMPLETED));
            break;
        case INTERNET_STATUS_REDIRECT: 
            pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_REDIRECTED));
            break;
    }
    */
}
4

1 回答 1

0

这可能会有所帮助:

Windows Mobile - 连接到 Internet

另请查看本页右侧相关下的建议

于 2012-05-10T09:00:51.367 回答