windows mobile下的MFC程序,怎么用http上传文件?
在一个MFC程序中,需要向服务器上传文件。 参考了一个windows xp下的程序,可以正常运行的,但是代码移植到windows mobile下就不行,主要是wm不支持这几个函数:CInternetFile::Write
CHttpFile::SendRequestEx
CHttpFile::EndRequest
请问,该怎么办? 有什么方法可以实现这个功能吗? 谢谢
另外,发觉有很多pc机里能用的win api函数,在windows mobile下用不了,不知道微软是出于什么考虑,要禁用这些函数。
--------------------编程问答-------------------- http://topic.csdn.net/u/20091214/09/14BB324B-9D9E-4C1D-8417-0AF1537145E1.html
就是通过socket上传 --------------------编程问答-------------------- 拿PPC跟PC比,楼主牛。 --------------------编程问答-------------------- winInet, 开发文档上有例子。。很简单
HRESULT CHttpConnect::DownloadFile(const TCHAR * lpURL ,
const TCHAR * lpFile,
DOWNLOADPROC lpProc ,
WPARAM lpUserData ,
DWORD dwTimeOut,
BOOL bUseProxy )
{
m_eCurReqStatus = ENoOperate;
m_bStopMission = FALSE;
HRESULT bRet = S_FALSE;
HINTERNET hSession = NULL;
HINTERNET hConnect = NULL;
BOOL bCheckFeePage = bUseProxy;
char * lpBufferA = new CHAR [READ_CHUCK_SIZE+1];
if (NULL==lpBufferA)
{
return bRet;
}
TCHAR szServer[MAX_PATH] = {0};
TCHAR szPath[MAX_PATH] = {0};
URL_COMPONENTS url = {0};
url.dwStructSize = sizeof(URL_COMPONENTS);
url.lpszHostName = szServer;
url.dwHostNameLength = MAX_PATH;
url.lpszUrlPath = szPath;
url.dwUrlPathLength = MAX_PATH;
url.nScheme = INTERNET_SCHEME_HTTP;
if (!InternetCrackUrl(lpURL,_tcslen(lpURL),0,&url))
goto _DownloadEnd;
if (lpProc)
lpProc(heStartTask,(LPARAM)lpURL,lpUserData);
m_eCurReqStatus = EInternetOpening;
if (bUseProxy)
{
hSession=::InternetOpen(TEXT("Profile/MIDP-1.0 Configuration/CLDC-1.0"),INTERNET_OPEN_TYPE_PROXY,_T("10.0.0.172:80"),NULL,INTERNET_FLAG_ASYNC);
}
else
{
hSession=::InternetOpen(TEXT("Profile/MIDP-1.0 Configuration/CLDC-1.0"),INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,INTERNET_FLAG_ASYNC);
}
if(!hSession || InternetSetStatusCallback(hSession, InetCallback) == INTERNET_INVALID_STATUS_CALLBACK)
{
if (lpProc)
lpProc(heError,(LPARAM)::GetLastError(),lpUserData);
goto _DownloadEnd;
}
if( m_bStopMission)
{
bRet = -1;
goto _DownloadEnd;
}
DWORD dwSize = 0;
TCHAR szTmpFilename[MAX_PATH] ={0};
lstrcpy(szTmpFilename,lpFile);
lstrcat(szTmpFilename,L".zip");
TCHAR lpRealURL[MAX_PATH] ={0};
lstrcpy(lpRealURL,lpURL);
_Request:
{
::ResetEvent(m_hRequestCompleteEvent);
m_eCurReqStatus = EHttpSendRequesting;
TCHAR szHead[] = TEXT("Accept: */*\r\n\r\n");
if (!(m_hRequest = InternetOpenUrl( hSession,
lpRealURL,
szHead ,
lstrlen (szHead),
INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE, (DWORD)this)))
{
if(GetLastError() != ERROR_IO_PENDING || WaitForSingleObject(m_hRequestCompleteEvent, KConnTimeOut) != WAIT_OBJECT_0)
{
if (lpProc)
lpProc(heError,(LPARAM)::GetLastError(),lpUserData);
goto _DownloadEnd;
}
}
if(m_bStopMission)
{
bRet = -1;
goto _DownloadEnd;
}
DWORD dwStatusCode;
dwSize = sizeof(dwStatusCode);
if (!m_hRequest || !::HttpQueryInfo(m_hRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER , &dwStatusCode,&dwSize, NULL))
{
if (lpProc)
lpProc(heError,(LPARAM)::GetLastError(),lpUserData);
goto _DownloadEnd;
}
if (dwStatusCode < HTTP_STATUS_OK || dwStatusCode > HTTP_STATUS_PARTIAL_CONTENT) //获取返回状态码
{
if (416==dwStatusCode)
{
bRet = S_OK;
goto _DownloadEnd;
}
else if (302==dwStatusCode)
{
CString strNewLocation;
::HttpQueryInfo(m_hRequest,HTTP_QUERY_LOCATION,NULL,&dwSize,NULL);
if (!m_hRequest || ::HttpQueryInfo(m_hRequest, HTTP_QUERY_LOCATION ,strNewLocation.GetBufferSetLength(dwSize),&dwSize,NULL))
{
lstrcpy(lpRealURL,strNewLocation.GetString());
strNewLocation.ReleaseBuffer(dwSize);
if (m_hRequest)
{
InternetCloseHandle (m_hRequest);
}
goto _Request;
}
}
if (lpProc)
lpProc(heError,(LPARAM)::GetLastError(),lpUserData);
goto _DownloadEnd;
}
TCHAR ContentType[MAX_PATH];
dwSize = MAX_PATH;
if (bCheckFeePage && HttpQueryInfo (m_hRequest, HTTP_QUERY_CONTENT_TYPE, (LPVOID) ContentType, &dwSize, NULL))
{
if ( _tcsnicmp(TEXT("text/vnd.wap.wml;"),ContentType,lstrlen(TEXT("text/vnd.wap.wml;"))) == 0) //资费页面
{
while (m_hRequest && InternetReadFile (m_hRequest, (LPVOID)lpBufferA, READ_CHUCK_SIZE, &dwSize));
{
Sleep(80);
}
bCheckFeePage = FALSE;
goto _Request;
}
}
if (lpProc)
lpProc(heQueryInfo,dwSize,lpUserData);
}
DWORD writes=0;
HANDLE hFile = CreateFile(szTmpFilename,GENERIC_WRITE,FILE_SHARE_WRITE|FILE_SHARE_READ,NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(INVALID_HANDLE_VALUE == hFile)
{
if (lpProc)
lpProc(heError,(LPARAM)::GetLastError(),lpUserData);
goto _DownloadEnd;
}
do
{
::ResetEvent(m_hRequestCompleteEvent);
m_eCurReqStatus = EInternetReadFileing;
if (!m_hRequest || !InternetReadFile (m_hRequest, (LPVOID)lpBufferA, READ_CHUCK_SIZE, &dwSize))
{
if(!m_hRequest || GetLastError() != ERROR_IO_PENDING || WaitForSingleObject(m_hRequestCompleteEvent,KConnTimeOut) != WAIT_OBJECT_0)
{
if (lpProc)
lpProc(heError,(LPARAM)::GetLastError(),lpUserData);
goto _DownloadEnd;
}
}
if(m_bStopMission)
{
bRet = -1;
goto _DownloadEnd;
}
if (dwSize == 0)
{
::DeleteFile(lpFile);
::MoveFile( szTmpFilename, lpFile);
bRet = S_OK;
}
else
{
if (lpProc)
lpProc(heTaskData,(LPARAM)dwSize,lpUserData);
::WriteFile(hFile,lpBufferA,dwSize,&writes,NULL);
}
Sleep(100);
} while (dwSize);
_DownloadEnd:
m_eCurReqStatus = ENoOperate;
if(INVALID_HANDLE_VALUE != hFile)
{
CloseHandle(hFile);
if (S_OK!=bRet)
{
::DeleteFile(szTmpFilename);
}
}
if (lpProc)
lpProc(heEndTask,(LPARAM)lpURL,lpUserData);
if (lpBufferA)
{
delete [] lpBufferA;
}
if (m_hRequest)
{
TRACE0("...InternetCloseHandle(m_hRequest)..\r\n");
InternetCloseHandle (m_hRequest);
m_hRequest = NULL;
}
if (hSession)
{
TRACE0("...InternetCloseHandle(hSession)..\r\n");
InternetSetStatusCallback(hSession, NULL);
InternetCloseHandle (hSession);
}
TRACE0("...return...\r\n");
return bRet;
}
--------------------编程问答-------------------- 一段代码,详细的查文档
补充:移动开发 , Windows Phone