关于在http服务器上下载文件的问题

toball 2003-12-11 03:15:53
比如说我要连接到http://127.0.0.1/setup.exe把setup.exe文件下载下来
存放到c:\下,应该怎么做,
能给个示例代码吗
注意我要下载的是exe文件。要通过http方式而不是ftp方式
...全文
207 10 打赏 收藏 举报
写回复
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
toball 2003-12-12
  • 打赏
  • 举报
回复
谁能给我写几行用winInet实现的方法吗
luoshayu 2003-12-12
  • 打赏
  • 举报
回复
这个破东西怎么不支持中文拷贝呢
很抱歉中文都是乱码了
luoshayu 2003-12-12
  • 打赏
  • 举报
回复
你看看HTTP协议1.1就知道了
我这里是首先查询一个文件的状态,从返回的信息中得到文件是否存在,文件的格式,文件的大小等信息

然后在生成一个新的HTTP头来下载文件

//多线程下载
//lpServer :服务器
//uPort :端口
//lpURL :URL
//dwTLen :文件的总长度
//thread :线程的个数
//lpDown :每个线程下载块的信息
//返回值 :实际启动的线程个数(实际上不是线程而是同时下载的SOCKET个数)
int DownLoadFile(LPCSTR lpServer,UINT uPort,LPCSTR lpURL,DWORD dwTLen,int thread,LPDOWNLOADINFO lpDown)
{
int i = 0; //
int len = 0; //·¢ËÍÏûÏ¢µÄ³¤¶È
DWORD dwSize = 0; //ÿ¿éµÄ´óС
int nOnLine = 0; //ÕýÈ·Á¬½ÓµÄÊýÄ¿
DWORD dwStart = 0; //ÿ¸öÏ߳̿ªÊ¼µÄµØÖ·
char szGetMsg[1024] = "";

//如果文件的长度未知--例如HTML文件等则用一个线程下载
if(dwTLen==0)
{
thread = 1;
}

//Æô¶¯Á¬½Ó
for(i=0;i<thread;i++)
{
//´ò¿ª·þÎñÆ÷
lpDown[i].s = Connect(lpServer,uPort);
//Èç¹ûÓÐij¸öÁ¬½Ó²»³É¹¦ÔòÏß³ÌÊýÏàÓ¦¼õС
if(lpDown[i].s==(SOCKET)-1)
{
thread;
lpDown[i].s = 0;
continue;
}
nOnLine++;
}

//Èç¹ûûÓÐÈ«²¿Ã»Óгɹ¦--·µ»Ø
if(nOnLine==0)
return 0;

//ÿ¿éµÄ´óС
dwSize = dwTLen/nOnLine;

//Èç¹ûûÓгɹ¦
if(nOnLine==0)
{
return nOnLine;
}

//ÕûÀíÁ¬½Ó³É¹¦µÄÌ×½Ó×Ö
if(nOnLine<thread)
TrimDownLoadInfo(lpDown,thread);
//Èç¹ûÏÂÔØÎļþµÄ³¤¶Èδ֪
if(dwTLen==0)
{
wsprintf(szGetMsg,"GET %s \n\n",lpURL);
len = lstrlen(szGetMsg);
len = send(lpDown->s,szGetMsg,len,0);
lpDown->bSave = TRUE;
return 1;
}

for(i=0;i<nOnLine;i++)
{
wsprintf(szGetMsg,
"GET %s HTTP/1.1\r\n" \
"Connection: close\r\n" \
"User-agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt; DTS Agent;)\r\n" \
"Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, application/x-comet, */*\r\n"\
"Host: %s\r\n" \
"Pragma: no-cache\r\n" \
"Range: bytes=%d-%d\r\n" \
"Cache-Control: no-cache\r\n\r\n",
lpURL,
lpServer,
dwStart,
(i==nOnLine-1)?(nOnLine==1?dwTLen:dwTLen):(dwStart+dwSize-1)
);

dwStart+=dwSize;

len = lstrlen(szGetMsg);
len = send(lpDown[i].s,szGetMsg,len,0);

if(len<lstrlen(szGetMsg))
{
MessageBox(ghMainWnd,"·¢ËÍÃüÁîʧ°Ü","ÍøÂç´íÎó",MB_OK);
DisConnect(lpDown[i].s);
lpDown[i].s = 0;
continue;
}
lpDown[i].dwStart = dwSize*i;
}
return nOnLine;
}


查询是否有数据下载
//TIMER
case WM_TIMER:
{
int i = 0;
timeval tv ;
int ret = 0;
int len = 0;
fd_set reads ;
__int64 llNum = 0;
char * lpData = NULL;
char szRecv[1024] = "";
char szText[MAX_PATH]= "";

//ÏÔʾÏÂÔØËÙÂÊ
if(wParam==IDDOWNSPEAD)
{
static DWORD dwOldLen = 0;
if(dwRLen==0)
{
dwOldLen = 0;
break;
}
else
{
wsprintf(szText,"%s--ÏÂÔØËÙÂÊ:%dKB/Ãë",szOldText,(dwRLen-dwOldLen)/1024);
dwOldLen=dwRLen;
SetWindowText(GetParent(hwnd),szText);
}
break;
}

//±£´æÏÂÔØÊý¾Ý
if(wParam==IDDOWNTIME)
{
for(i=0;i<nOnLine;i++)
{
FD_ZERO(&reads);
FD_SET(lpDownInfo[i].s,&reads);
tv.tv_sec = 0;
tv.tv_usec = 0;
ret = select(0,&reads,NULL,NULL,&tv);

if(ret<0)
continue;
if(ret==0)
continue;
if(FD_ISSET(lpDownInfo[i].s,&reads))
{
len = SaveDataToFile(&lpDownInfo[i],hFile);
//Èç¹ûÏÂÔس¤¶Èδ֪ÇÒûÓжÁÈ¡µ½Êý¾Ý--Ôò¿ÉÒÔÈÏΪÏÂÔØÍê±Ï
if(dwTLen==0 && len==0)
{
SendMessage(hwnd,WM_COMMAND,IDC_FINISH,NULL);
break;
}
dwRLen+=len;
}
}
//¼ÆËã½ø¶È
llNum = 100*dwRLen;
if(dwTLen!=0)
SendMessage(hProgress,PBM_SETPOS,WPARAM(llNum/dwTLen),NULL);
else
SendMessage(hProgress,PBM_SETPOS,100,NULL);

if(dwRLen==dwTLen)
{
SendMessage(hwnd,WM_COMMAND,IDC_FINISH,NULL);
}
}
break;
}


//保存数据
//±£´æÏÂÔصÄÊý¾Ý
DWORD SaveDataToFile(LPDOWNLOADINFO lpDown,HFILE hFile)
{
int Len = 0;
DWORD dwRet = 0;
LPSTR lpData = NULL;
char szRecv[1024] = "";

memset(szRecv,0,1024);

Len = 0;
Len = recv(lpDown->s,szRecv,1024,0);

if(Len<=0)
return 0;
else
{
_llseek(hFile,lpDown->dwStart+lpDown->dwRead,FILE_BEGIN);
//Èç¹ûÊÇÐÂÐÅÏ¢Ôò·ÖÎöÊý¾Ý
if(lpDown->bSave==FALSE)
{
//¿ªÊ¼ÏÂÔØ
lpDown->bSave = TRUE;
lpData = strstr(szRecv,"\r\n\r\n");
if(lpData==NULL)
dwRet = 0;
else
{
lpData+=4;
dwRet = Len-(lpData-szRecv);
_lwrite(hFile,lpData,dwRet);
}
}
else
{
dwRet = Len;
_lwrite(hFile,szRecv,dwRet);
}
}
lpDown->dwRead+=dwRet;

return dwRet;
}

//创建文件
//´´½¨ÒªÏÂÔصÄÎļþ
HFILE CreateDownLoadFile(LPCSTR lpFile,DWORD dwLen)
{
HFILE hFile = -1;
char szBuf[1024] = "";

hFile = _lcreat(lpFile,0);
if(hFile==HFILE_ERROR)
{
return HFILE_ERROR;
}

//Ìî³äÎļþ
memset(szBuf,0,1024);
for(int i=0;i<int(dwLen/1024);i++)
_lwrite(hFile,szBuf,1024);
_lwrite(hFile,szBuf,dwLen%1024);
return hFile;
}

几乎所有的代码都给你了应该没有问题了
kof99th 2003-12-12
  • 打赏
  • 举报
回复
他说的是直接构造一个http头发到服务器。
用wininet简单些。

BOOL GetInternetFile (LPTSTR lpszServer, LPTSTR lpszProxyServer)
{
BOOL bReturn = FALSE;

HINTERNET hOpen = NULL,
hConnect = NULL,
hRequest = NULL;

DWORD dwSize = 0,
dwFlags = INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE;

TCHAR szErrMsg[200];

char *lpBufferA,
*lpHeadersA;

TCHAR *lpBufferW,
*lpHeadersW;

LPTSTR AcceptTypes[2] = {TEXT("*/*"), NULL};


// Initialize the use of the Windows CE Internet functions.
if (g_bProxy)
{
hOpen = InternetOpen (TEXT("CeHttp"), INTERNET_OPEN_TYPE_PROXY,
lpszProxyServer, 0, 0);
}
else
{
hOpen = InternetOpen (TEXT("CeHttp"), INTERNET_OPEN_TYPE_PRECONFIG,
NULL, 0, 0);
}

if (!hOpen)
{
wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("InternetOpen Error"),
GetLastError());
return FALSE;
}

if (g_bOpenURL)
{
if (!(hRequest = InternetOpenUrl (hOpen, lpszServer, NULL, 0,
INTERNET_FLAG_RELOAD, 0)))
{
wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("InternetOpenUrl Error"),
GetLastError());
goto exit;
}
}
else
{
// Open an HTTP session for a specified site by using lpszServer.
if (!(hConnect = InternetConnect (hOpen,
lpszServer,
INTERNET_INVALID_PORT_NUMBER,
NULL, NULL,
INTERNET_SERVICE_HTTP,
0, 0)))
{
wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("InternetConnect Error"),
GetLastError());
goto exit;
}

// Open an HTTP request handle.
if (!(hRequest = HttpOpenRequest (hConnect,
TEXT("GET"),
NULL,
HTTP_VERSION,
NULL,
(LPCTSTR*)AcceptTypes,
dwFlags, 0)))
{
wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("HttpOpenRequest Error"),
GetLastError());
goto exit;
}

// Send a request to the HTTP server.
if (!HttpSendRequest (hRequest, NULL, 0, NULL, 0))
{
wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("HttpSendRequest Error"),
GetLastError());
goto exit;
}
}

// Call HttpQueryInfo to find out the size of the headers.
HttpQueryInfo (hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &dwSize,
NULL);

// Allocate a block of memory for lpHeadersA.
lpHeadersA = new CHAR [dwSize];

// Call HttpQueryInfo again to get the headers.
if (!HttpQueryInfo (hRequest,
HTTP_QUERY_RAW_HEADERS_CRLF,
(LPVOID) lpHeadersA, &dwSize, NULL))
{
wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("HttpQueryInfo"),
GetLastError());
goto exit;
}
else
{
// Clear all of the existing text in the edit control and prepare
// to put the new information in it.
SendMessage (g_hwndEdit, EM_SETSEL, 0, -1);
SendMessage (g_hwndEdit, WM_CLEAR, 0, 0);
SendMessage (g_hwndEdit, WM_PAINT, TRUE, 0);
}

// Terminate headers with NULL.
lpHeadersA [dwSize] = '\0';

// Get the required size of the buffer that receives the Unicode
// string.
dwSize = MultiByteToWideChar (CP_ACP, 0, lpHeadersA, -1, NULL, 0);

// Allocate a block of memory for lpHeadersW.
lpHeadersW = new TCHAR [dwSize];

// Convert headers from ASCII to Unicode
MultiByteToWideChar (CP_ACP, 0, lpHeadersA, -1, lpHeadersW, dwSize);

// Put the headers in the edit control.
SendMessage (g_hwndMain, WM_PUTTEXT, NULL, (LPARAM) lpHeadersW);

// Free the blocks of memory.
delete[] lpHeadersA;
delete[] lpHeadersW;

// Allocate a block of memory for lpHeadersW.
lpBufferA = new CHAR [32000];

do
{
if (!InternetReadFile (hRequest, (LPVOID)lpBufferA, 32000, &dwSize))
{
wsprintf(szErrMsg, TEXT("%s: %x"), TEXT("InternetReadFile Error"),
GetLastError());
goto exit;
}

if (dwSize != 0)
{
// Terminate headers with NULL.
lpBufferA [dwSize] = '\0';

// Get the required size of the buffer which receives the Unicode
// string.
dwSize = MultiByteToWideChar (CP_ACP, 0, lpBufferA, -1, NULL, 0);

// Allocate a block of memory for lpBufferW.
lpBufferW = new TCHAR [dwSize];

// Convert the buffer from ASCII to Unicode.
MultiByteToWideChar (CP_ACP, 0, lpBufferA, -1, lpBufferW, dwSize);

// Put the buffer in the edit control.
SendMessage (g_hwndMain, WM_PUTTEXT, NULL, (LPARAM) lpBufferW);

// Free the block of memory.
delete[] lpBufferW;
}
} while (dwSize);

// Free the block of memory.
delete[] lpBufferA;

bReturn = TRUE;

exit:

// Close the Internet handles.
if (hOpen)
{
if (!InternetCloseHandle (hOpen))
wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("CloseHandle Error"),
GetLastError());
}

if (hConnect)
{
if (!InternetCloseHandle (hConnect))
wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("CloseHandle Error"),
GetLastError());
}

if (hRequest)
{
if (!InternetCloseHandle (hRequest))
wsprintf (szErrMsg, TEXT("%s: %x"), TEXT("CloseHandle Error"),
GetLastError());
}

return bReturn;
}
toball 2003-12-12
  • 打赏
  • 举报
回复
楼上的能不能写得详细点
luoshayu 2003-12-12
  • 打赏
  • 举报
回复
可以的话请给分^_^
我还没有在上面回答过问题呢,不要叫我空手而归啊
luoshayu 2003-12-11
  • 打赏
  • 举报
回复
wsprintf(szGetMsg,
"HEAD %s HTTP/1.1\n" \
"User-agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)\n" \
"Accept: */* \n" \
"Host: %s\n" \
"Pragma: no-cache\n" \
"Connection: close\n" \
"Referer: %s\n" \
"Cache-Control: no-cache\n\n",
lpURL,
lpServer,
szReferer);

send(s,szGetMsg,lstrlen(szGetMsg))
toball 2003-12-11
  • 打赏
  • 举报
回复
怎么没人来看看
toball 2003-12-11
  • 打赏
  • 举报
回复
你可以给个示例代码
会者不难
只要几句话可能
sdchl 2003-12-11
  • 打赏
  • 举报
回复
wininet就可以啊
相关推荐
发帖
网络编程

1.8w+

社区成员

VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
帖子事件
创建了帖子
2003-12-11 03:15
社区公告
暂无公告