Checking URL's - go to second url if first url fails using axWebBrowser


This article explains about how to Checking URL go to second url if first url fails using axWebBrowser

This program checks the url and if that files it will go to second url

Binary file and source code files can be downloaded from

http://eng.utoledo.edu/~smulpuru/projects/URL_Testing.zip

Note : i used c# 2005 & axWebBrowser Component (Microsoft Web Browser)

Binary file and source code files can be downloaded from


http://eng.utoledo.edu/~smulpuru/projects/URL_Testing.zip


Form1.Designer.cs Contents


namespace webapplication
{
partial class Form1
{
///
/// Required designer variable.
///

private System.ComponentModel.IContainer components = null;

///
/// Clean up any resources being used.
///

/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.axWebBrowser1 = new AxSHDocVw.AxWebBrowser();
this.URL1 = new System.Windows.Forms.Label();
this.URL2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).BeginInit();
this.SuspendLayout();
//
// axWebBrowser1
//
this.axWebBrowser1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.axWebBrowser1.Enabled = true;
this.axWebBrowser1.Location = new System.Drawing.Point(0, 68);
this.axWebBrowser1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axWebBrowser1.OcxState")));
this.axWebBrowser1.Size = new System.Drawing.Size(360, 287);
this.axWebBrowser1.TabIndex = 0;
// this is used for naviagtion error event (url fail)
this.axWebBrowser1.NavigateError += new AxSHDocVw.DWebBrowserEvents2_NavigateErrorEventHandler(this.axWebBrowser1_NavigateError);
//
// URL1
//
this.URL1.AutoSize = true;
this.URL1.Location = new System.Drawing.Point(9, 16);
this.URL1.Name = "URL1";
this.URL1.Size = new System.Drawing.Size(35, 13);
this.URL1.TabIndex = 1;
this.URL1.Text = "URL1";
//
// URL2
//
this.URL2.AutoSize = true;
this.URL2.Location = new System.Drawing.Point(9, 42);
this.URL2.Name = "URL2";
this.URL2.Size = new System.Drawing.Size(35, 13);
this.URL2.TabIndex = 2;
this.URL2.Text = "URL2";
//
// textBox1
//
this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox1.Location = new System.Drawing.Point(50, 16);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(255, 20);
this.textBox1.TabIndex = 3;
//
// textBox2
//
this.textBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox2.Location = new System.Drawing.Point(50, 42);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(255, 20);
this.textBox2.TabIndex = 4;
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.button1.Location = new System.Drawing.Point(313, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(35, 43);
this.button1.TabIndex = 5;
this.button1.Text = "GO";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(360, 355);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.URL2);
this.Controls.Add(this.URL1);
this.Controls.Add(this.axWebBrowser1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();

}




#endregion

private AxSHDocVw.AxWebBrowser axWebBrowser1;
private System.Windows.Forms.Label URL1;
private System.Windows.Forms.Label URL2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
}
}




Form1.cs Contents


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//Author - Siva Mulpuru (MSEE From university of toledo, Ohio)
//suggestions or comments are always welcomed @ siva.mulpuru@gmail.com


//for testing purpose please use mail.yahoo.com or gmail.com or orkut.com or youtube.com
// as they dont have any error on the page and they are fast, websites like cnn.com or indiafm.com have lot of content and it takes some time to download (especially with Dialup)

namespace webapplication
{
public partial class Form1 : Form
{
public int i;
public Form1()
{
i = 0;//flag
InitializeComponent();
}
// this is called when there's an navigation error (ie URls Fail to work)
// dont forget to include this.axWebBrowser1.NavigateError += new AxSHDocVw.DWebBrowserEvents2_NavigateErrorEventHandler(this.axWebBrowser1_NavigateError); in InitializeComponent() - form1.designer.cs file
void axWebBrowser1_NavigateError(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateErrorEvent e)
{
try
{
if (i==0)
{
//if URL1 fails navigate to URL2 (textbox2)
axWebBrowser1.Navigate(textBox2.Text);
// to prevent from navigating again and again if url2 fails too
i = 10;//Can specify any number other than 0

}

}
catch
{
}
}

//Go Button
private void button1_Click(object sender, EventArgs e)
{
try
{
i = 0;//Flag
//Basic Conditional Statements
if (textBox1.Text != "")
{
i = 0;
axWebBrowser1.Navigate(textBox1.Text);
}
else if (textBox2.Text != "")
{
i = 1;
axWebBrowser1.Navigate(textBox2.Text);
}
else
{
MessageBox.Show("Both Urls are empty... try again");
textBox1.Focus();
}
}
catch
{
}
}

private void Form1_Load(object sender, EventArgs e)
{
// to prevent the browser from displaying errors on the webpage (URL)
axWebBrowser1.Silent = true;

}

}
}


Note: i used c# 2005, if ur using 2003 then form1.design and form1 code will be in one cs file called form1.cs


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: