2010-01-18 51 views
0

我的程序中有以下代碼。當我嘗試檢查代理是否是有效代理時,它會使我的程序崩潰。任何想法如何解決這一問題?我使用的是印10爲什麼此代碼用於檢查代理有效性崩潰?

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

如果我嘗試調用另一個idHTTP請求checkproxy崩潰的程序之後連接。

這是實際的代碼....這將返回錯誤......


function TregForm.webSession(sURL : ansistring) : 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; 

除了 在E:EIdException就開始 結果:= FALSE; 結束; 關於E:例外開始 // showmessage('外部錯誤:'+#13#10 + E.Message); showmessage('Proxy Settings Error'); 結果:= false; 結束; 結束; antifreeze.Active:= false; freeandnil(httpcon); freeandnil(防凍液); // freeandnil(sstream); 結束;

錯誤消息:E001B844A模塊key.exe中的異常AccessViolation。 模塊'key.exe'中地址爲005B844A的訪問衝突。讀地址00000000

+0

你是什麼意思,當你說「它崩潰」? – 2010-01-18 06:21:30

+0

我得到一個運行時錯誤,並且程序崩潰了,並且想要將崩潰數據發送到微軟。 – Brad 2010-01-18 07:02:36

+0

我剪切/粘貼的實際代碼...當我使用調試器運行程序關閉它立即彈出與請告訴微軟有關此問題的消息。然後提供錯誤信息。當我使用調試器運行時,會給我一箇中斷通知,但不會導致導致問題的代碼行。 – Brad 2010-01-18 07:39:31

回答

4

的在上面的代碼,如果「SettingsForm.edtProxyServer.text」小於7個字符,你將不得不在您使用防凍液和HttpCon無需先創建它們的情況。

我試驗了一下你的代碼,並在我的WinXP SP3(D2010,Indy v10.5.5)上進行了測試。這應該是工作代碼...

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

我不敢相信我錯過了!感謝您的信息和代碼。 -Brad – Brad 2010-01-18 10:09:59

+0

不客氣。第二雙眼睛通常會看到第一對錯過的東西! – 2010-01-18 10:25:25