httpdelegate.cpp
来自「该程序是mobile5.0 ppc2003系统下的」· C++ 代码 · 共 1,161 行 · 第 1/3 页
CPP
1,161 行
m_pConnection=NULL;
}
session.Close();
if(bSuccess)
return TRUE;
return nRetCode;
}
bool CHttpDelegate::Update(HttpParams * pHttpParams,CString currentdirectory)
{
CHttpConnection *m_pConnection ;
CHttpFile *m_pHttpFile ;
int nRetCode = 0;
DWORD dwAccessType = PRE_CONFIG_INTERNET_ACCESS;
DWORD m_dwHttpRequestFlags;
m_dwHttpRequestFlags =HSR_DOWNLOAD | INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_NO_AUTO_REDIRECT;
CMyInternetSession m_pSession(NULL, (DWORD)this, dwAccessType);
m_pSession.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,5);
BOOL bSuccess = TRUE;
TRY
{
// check to see if this is a reasonable URL
if (!AfxParseURL(pHttpParams->m_pszURL, pHttpParams->m_dwServiceType, pHttpParams->m_strServerName,
pHttpParams->m_strObject, pHttpParams->m_nPort) ||
pHttpParams->m_dwServiceType != INTERNET_SERVICE_HTTP)
{
CInternetException* pEx = new CInternetException(kError1);
THROW(pEx);
}
m_pSession.EnableStatusCallback(TRUE);
m_pConnection = m_pSession.GetHttpConnection(pHttpParams->m_strServerName, pHttpParams->m_nPort);
m_pHttpFile = m_pConnection->OpenRequest(_T("POST"),pHttpParams->m_strObject,NULL, 1, NULL, NULL,
INTERNET_FLAG_NO_AUTO_REDIRECT);
m_pHttpFile->SendRequest();
DWORD dwRet;
m_pHttpFile->QueryInfoStatusCode(dwRet);
if (dwRet == HTTP_STATUS_DENIED)
{
return 0;
}
CString str=_T("");
if(m_pHttpFile)
{
m_pHttpFile->QueryInfo(HTTP_QUERY_STATUS_CODE,str);
if(str=="404")
{
AfxMessageBox(L"文件不存在!");
m_pHttpFile->Close();
return FALSE;
}
/* if(pHttpParams->m_strObject.GetLength()-pHttpParams->m_strObject.ReverseFind(L'/')>0)
{
str = pHttpParams->m_strObject.Right(pHttpParams->m_strObject.GetLength()-pHttpParams->m_strObject.ReverseFind(L'/')-1);
}
*/
CStdioFile csf;
CString filename=_T("");
//AfxMessageBox(pHttpParams->m_filename);
//filename =_T("\\My Documents\\test3.gif");
//CreateDirectory(_T("\\My Documents"),NULL);
//int m = pHttpParams->m_filename.GetLength()-pHttpParams->m_filename.Find('\\')-1;
filename = currentdirectory + pHttpParams->m_filename.Right(pHttpParams->m_filename.GetLength()-pHttpParams->m_filename.Find('\\')-1);//+_T(".upd");
//filename += "tests.gif";
//AfxMessageBox(filename);
if(!csf.Open(filename,CFile::modeCreate|CFile::modeWrite | CFile::shareDenyWrite))
{
AfxMessageBox(L"写文件错误!\n文件正在使用中,请先关闭程序!",MB_ICONSTOP);
m_pHttpFile->Close();
return FALSE;
}
int n=0;
char buf[2048];
//AfxMessageBox(L"开始读文件");
while((n=m_pHttpFile->Read(buf,sizeof(buf)))>0)
{
csf.Write(buf,n);
}
//AfxMessageBox(L"文件下载成功!");
m_pHttpFile->Close();
}
m_pConnection->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);
}
bSuccess = FALSE;
}
AND_CATCH (CMemoryException, pMemory)
{
// catch things wrong with memor
pMemory->Delete();
bSuccess = FALSE;
}
END_CATCH_ALL
if (m_pHttpFile != NULL)
{
delete m_pHttpFile;
m_pHttpFile=NULL;
}
if (m_pConnection != NULL)
{
delete m_pConnection;
m_pConnection=NULL;
}
m_pSession.Close();
if(bSuccess)
return TRUE;
return nRetCode;
}
BOOL CHttpDelegate::DownloadPic(HttpParams * pHttpParams)
{
CHttpConnection *m_pConnection ;
CHttpFile *m_pHttpFile ;
int nRetCode = 0;
DWORD dwAccessType = PRE_CONFIG_INTERNET_ACCESS;
DWORD m_dwHttpRequestFlags;
m_dwHttpRequestFlags =HSR_DOWNLOAD | INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_NO_AUTO_REDIRECT;
CMyInternetSession m_pSession(NULL, (DWORD)this, dwAccessType);
m_pSession.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,5);
BOOL bSuccess = TRUE;
TRY
{
// check to see if this is a reasonable URL
if (!AfxParseURL(pHttpParams->m_pszURL, pHttpParams->m_dwServiceType, pHttpParams->m_strServerName,
pHttpParams->m_strObject, pHttpParams->m_nPort) ||
pHttpParams->m_dwServiceType != INTERNET_SERVICE_HTTP)
{
CInternetException* pEx = new CInternetException(kError1);
THROW(pEx);
}
m_pSession.EnableStatusCallback(TRUE);
m_pConnection = m_pSession.GetHttpConnection(pHttpParams->m_strServerName, pHttpParams->m_nPort);
m_pHttpFile = m_pConnection->OpenRequest(_T("POST"),pHttpParams->m_strObject,NULL, 1, NULL, NULL,
INTERNET_FLAG_NO_AUTO_REDIRECT);
m_pHttpFile->SendRequest();
DWORD dwRet;
m_pHttpFile->QueryInfoStatusCode(dwRet);
if (dwRet == HTTP_STATUS_DENIED)
{
return 0;
}
CString str=_T("");
if(m_pHttpFile)
{
m_pHttpFile->QueryInfo(HTTP_QUERY_STATUS_CODE,str);
if(str=="404")
{
AfxMessageBox(L"文件不存在!");
m_pHttpFile->Close();
return FALSE;
}
/* if(pHttpParams->m_strObject.GetLength()-pHttpParams->m_strObject.ReverseFind(L'/')>0)
{
str = pHttpParams->m_strObject.Right(pHttpParams->m_strObject.GetLength()-pHttpParams->m_strObject.ReverseFind(L'/')-1);
}
*/
CStdioFile csf;
CString filename=_T("");
//AfxMessageBox(pHttpParams->m_filename);
//filename =_T("\\My Documents\\test3.gif");
CreateDirectory(_T("\\My Documents\\download"),NULL);
//int m = pHttpParams->m_filename.GetLength()-pHttpParams->m_filename.Find('\\')-1;
filename = SetupPath + pHttpParams->m_filename.Right(pHttpParams->m_filename.GetLength()-pHttpParams->m_filename.Find('\\')-1);
//filename += "tests.gif";
//AfxMessageBox(filename);
if(!csf.Open(filename,CFile::modeCreate|CFile::modeWrite | CFile::shareDenyWrite))
{
AfxMessageBox(L"写文件"+str+"错误!\n文件正在使用中,请先关闭程序!",MB_ICONSTOP);
m_pHttpFile->Close();
return FALSE;
}
int n=0;
char buf[2048];
//AfxMessageBox(L"开始读文件");
while((n=m_pHttpFile->Read(buf,sizeof(buf)))>0)
{
csf.Write(buf,n);
}
//AfxMessageBox(L"文件下载成功!");
m_pHttpFile->Close();
}
m_pConnection->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);
}
bSuccess = FALSE;
}
AND_CATCH (CMemoryException, pMemory)
{
// catch things wrong with memor
pMemory->Delete();
bSuccess = FALSE;
}
END_CATCH_ALL
if (m_pHttpFile != NULL)
{
delete m_pHttpFile;
m_pHttpFile=NULL;
}
if (m_pConnection != NULL)
{
delete m_pConnection;
m_pConnection=NULL;
}
m_pSession.Close();
if(bSuccess)
return TRUE;
return nRetCode;
}
void CHttpDelegate::StrToTchar(CString temString,TCHAR * pTchar)
{
int tmpLength=0 ;
tmpLength = temString.GetLength();
TCHAR * ptemTchar = temString.GetBuffer(tmpLength);
wcscpy( pTchar,ptemTchar);
}
BOOL CHttpDelegate::readfile(CString filename)
{
CFile cf;
CFileException ex;
// BYTE buff[2048];
char buffer[2048];
strcpy(buffer,"");
//CString buffer;
strcpy(filebuffer,"");
filesize=0;
/*
CFile mFile;
if(mFile.Open(filename,CFile::modeRead | CFile::shareDenyWrite| CFile::typeBinary)==0)
return false;
CArchive ar(&mFile,CArchive::load);
ar>>buffer;
ar.Close();
filebuffer=_T("just a test");
mFile.Close();
*/
/*
FILE *stream;
// FILE *tmp;
//char filebf[40960];
int size=0;
char list[4096];
int i=0;
char *filenames;
//strcpy(filenames,"");
filenames="";
strcpy(list,"");
//strcpy(filebf,"");
AfxMessageBox(filename);
WideCharToMultiByte(CP_ACP, 0, filename, -1, filenames, filename.GetLength(), NULL, NULL);
//printf("%s",filenames);
if( (stream = fopen( filenames, "r+b" )) != NULL )
{
//filesize = fread( filebuffer, sizeof( char ), 40960, stream );
while((size = fread( list, sizeof( char ), 4096, stream ))>0)
{
for(int r=0;r<size;r++)
{
filebuffer[filesize+r]=list[r];
}
filesize+=size;
strcpy(list,"");
}
fclose( stream );
}
else
{
AfxMessageBox(_T("打开文件失败"));
}
delete filenames;
*/
int dwRead;
//filename=m_filepath;
//AfxMessageBox(filename);
if(!cf.Open(filename,CFile::modeRead | CFile::shareDenyWrite| CFile::typeBinary,&ex))
{
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
AfxMessageBox(szError);
return false;
}
else
{
while ((dwRead=cf.Read(buffer, 2048)) > 0)
{
//dwRead = cf.Read(buffer, 2048);
//strcat(filebuffer,buffer);
for(int j=0;j<dwRead;j++)
{
filebuffer[filesize+j]=buffer[j];
}
filesize+=dwRead;
strcpy(buffer,"");
}
//strcpy(filebuffer,"hi you");
//cf.Read(filebuffer, 2048);
//strcpy(filebuffer,"hello this is a test");
cf.Close();
}
return true;
}
/*
BOOL CHttpDelegate::Str2Bin (char deststr[],char* str)
{
int change=0,k=0,mask=8;
char bit=0;
char stack[40960*4]={0};
for (int i = 0;i < strlen(str);i++)
{
for(int j=0;j<4;j++)
{
stack[k++]=(mask&(str[i]-48))?49:48;
mask>>=1;
}
mask=8;
}
//return(stack);
return TRUE;
}
*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?