bool  CTCSDlg::UpFileOfHttp(TCHAR* strSourceName,TCHAR* strDestName,TCHAR* _strFtpSite)
{
TCHAR* pName=new TCHAR[256];
wsprintf(pName,L"%s?filename=%s",m_strFtpSite,strDestName);
AfxOutInfoToFile("===上传文件名:",strSourceName);
AfxOutInfoToFile("===上传地址:",pName);
ASSERT(m_strFtpSite != "" && strSourceName != "");
BOOL bResult = FALSE;
DWORD dwType = 0;
CString strServer;
CString strObject;
INTERNET_PORT wPort = 80;
DWORD dwFileLength = 0;
char * pFileBuff = NULL;
CHttpConnection * pHC = NULL;
CHttpFile * pHF = NULL;
CInternetSession cis;
bResult =  AfxParseURL(pName, dwType, strServer, strObject, wPort);
AfxOutInfoToFile(L"===解析的服务器:",strServer.GetBuffer(strServer.GetLength()));
AfxOutInfoToFile(L"===解析的地址:",strObject.GetBuffer(strObject.GetLength()));
//AfxOutInfoToFile("===解析的地址:",m_strFtpSite);
if(!bResult)
return false;
CFile file;
try
{
if(!file.Open(strSourceName, CFile::shareDenyNone | CFile::modeRead))
return false;
dwFileLength = file.GetLength();
if(dwFileLength <= 0)
return false;
pFileBuff = new char[dwFileLength];
memset(pFileBuff, 0, sizeof(char) * dwFileLength);
file.Read(pFileBuff, dwFileLength);
const int nTimeOut = 5000;
cis.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut); //联接超时设置
cis.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);  //重试1次
pHC = cis.GetHttpConnection(strServer, wPort);  //取得一个Http联接
pHF = pHC->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject);
if(!pHF->SendRequest(NULL, 0, pFileBuff, dwFileLength))
{
delete[]pFileBuff;
pFileBuff = NULL;
pHF->Close();
pHC->Close();
cis.Close();
AfxOutInfoToFile(L"上传中,SendRequest error\n");
return FALSE;
}
CString szError=L"0";
char szBuff[1024];
UINT nRead;
while (nRead = pHF->Read(szBuff,1023))
{
szError=CString(szBuff,nRead);
AfxOutInfoToFile(L"返回值:",szError.GetBuffer(szError.GetLength()));
}
/*
DWORD  dwError,  dwBufferLength; 
TCHAR szError[256]={_T("0")};
while(pHF->Read(szError,256))
{
lstrcpy(szError,L"error");
AfxOutInfoToFile(L"返回的值 :",szError);
}
*/
/
/if(lstrcmp(_T("0"),szError)==0)
// {
//  return false;
// }
DWORD dwStateCode = 0;
pHF->QueryInfoStatusCode(dwStateCode);
if(dwStateCode == HTTP_STATUS_OK)
bResult = TRUE;
else
{
AfxOutInfoToFile(L"上传中,服务器返回的代码为");
return false;
}
// AfxOutInfoToFile(L"上传中,服务器返回的代码为");
// AfxOutInfoToFile(L"上传中,服务器返回的代码为");
}
catch(...)
{
char sz[256] = "";
// pEx->GetErrorMessage(sz, 25);
CString str;
// str.Format("InternetException occur!\r\n%s", sz);
/
/AfxMessageBox(str);
// g_writelog.WriteLine("上传中,%s",str);
}
delete[]pFileBuff;
p
FileBuff = NULL;
file.Close();
pHF->Close();
pHC->Close();
cis.Close();
}
************************************************************************
上传
UploadFile(LPCTSTR strURL, //负责接收上传操作的页面的URL
LPCTSTR strLocalFileName)  //待上传的本地文件路径
{
ASSERT(strURL != NULL && strLocalFileName != NULL);
BOOL bResult = FALSE;
DWORD dwType = 0;
CString strServer;
CString strObject;
INTERNET_PORT wPort = 0;
DWORD dwFileLength = 0;
char * pFileBuff = NULL;
CHttpConnection * pHC = NULL;
CHttpFile * pHF = NULL;
CInternetSession cis;
bResult =  AfxParseURL(strURL, dwType, strServer, strObject, wPort);
if(!bResult)
return FALSE;
CFile file;
try
{
if(!file.Open(strLocalFileName, CFile::shareDenyNone | CFile::modeRead))
return FALSE;
dwFileLength = file.GetLength();
if(dwFileLength <= 0)
return FALSE;
pFileBuff = new char[dwFileLength];
memset(pFileBuff, 0, sizeof(char) * dwFileLength);
file.Read(pFileBuff, dwFileLength);
const int nTimeOut = 5000;
cis.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut); //联接超时设置
cis.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);  //重试1次
pHC = cis.GetHttpConnection(strServer, wPort);  //取得一个Http联接
pHF = pHC->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject);
if(!pHF->SendRequest(NULL, 0, pFileBuff, dwFileLength))
{
delete[]pFileBuff;
pFileBuff = NULL;
pHF->Close();
pHC->Close();
cis.Close();
return FALSE;
}
DWORD dwStateCode = 0;
pHF->QueryInfoStatusCode(dwStateCode);
if(dwStateCode == HTTP_STATUS_OK)
bResult = TRUE;
}
catch(CInternetException * pEx)
{
char sz[256] = "";
pEx->GetErrorMessage(sz, 25);
CString str;
str.Format("InternetException occur!\r\n%s", sz);
AfxMessageBox(str);
}
catch(CFileException& fe)
{
CString str;
str.Format("FileException occur!\r\n%d", fe.m_lOsError);
AfxMessageBox(str);
}
catch(...)
{
DWORD dwError = GetLastError();
CString str;
str.Format("Unknow Exception occur!\r\n%d", dwError);
AfxMessageBox(str);
}
delete[]pFileBuff;
pFileBuff = NULL;
file.Close();
pHF->Close();
pHC->Close();
cis.Close();
return bResult;
}
下载
bool Download(const CString& strFileURLInServer, //待下载文件的URL
const CString & strFileLocalFullPath)//存放到本地的路径
{
ASSERT(strFileURLInServer != "");
ASSERT(strFileLocalFullPath != "");
CInternetSession session;
CHttpConnection* pHttpConnection = NULL;
CHttpFile* pHttpFile = NULL;
CString strServer, strObject;
INTERNET_PORT wPort;
bool bReturn = false;
DWORD dwType;
const int nTimeOut = 2000;
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut); //重试之间的等待延时
session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1); //重试次数
char* pszBuffer = NULL;
try
{
AfxParseURL(strFileURLInServer, dwType, strServer, st
rObject, wPort);
pHttpConnection = session.GetHttpConnection(strServer, wPort);
pHttpFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject);
if(pHttpFile->SendRequest() == FALSE)
return false;
DWORD dwStateCode;
pHttpFile->QueryInfoStatusCode(dwStateCode);
if(dwStateCode == HTTP_STATUS_OK)
{
HANDLE hFile = CreateFile(strFileLocalFullPath, GENERIC_WRITE,
FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
NULL); //创建本地文件
if(hFile == INVALID_HANDLE_VALUE)
{
pHttpFile->Close();
pHttpConnection->Close();
session.Close();
return false;
}
char szInfoBuffer[1000]; //返回消息
DWORD dwFileSize = 0; //文件长度
DWORD dwInfoBufferSize = sizeof(szInfoBuffer);
BOOL bResult = FALSE;
bResult = pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH,
(void*)szInfoBuffer,&dwInfoBufferSize,NULL);
dwFileSize = atoi(szInfoBuffer);
const int BUFFER_LENGTH = 1024 * 10;
pszBuffer = new char[BUFFER_LENGTH]; //读取文件的缓冲
DWORD dwWrite, dwTotalWrite;
dwWrite = dwTotalWrite = 0;
UINT nRead = pHttpFile->Read(pszBuffer, BUFFER_LENGTH); //读取服务器上数据
while(nRead > 0)
{
WriteFile(hFile, pszBuffer, nRead, &dwWrite, NULL); //写到本地文件
dwTotalWrite += dwWrite;
nRead = pHttpFile->Read(pszBuffer, BUFFER_LENGTH);
}
delete[]pszBuffer;
pszBuffer = NULL;
CloseHandle(hFile);
bReturn = true;
}
}
catch(CInternetException* e)
{
TCHAR tszErrString[256];
e->GetErrorMessage(tszErrString, ArraySize(tszErrString));
TRACE(_T("Download XSL error! URL: %s,Error: %s"), strFileURLInServer, tszErrString);
e->Delete();
}
catch(...)
{
}
if(pszBuffer != NULL)
{
delete[]pszBuffer;
}
if(pHttpFile != NULL)
{
pHttpFile->Close();
delete pHttpFile;
}
if(pHttpConnection != NULL)
{
pHttpConnection->Close();
delete pHttpConnection;
}
session.Close();
return bReturn;
} error parse new

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。