Re: How to convert ftp to http
Hi,
You can try the following:
DWORD dwAccessType = PRE_CONFIG_INTERNET_ACCESS;
DWORD dwHttpRequestFlags = INTERNET_FLAG_EXISTING_CONNECT |
INTERNET_FLAG_DONT_CACHE;
CString szHeaders = "Accept: audio/x-aiff, audio/basic, audio/midi,
audio/mpeg, audio/wav, image/jpeg, image/gif, image/jpg, image/png,
image/mng, image/bmp, text/plain, text/html, text/htm\r\n";
CInternetSession* pSession =
new CInternetSession( NULL,
1,
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
0);
CHttpConnection* pHttpConn = NULL;
CHttpFile* pHttpFile = NULL;
DWORD dwFlag = 0;
pHttpConn = pSession->GetHttpConnection(someservername, 0, 8080);
pHttpFile = pHttpConn->OpenRequest("GET", "location/somefile.jpg");
pHttpFile->AddRequestHeaders(szHeaders);
pHttpFile->AddRequestHeaders("User-Agent: GetWebFile/1.0\r\n",
HTTP_ADDREQ_FLAG_ADD_IF_NEW);
pHttpFile->SendRequest();
DWORD dwRet;
pHttpFile->QueryInfoStatusCode(dwRet);//Check wininet.h for info
//about the status codes
if(dwRet == HTTP_STATUS_OK)
{
int len = pHttpFile->GetLength();
char buf[2000];
int numread;
CString filepath;
filepath = "C:\\copyfile.JPG";
CFile myfile(filepath,
CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
while ((numread = pHttpFile->Read(buf,sizeof(buf)-1)) > 0)
{
buf[numread] = '\0';
strFile += buf;
myfile.Write(buf, numread);
}
myfile.Close();
}
You will need to do some little tweaking to match your applicaiton.
Regards,
Anwar.
"Daniel" <imransheikh100@hotmail.com> wrote in message
news:1148408074.629660.207200@g10g2000cwb.googlegroups.com...
Hey every one,
I have an application that copy some files with FTP to local folder
(tmp folder).
These files are used for list of articles in tree control and some
images.
I have not much experience with C++/MFC. This small application is
made with help of someone else and that person is no more available.
Can someone help to convert this ftp part which copies the files to
local folder into http so that instead of copying these files to local
folder, the application can read the files from the website?
I will be very great full
Thanks in advance.
// Create Internet session
m_pInetSession = new CInternetSession("Application_Name");
if (!m_pInetSession)
MessageBox("Internet session could not be created", "Error",
MB_OK|MB_ICONERROR);
// Create ftp connection and download data
// Create Session Object
try
{
m_pFtpConnection = m_pInetSession->GetFtpConnection("Server_Name",
"User_Name", "Password");
}
catch (CInternetException* pEx)
{
// catch errors from WinINet
TCHAR szErr[1024];
if (pEx->GetErrorMessage(szErr, 1024))
MessageBox(szErr, "Error", MB_OK|MB_ICONERROR);
else
MessageBox("Exception occurred connecting server", "Error",
MB_OK|MB_ICONERROR);
pEx->Delete();
m_pFtpConnection = NULL;
}
// Set current directory and download data
m_pFtpConnection->SetCurrentDirectory(strRemoteDir);
// Find the file
CFileFind localFinder;
if (!localFinder.FindFile(strLocalPath))
if (!m_pFtpConnection->GetFile(File_Name, File_LocalPath))
MessageBox("Error downloading data", "Error", MB_OK|MB_ICONERROR);
localFinder.Close();
// Set the new downloaded image
newObj.img = Image::FromFile(strLocalPath.AllocSysString());
wait.Restore ();
}