So I have this giant monstrosity of code, which works as expected, goes to the respective URL and if it's not a URL it goes to search for the string(s). The problem is
- the websites that are down but exist or don't give a response are deemed as not a URL and therefore are being searched for instead
- pinging the same site too many times leads to being blocked, eg google.
- The ping method takes too long to respond.
[...]
try
{
timer1.Start();
if (SearchBox.Text.Contains(SearchBox.Text.ToString()))
{
if (webView21 != null && webView21.CoreWebView2 != null)
{
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
// Use the default Ttl value which is 128,
// but change the fragmentation behavior.
options.DontFragment = true;
// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 0;
PingReply reply = pingSender.Send(SearchBox.Text.ToString(), timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
if (SearchBox.Text.StartsWith("https://"))
{
webView21.CoreWebView2.Navigate(SearchBox.Text);
}
else if (SearchBox.Text.StartsWith("http://"))
{
webView21.CoreWebView2.Navigate(SearchBox.Text);
}
else
{
webView21.CoreWebView2.Navigate("https://" + SearchBox.Text);
}
}
else
{
webView21.CoreWebView2.Navigate("https://duckduckgo.com/?q=" + SearchBox.Text + "&t=h_&ia=web");
}
}
}
else if (SearchBox.Text.StartsWith("https://"))
{
if (webView21 != null && webView21.CoreWebView2 != null)
{
webView21.CoreWebView2.Navigate(SearchBox.Text);
}
}
else if (SearchBox.Text.StartsWith("http://"))
{
if (webView21 != null && webView21.CoreWebView2 != null)
{
webView21.CoreWebView2.Navigate(SearchBox.Text);
}
}
else
{
if (webView21 != null && webView21.CoreWebView2 != null)
{
webView21.CoreWebView2.Navigate(SearchBox.Text);
}
}
}
catch
{
timer1.Start();
webView21.CoreWebView2.Navigate("https://duckduckgo.com/?q=" + SearchBox.Text + "&t=h_&ia=web");
}
}
Does anybody know how I could improve on this? How can I make my web browser more efficient at this. Regex maybe?