开发者

Why does this code for checking proxy validity crash?

开发者 https://www.devze.com 2022-12-16 21:42 出处:网络
I have the following code in my program.When I try to check if the proxy is a valid proxy or not it crashes my program.Any ideas how to fix this?I\'m using indy 10.

I have the following code in my program. When I try to check if the proxy is a valid proxy or not it crashes my program. Any ideas how to fix this? I'm using indy 10.

if checkproxy('http://www.google.com') label1.text:='Good' else label1.text:='bad';

If I try to call another idHTTP connect after requesting checkproxy it crashes the program.

This is the actual code.... That returns the error...


function TregForm.webSession(sURL : 开发者_如何学JAVAansistring) : ansistring;
  var  sstream : Tstringstream;
       httpcon : TIdHTTP;
       antifreeze : TIdAntiFreeze;
begin
  result := '';
  sstream := tstringstream.Create('');
  try
    if length(SettingsForm.edtProxyServer.text) >= 7 then  // 0.0.0.0 
    if checkproxy('http://www.google.com') then
    begin
      antifreeze := TIdAntiFreeze.Create(nil);
      httpcon := TIdHTTP.create;
      httpcon.ProxyParams.BasicAuthentication:=true;
      httpcon.ProxyParams.ProxyPassword:=SettingsForm.edtProxyPass.Text;
      httpcon.ProxyParams.ProxyPort:=strtoint(SettingsForm.edtProxyPort.Text);
      httpcon.ProxyParams.ProxyServer:=SettingsForm.edtProxyServer.Text;
      httpcon.ProxyParams.ProxyUsername:=SettingsForm.edtProxyUserName.Text;
    end
    else
    begin
      showmessage('Proxy Server Not Working..  Attempting to bypass the Proxy');
      antifreeze := TIdAntiFreeze.Create(nil);
      httpcon := TIdHTTP.create;
      //httpcon.Request.ProxyConnection:='';
      //httpcon.ProxyParams.BasicAuthentication:=false;
      //httpcon.ProxyParams.ProxyPassword:='';
      //httpcon.ProxyParams.ProxyPort:=0;
      //httpcon.ProxyParams.ProxyServer:='';
      //httpcon.ProxyParams.ProxyUsername:='';
    end;
    httpcon.HandleRedirects := true;
    antifreeze.Active := true;
    HttpCon.Get(sURL,sstream);
    result := utf8towidestring(sstream.DataString);
  except
    httpcon.Disconnect;
  end;
  antifreeze.Active := false;
  freeandnil(httpcon);
  freeandnil(antifreeze);
  freeandnil(sstream);
end;

function TregForm.checkproxy(sURL : ansistring) : boolean; var //sstream : Tstringstream; httpcon : TIdHTTP; antifreeze : TIdAntiFreeze; begin result := true; try antifreeze := TIdAntiFreeze.Create(nil); httpcon := TIdHTTP.create; //sstream := tstringstream.Create(''); httpcon.ProxyParams.BasicAuthentication:=true; httpcon.ProxyParams.ProxyPassword:=SettingsForm.edtProxyPass.Text; httpcon.ProxyParams.ProxyPort:=strtoint(SettingsForm.edtProxyPort.Text); httpcon.ProxyParams.ProxyServer:=SettingsForm.edtProxyServer.Text; httpcon.ProxyParams.ProxyUsername:=SettingsForm.edtProxyUserName.Text;

httpcon.HandleRedirects := true;
antifreeze.Active := true;
HttpCon.Request.ProxyConnection:=SettingsForm.edtProxyServer.Text;
HttpCon.head(sURL);
httpCon.Disconnect;

except on E: EIdException do begin result:=false; end; On E:exception do begin //showmessage('External error:' + #13#10 + E.Message); showmessage('Proxy Settings Error'); result:=false; end; end; antifreeze.Active := false; freeandnil(httpcon); freeandnil(antifreeze); //freeandnil(sstream); end;

Error mesage: Exception AccessViolation in module key.exe at E001B844A. Access violation at address 005B844A in module 'key.exe'. Read of address 00000000.


In your code above, if the "SettingsForm.edtProxyServer.text" is less than 7 chars, you will have a situation where you use AntiFreeze and HttpCon without creating them first.

I experimented a little with your code, and tested this on my WinXP SP3 (D2010, Indy v10.5.5). This should be working code...

procedure TForm1.Button1Click(Sender: TObject);
Var
   Resp : String;
begin
     Resp := webSession('http://www.celarius.com');
     if Length(Resp)>0 then
        MessageDlg('Got the body ok',mtInformation,[mbOk],0);
end;

function TForm1.webSession(sURL : ansistring) : ansistring;
var
   SStream    : Tstringstream;
   HTTPCon    : TIdHTTP;
   AntiFreeze : TIdAntiFreeze;
begin
     Result := '';
     if Length(SettingsForm.edtProxyServer.text) >= 7 then  // 0.0.0.0
     Try
        SStream := NIL;
        AntiFreeze := NIL;
        HTTPCon := NIL;
        Try
           SStream := tstringstream.Create('');
           { Create & Set IdHTTP properties }
           HTTPCon := TIdHTTP.create;
           HTTPCon.HandleRedirects := true;
           { Check Proxy }
           if checkproxy('http://www.google.com') then
           Begin
                HTTPCon.ProxyParams.ProxyServer := SettingsForm.edtProxyServer.text;
                HTTPCon.ProxyParams.ProxyPort := StrToInt(SettingsForm.edtProxyPort.Text);
                HTTPCon.ProxyParams.BasicAuthentication := True;
                HTTPCon.ProxyParams.ProxyUsername := SettingsForm.edtProxyServer.Text;
                HTTPCon.ProxyParams.ProxyPassword := SettingsForm.edtProxyUserName.Text;
           End;
           { Create another AntiFreeze - only 1/app }
           AntiFreeze := TIdAntiFreeze.Create(nil);
           AntiFreeze.Active := true;
           HTTPCon.Get(sURL,SStream);
           Result := UTF8ToWideString(SStream.DataString);
        Finally
           If Assigned(HTTPCon) then FreeAndNil(HTTPCon);
           If Assigned(AntiFreeze) then FreeAndNil(AntiFreeze);
           If Assigned(SStream) then FreeAndNil(SStream);
        End;
     Except
        { Handle exceptions }
        On E:Exception do
           MessageDlg('Exception: '+E.Message,mtError, [mbOK], 0);
     End;
end;

function TForm1.checkproxy(sURL : ansistring) : boolean;
var
   HTTPCon : TIdHTTP;
   AntiFreeze : TIdAntiFreeze;
begin
     Result := False;
     Try
        { Inti vars }
        AntiFreeze := NIL;
        HTTPCon := NIL;
        Try
           { AntiFreeze }
           AntiFreeze := TIdAntiFreeze.Create(NIL);
           AntiFreeze.Active := true;
           { Create & Set IdHTTP properties }
           HTTPCon := TIdHTTP.Create(NIL);
           HTTPCon.ProxyParams.ProxyServer := SettingsForm.edtProxyServer.text;
           HTTPCon.ProxyParams.ProxyPort := StrToInt(SettingsForm.edtProxyPort.Text);
           HTTPCon.ProxyParams.BasicAuthentication := True;
           HTTPCon.ProxyParams.ProxyUsername := SettingsForm.edtProxyServer.Text;
           HTTPCon.ProxyParams.ProxyPassword := SettingsForm.edtProxyUserName.Text;
           HTTPCon.HandleRedirects := true;
           HTTPCon.ConnectTimeout := 1000;
           HTTPCon.Request.Connection := 'close';
           HTTPCon.Head(sURL);
        Finally
           { Cleanup }
           if Assigned(HTTPCon) then
           Begin
                { Return Success/Failure }
                Result := HTTPCon.ResponseCode = 200;
                If HTTPCon.Connected then HTTPCon.Disconnect;
                FreeAndNil(HTTPCon);
           End;
           if Assigned(AntiFreeze) then FreeAndNil(AntiFreeze);
        End;
     Except
        On E:EIdException do ;
        { Handle exceptions }
        On E:Exception do
           MessageDlg('Exception: '+E.Message,mtError, [mbOK], 0);
     End;
end;
0

精彩评论

暂无评论...
验证码 换一张
取 消