vc 下载http 图片

//下载
#include    <wininet.h> 
#pragma   comment(lib, "wininet.lib ")

void   Down(CString   Url,CString   Path)
{
HINTERNET   hSession=InternetOpen(NULL,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
HINTERNET   hConnection=InternetOpenUrl(hSession,Url,NULL,0,0,0);
if(hConnection   ==   NULL)return   FALSE;//返回
BYTE   Buffer[4096];
DWORD   Read=0,i;
HANDLE   hFile=CreateFile(Path,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,0,NULL);
InternetReadFile(hConnection,Buffer,sizeof(Buffer),&Read);
while(Read!=0)
{
WriteFile(hFile,Buffer,Read,&i,NULL);
InternetReadFile(hConnection,Buffer,sizeof(Buffer),&Read);
}
CloseHandle(hFile);

InternetCloseHandle(hConnection);
InternetCloseHandle(hSession);
return   TRUE;

}



上传

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, strObject, 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;





类似网络爬虫,从一个网页“爬”到另一个网页,然后选择图片下载。多线程。 可以用来按照一定规则下载网页中的元素,如图片、网页、flash等,举例如下: 1. 下载sohu主页的所有图片 在地址栏中输入www.sohu.com,在“选项”中将最大下载图片数目设为0,最大访问网页数设为1,点开始即可。 2. 下载sohu主页及其子链接中的所有图片,共下载100张 在“选项”中将最大下载图片数目设为100,最大访问网页数设为0 3. 下载人人网中的相册 打开相册中的一张相片,如http://photo.renren.com/getphoto.do?id=975410152&owner=230410031&curpage=0&t=#975410152,将地址复制到软件的地址栏(注意要将窗口拉长,使得地址栏足够容下这个地址,否则地址会被切断,这个bug我一直没找到解决办法)。 接下来需要一点观察。先看要下载图片链接(http://fmn017.xnimg.cn/fmn017/pic001/20080926/17/21/large_wJZc_3213f200058.jpg),再看下一张,是http://fmn014.xnimg.cn/fmn014/pic001/20080926/17/21/large_Ao10_3208l200058.jpg,找到其中的公共部分,不妨取为xnimg.cn/fmn,将其输入“选项”中的“图片路径含有”,并勾选复选框。 再观察“上一张”、“下一张”的链接(http://photo.renren.com/getphoto.do?id=975410152&owner=230410031&curpage=0&t=#975409483)(http://photo.renren.com/getphoto.do?id=975410152&owner=230410031&curpage=0&t=#975410114),取其公共部分photo.renren.com/getphoto.do,填入“网页路径含有”并勾选复选框。 最后将最大下载图片数目和最大访问网页数都设为0,点开始即可。上面限制了图片和网页路径,只是为了防止下载不需要的图片。 4. 一次性在你的100个好友的页面上留下脚印 观察所有个人主页链接url,取出公共部分http://renren.com/profile.do,剩下的我就不用说了吧……对了,要是一张图片都不想下载,只要在“图片路径含有”中输入一个空格就可以了,因为任何图片url都不包含空格…… 5. 下载一部小说目录中的所有链接 提示:“选项”中有一个小小的“高级”按钮,有兴趣的同志可以研究一下…… ps. 大家可能看出来了,这个软件很类似网络爬虫,从一个网页“爬”到另一个网页,然后选择图片下载。如果有人需要根据关键词从大的图片搜索引擎下载图片,我推荐crazyPic这个软件。我这个软件的用途我暂时就想出这么多,欢迎发掘更多用途,也欢迎挑bug!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值