vc 下载http 图片
来源:互联网 发布:java方法名带下划线 编辑:程序博客网 时间:2024/05/16 12:38
#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)
{
}
下载
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;
}
- vc 下载http 图片
- vc++ 下载远程图片
- http下载图片
- Android http 下载图片
- Http下载图片
- vc实现http文件下载
- android用http下载图片
- IOS HTTP下载一个图片
- http 网络请求 下载图片
- 一般的http下载图片
- VC利用http远程下载文件
- VC利用http远程下载文件
- vc++实现http下载的通用类
- VC利用http远程下载文件
- VC实现HTTP上传和下载
- vc http 使用CHttpConnection下载文件
- vc http 使用CHttpConnection下载文件
- vc++实现http下载的通用类
- JAVA中的接口和抽象类(转)
- Gnome 3 gedit中文乱码 && gconf-editor找不到gedit
- JSP中文乱码问题终极解决方案
- telnet报错,Temporary failure in name resolution:Illegal seek
- VC中预处理指令与宏定义的妙用之一
- vc 下载http 图片
- row_number() OVER(PARTITION BY)函数介绍
- Are iOS 4 background API really the best solution to multitasking?
- 最速下降法与共轭梯度法
- C# 连接 SQL
- 三连消算法简介
- 梯度下降法
- VC中预处理指令与宏定义的妙用之二
- 【面试时最令职场面试官讨厌的十种行为】