您的位置:首页 > 理论基础 > 计算机网络

ItenetAPI Http下载(得到真正的下载地址)

2008-04-01 16:22 190 查看
得到两天时间,修改了原有的升级程序,

1、解决了不能得到真正URL的问题,此功能主要是当查询状态返回代码是302时,调用HttpQueryInfo,查询location内容,得到下载地址。

2、解决了从URL得到真正要的下载文件的文件名的问题。此功能通过HttpQueryInfo,查询conten_dispostion,从得到内容中查找filename=字符串,其后的内容即为要下载的文件名。

查询参数

HTTP_QUERY_CONTENT_DISPOSITION //得到真正下载的文件名,当不能从下地址得到文件名时,查询此参数,从中查询filename字符,得到真正的下载文件名。

HTTP_QUERY_STATUS_CODE //查询状态返回代码

HTTP_QUERY_LOCATION //查询真正的下载地址,当查询状态代码为302即Object move时,查询此参数。

function WWWDownFile(const FTURL, LocalFile: string): Boolean; //下载函数.
const
FTAcceptTypes = '*/*';
FTAgent = 'Explorer';
FTUserName = ''; //用户名.
FTPassword = ''; //密码.
FTPort = 80; //端口.
FTPostQuery = 'GET';
FTReferer = '';
var
hSession, hConnect, hRequest: hInternet;
HostName, FileName: string;
f: file;
Buf: Pointer;
dwBufLen, dwIndex: DWord;
Data: array[0..$400] of Char;
RequestMethod: PChar;
InternetFlag: DWord;
TimeOut: Cardinal;
AcceptType: LPStr;
BytesToRead, BytesReaded: DWord;
FTFileSize: integer;
myBuf: array[0..1023] of Char;
procedure ParseURL(URL: string; var HostName, FileName: string);
var
i: Integer;
begin
if Pos('http://', LowerCase(URL)) <> 0 then
System.Delete(URL, 1, 7);
i := Pos('/', URL);
HostName := Copy(URL, 1, i);
FileName := Copy(URL, i, Length(URL) - i + 1);

if (Length(HostName) > 0) and (HostName[Length(HostName)] = '/') then
SetLength(HostName, Length(HostName) - 1);
end;
procedure CloseHandles;
begin
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
end;
begin
TimeOut := 6000000;
ParseURL(FTURL, HostName, FileName);
RequestMethod := PChar(FTPostQuery); // 'GET'
//InternetFlag := 0;
InternetFlag := INTERNET_FLAG_NO_AUTO_REDIRECT;
AcceptType := PChar('Accept: ' + FTAcceptTypes);
if FTAgent <> '' then
hSession := InternetOpen(PChar(FTAgent),
INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0)
else
hSession := InternetOpen(nil,
INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
//hConnect := InternetConnect(hSession, PChar(HostName),
// FTPort, PChar(FTUserName), PChar(FTPassword),
// INTERNET_SERVICE_HTTP, 0, 0);
hConnect := InternetConnect(hSession, PChar(HostName),
FTPort, nil, nil,
INTERNET_SERVICE_HTTP, 0, 0);
hRequest := HttpOpenRequest(hConnect, RequestMethod, PChar(FileName),
'HTTP/1.1',
PChar(FTReferer), @AcceptType, InternetFlag, 0);
InternetSetOption(hRequest, INTERNET_OPTION_CONNECT_TIMEOUT,
@TimeOut, SizeOf(TimeOut));
InternetSetOption(hRequest, INTERNET_OPTION_DATA_RECEIVE_TIMEOUT, @TimeOut,
SizeOf(TimeOut));
if FTPostQuery = '' then
HttpSendRequest(hRequest, nil, 0, nil, 0)
else
HttpSendRequest(hRequest, 'Content-Type: application/x-www-form-urlencoded',
47,
PChar(FTPostQuery), Length(FTPostQuery));

//-----------------------------------------
dwIndex := 0;
dwBufLen := 1024;
FillChar(myBuf, 1024, 0);
Result := HttpQueryInfo(hRequest, HTTP_QUERY_STATUS_CODE,
@myBuf[0], dwBufLen, dwIndex);

//-------------------------------------

dwIndex := 0;
dwBufLen := 1024;
GetMem(Buf, dwBufLen);

Result := HttpQueryInfo(hRequest, HTTP_QUERY_CONTENT_LENGTH,
Buf, dwBufLen, dwIndex);
if Result then begin
BytesReaded := 0;
FTFileSize := StrToInt(StrPas(Buf));
AssignFile(f, LocalFile);
Rewrite(f, 1);
while True do begin
if not InternetReadFile(hRequest, @Data, SizeOf(Data), BytesToRead) then
break
else if BytesToRead = 0 then Break
else
BlockWrite(f, Data, BytesToRead);
BytesReaded := BytesReaded + BytesToRead;
end;
Result := FTFileSize = Integer(BytesReaded);
CloseFile(f);
end;
FreeMem(Buf);
CloseHandles;

end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: