Search the web from the address bar

2019-02-21 05:33发布

问题:

on my WebBrowser I have a address bar of course, but I can only search urls in the address bar, like "facebook.com" and something. I want my address field to be able to work like a search engine to, so when I type like www.facebook.com I will go to facebook.com and when I search like facebook, without www or .com or something I want my address bar to redirect me to https://www.google.se/?gws_rd=ssl#q=facebook if you know what I mean.

回答1:

Maybe this is not the best way but it works.

The only thing you need to do is add More things to the arrays to make it more prefect."because there are more possible web url endings and starts"
Dim CheckArrStart()
Dim CheckArrEnd()

I would make a function to check to check the url entered in the address bar.
Private Function CheckWebPageLink(ByVal Link As String) As Boolean

In this function i would use 2 for each loops to check the start en the end of the url.
If i find a start i would + 1 to my ok dim "Dim Ok As Integer = 0" and remove it from the string
Then go to the next for each loop to find a url end.
So if a find a end + 1 to my ok dim and also remove the end from the string. i do this because i want to check how long the string is at the end becaus there must be something between www. and .com

Then at the end of my function i check if my ok dim has a value of 2 and how long the link is without the start and the end.
If true i return a true boolean and if false i return a false boolean.

Then in my navigate button i make a if statement end check if the booblean from my function is true ore false."If CheckWebPageLink(ToolStripTextBox1.Text.ToLower) = True Then"
If true just navigate to the url and if false :
WebBrowser1.Navigate("https://www.google.com/?gws_rd=ssl#q=" & ToolStripTextBox1.Text)

Like i already said the arrays you still need to fix, i just added some to test.

Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click

    If CheckWebPageLink(ToolStripTextBox1.Text.ToLower) = True Then
        WebBrowser1.Navigate(ToolStripTextBox1.Text)
    Else
        WebBrowser1.Navigate("https://www.google.com/?gws_rd=ssl#q=" & ToolStripTextBox1.Text)
    End If

End Sub

Private Function CheckWebPageLink(ByVal Link As String) As Boolean

    Dim CheckArrStart() As String = ({"http://www.", "https://www.", "www."})
    Dim CheckArrEnd() As String = ({".se", ".nl", ".com", ".cu", ".tk"})
    Dim Ok As Integer = 0

    For Each LinkStart As String In CheckArrStart
        If Link.Contains(LinkStart) Then
            Link = Replace(Link, LinkStart, "")
            Ok = Ok + 1
            For Each LinkEnd As String In CheckArrEnd
                If Link.Contains(LinkEnd) Then
                    Link = Replace(Link, LinkEnd, "")
                    Ok = Ok + 1
                    Exit For
                End If
            Next
            Exit For
        End If
    Next

    If Ok = 2 And Link.Count > 2 Then
        Return True
    Else
        Return False
    End If

End Function


回答2:

You can try using this function::

Private Function CheckURL(ByVal UrlText As String)
    Dim URL As New Uri(UrlText)
    Dim Req As System.Net.WebRequest
    Req = System.Net.WebRequest.Create(URL)
    Dim Resp As System.Net.WebResponse

    Try
        Resp = Req.GetResponse()
        Resp.Close()
        Req = Nothing
        Return True
    Catch ex As Exception
        Req = Nothing
        Return False
    End Try
End Function