首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

急怎么判断webbrowser能否打开网页

2013-09-09 
急:如何判断webbrowser能否打开网页?我使用webbrowser控件浏览某网页,但如果打开失败或者无法连接服务器时

急:如何判断webbrowser能否打开网页?
我使用webbrowser控件浏览某网页,但如果打开失败或者无法连接服务器时,就会显示:此程序无法显示网页......
我不想webbrowser显示这个提示,想先检测一下网页是否能够打开,或者当失败时跳到自己本地的出错提示页或者图片,该如何判断? 
[解决办法]
在DocumentComplete事件中判断下标题
uses MSHTML;
procedure TForm1.wb1DocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
begin
  ShowMessage(IHtmlDocument2(wb1.Document).title);
end;
[解决办法]
找个idhttp获取网站http头的例子给你参考下:
procedure Button1Click(Sender: TObject);
var
  http : TIdHttp;
  url : string;
  code : integer;
begin
  url := 'http://www.WEBSITE.com';
  http := TIdHTTP.Create(nil);
  try
    try
      http.Head(url);
      code := http.ResponseCode;
    except
      on E: EIdHTTPProtocolException do
        code := http.ResponseCode; 
    end;
    ShowMessage(IntToStr(code));
  finally
    http.Free();
  end;
end; 

这个ResponseCode返回200就是OK,具体代码表示什么你可以搜索http头状态码,你先判断OK了再用webbrower去打开页面,或者你能找到webbrower取http头的方法也可以,我也不知道可不可以。
[解决办法]


function TForm1.CheckInternetOnline: Boolean; // 检查互联网是否在线
var
  ConnectState: DWORD;
  StateSize: DWORD;
begin
  ConnectState := 0;
  StateSize := sizeof(ConnectState);
  Result := false;
  // Use WinInet.pas;
  if InternetQueryOption(nil, INTERNET_OPTION_CONNECTED_STATE, @ConnectState,
    StateSize) then
    Result := (ConnectState and INTERNET_STATE_DISCONNECTED) <> 2;
  if Result then
    Result := InternetCheckConnection('http://www.baidu.com', 1, 0);


end;




把里面的网址 换成你要访问的 地址  

热点排行