F
feizaihz
Unregistered / Unconfirmed
GUEST, unregistred user!
我用的是Dephi6 自己带的indy,使用IDHttp来下载文件,对于有代理的情况,也设置了代理相关的参数,但是结果就是出错,
出错的主要信息是:错误代码: 407 Proxy Authentication Required。The ISA Server requires authorization to fulfill the request. Access to the Web Proxy service is denied
但是通过IE却能够正常下载,没有代理的情况下程序运行都很正常,主要的源代码如下:
说明:
ProxyObject是自己定义的一个代理信息的对象。
procedure TThreadHttp.Execute;
var
MyDownLoadFile:TFileStream;
tStream: TFileStream;
aFile: string;
function DownLoad:Boolean;
begin
Try
//设置缓存过期时间
IdHTTP.Request.Expires:=Now-1;
//不使用缓存,我附加的
//与以前的服务器兼容
IdHTTP.Request.ExtraHeaders.Add('Pragma: no-cache');
//不使用缓存新的
IdHTTP.Request.ExtraHeaders.Add('Cache-Control: no-cache');
IdHTTP.Get(SourceFile,MyDownLoadFile);
Result:=True;
Except
Result:=False;
end;
end;
begin
FreeOnTerminate := True;
IdHTTP := TIdHTTP.Create(nil);
IdHttp.OnConnected:=DSDownLoadWin.IdHTTPConnected;
IdHttp.OnDisconnected:=DSDownLoadWin.IdHTTPDisconnected;
IdHTTP.OnWork:=DSDownLoadWin.IdHTTPWork;
IdHTTP.OnWorkBegin:=DSDownLoadWin.IdHTTPWorkBegin;
IdHTTP.OnWorkEnd :=DSDownLoadWin.IdHTTPWorkEnd;
//如果需要使用代理信息
if ProxyObject.UseProxy then
begin
IdHTTP.Request.ProxyServer:=ProxyObject.ProxyServer;
IdHTTP.Request.ProxyPort:=ProxyObject.ProxyPort;
IdHTTP.Request.ProxyUsername:=ProxyObject.UserName;
IdHTTP.Request.ProxyPassword:=ProxyObject.PassWord;
end;
Try
Try
if FileExists(DestFile) then
MyDownLoadFile := TFileStream.Create(DestFile, fmOpenWrite)
else
MyDownLoadFile:=TFileStream.Create(DestFile,fmCreate);
Success:=DownLoad;
except
Success:=False;
IdHTTP.DisconnectSocket;
end;
Finally
MyDownLoadFile.Free;
if Success=False then DeleteFile(PChar(DestFile));
IdHTTP.DisconnectSocket;
DSDownLoadWin.Success:=Success;
DSDownLoadWin.HaveDownLoad:=True;
FreeAndNil(IdHTTP);
end;
end;
出错的主要信息是:错误代码: 407 Proxy Authentication Required。The ISA Server requires authorization to fulfill the request. Access to the Web Proxy service is denied
但是通过IE却能够正常下载,没有代理的情况下程序运行都很正常,主要的源代码如下:
说明:
ProxyObject是自己定义的一个代理信息的对象。
procedure TThreadHttp.Execute;
var
MyDownLoadFile:TFileStream;
tStream: TFileStream;
aFile: string;
function DownLoad:Boolean;
begin
Try
//设置缓存过期时间
IdHTTP.Request.Expires:=Now-1;
//不使用缓存,我附加的
//与以前的服务器兼容
IdHTTP.Request.ExtraHeaders.Add('Pragma: no-cache');
//不使用缓存新的
IdHTTP.Request.ExtraHeaders.Add('Cache-Control: no-cache');
IdHTTP.Get(SourceFile,MyDownLoadFile);
Result:=True;
Except
Result:=False;
end;
end;
begin
FreeOnTerminate := True;
IdHTTP := TIdHTTP.Create(nil);
IdHttp.OnConnected:=DSDownLoadWin.IdHTTPConnected;
IdHttp.OnDisconnected:=DSDownLoadWin.IdHTTPDisconnected;
IdHTTP.OnWork:=DSDownLoadWin.IdHTTPWork;
IdHTTP.OnWorkBegin:=DSDownLoadWin.IdHTTPWorkBegin;
IdHTTP.OnWorkEnd :=DSDownLoadWin.IdHTTPWorkEnd;
//如果需要使用代理信息
if ProxyObject.UseProxy then
begin
IdHTTP.Request.ProxyServer:=ProxyObject.ProxyServer;
IdHTTP.Request.ProxyPort:=ProxyObject.ProxyPort;
IdHTTP.Request.ProxyUsername:=ProxyObject.UserName;
IdHTTP.Request.ProxyPassword:=ProxyObject.PassWord;
end;
Try
Try
if FileExists(DestFile) then
MyDownLoadFile := TFileStream.Create(DestFile, fmOpenWrite)
else
MyDownLoadFile:=TFileStream.Create(DestFile,fmCreate);
Success:=DownLoad;
except
Success:=False;
IdHTTP.DisconnectSocket;
end;
Finally
MyDownLoadFile.Free;
if Success=False then DeleteFile(PChar(DestFile));
IdHTTP.DisconnectSocket;
DSDownLoadWin.Success:=Success;
DSDownLoadWin.HaveDownLoad:=True;
FreeAndNil(IdHTTP);
end;
end;