Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

Google web service in asp.net web application

I have a google search web service working in my application but want to restrict the search to just my site.  But I can't work out where to put the url address.

Dim gs As New com.google.api.GoogleSearchService()
        Dim r As com.google.api.GoogleSearchResult = gs.doGoogleSearch(key, txtSearch.Text, 0, 10, True, "countryUK", False, "lang_en", "", "")
Any suggestions?
  • 6
  • 3
  • 3
1 Solution
Can you take a look at this page.

See View code. They had done it with setting the target and hidden fields only.

http://www.csharpcorner.com/ -->Go here and click search link on the top right corner.


Lookout this article.


Peter Bromberg had commentted atlast in his article.

"If you wanted to, you could get pretty sophisticated with this. One idea is to create a usercontrol that can simply be plugged into a web page at the location of your choice. You can also use the tool to restrict the search to only a particular site (like their "put Google Search on your site" feature) so that it only searches on your site."

Hope you got it.
I guess you can use this url to refer for your query.

You need to restrict google search to only your site.
There is a feature call "Site restriction"

Under above url look for "Special Query Terms"
In that topic there are details.

Site Restricted Search

 admission site:www.stanford.edu  

If you know the specific web site you want to search but aren't sure where the information is located within that site, you can use Google to search only within a specific web site.
Do this by entering your query followed by the string "site:" followed by the host name.

Note: The exclusion operator ("-") can be applied to this query term to remove a web site from consideration in the search.

Note: Only one site: term per query is supported.

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

I had this code tested and ran you just replace key with your own key value

<%@ Import Namespace="System.Text" %>
<Script Language="VB" Runat="Server">

Dim key As String="your key goes here", _
      maxResults As Integer, _
      formatString As String="<p><a href=""{1}"">{0}</a><br>{2}<br>{1}-{3}</p>"
Sub Page_Load(s As Object, e As EventArgs)
      If Not IsPostBack Then
      End If
End Sub

Sub Search(s As Object, e As EventArgs)
      resultlist.Text=doQuery(searchtext.Text, 0)
End Sub

Function doQuery(st As String, rc As Integer)
      Dim totalcount As Integer, _
            displaytitle As String, _
            sb As StringBuilder=New StringBuilder()
      Dim s As GoogleSearchService=New GoogleSearchService(), _
            r As GoogleSearchResult=s.doGoogleSearch( _
                  key, st, rc, maxresults, false, "", false, "", "", "")
      Dim result As ResultElement
      For Each result In r.resultElements
            If result.title <> "" Then
            End If
            sb.Append(String.Format( _
                  formatString, displayTitle, result.URL, result.snippet, result.cachedSize))
      If r.estimateIsExact And r.endIndex >= totalCount Then
      End If
      If rc=0 Then
      End If
      Return sb.ToString()
End Function

Sub NextClick(s As Object, e As EventArgs)
      resultList.Text=doQuery(searchtext.Text, Session("CurrentRecord"))
End Sub

Sub PreviousClick(s As Object, e As EventArgs)
      resultList.Text=doQuery(searchtext.Text, Int32.Parse(Session("CurrentRecord")))      
End Sub

<TITLE>Google API Example</TITLE>

<form runat="server">
            Google Search: <asp:textbox id="searchtext" runat="server" />
            <asp:button id="btn1" text="Search" runat="server" onclick="search" />
      <asp:panel id="headerpanel" runat="server">
                  <asp:label id="lblresults" runat="server" text="Total Results Returned:" />
                  <asp:label id="lbltotalresults" runat="server" />
                  <asp:label id="resultlist" runat="server" />
                  <asp:linkbutton id="lbprev" onclick="previousclick" visible="false" runat="server">Previous</asp:linkbutton>
                  <asp:linkbutton id="lbnext" onclick="nextclick" visible="false" runat="server">Next</asp:linkbutton>

you can also test and debug web services by using Web Services Studio

this tool will show you everything in details about what some particular WS requires, it made lots of thing very easy for me...
ladykhAuthor Commented:
This is the code for when I click the search button and it works to search many sites, but I still cannot get is to search one site?

Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
        Const key As String = "google key"

        Dim gs As New com.google.api.GoogleSearchService()
        Dim r As com.google.api.GoogleSearchResult = gs.doGoogleSearch(key, txtSearch.Text, 0, 10, True, "countryUK", False, "lang_en", "", "")
        Dim x As Integer
        Dim dt As New GoogleResultElement.ResultElementDataTable()
        If r.startIndex = r.endIndex Then
            lblGoogleMessage.Text = "No Results Found"
            For x = r.startIndex - 1 To r.endIndex - 1
                Dim dr As GoogleResultElement.ResultElementRow = dt.NewResultElementRow
                Dim re As com.google.api.ResultElement = r.resultElements(x)
                dr.summary = re.summary
                dr.URL = re.URL
                dr.snippet = re.snippet
                dr.title = re.title
                dr.CachedSize = re.cachedSize
                dr.relatedInformationPresent = re.relatedInformationPresent
                dr.hostName = re.hostName
                dr.directoryCategory = re.directoryCategory.fullViewableName
                dr.directoryTitle = re.directoryTitle
            DataGrid1.DataSource = dt
            lblGoogleMessage.Text = r.searchComments
        End If
    End Sub
> but I still cannot get is to search one site?

I don't get it...
ladykhAuthor Commented:
I just need it to search the pages in the website I am creating, not all of google. If this is possible?
ok, now I think I got you now... are you trying to search your site using google web service..? I think it depends how much info google has about your site, however you can use MS Index Server or any other third party software for your site. There is also a cool feature that you can integrate into your website, here just copy and paste

<FORM method=GET action="http://www.google.com/search">
<input type=hidden name=ie value=UTF-8>
<input type=hidden name=oe value=UTF-8>
<TABLE bgcolor="#FFFFFF"><tr><td>
<A HREF="http://www.google.com/">
<IMG SRC="http://www.google.com/logos/Logo_40wht.gif"
border="0" ALT="Google"></A>
<INPUT TYPE=text name=q size=31 maxlength=255 value="">
<INPUT type=submit name=btnG VALUE="Google Search">
<font size=-1>
<input type=hidden name=domains value="PUT YOUR DOMAIN NAME HERE"><br><input type=radio name=sitesearch value=""> WWW <input type=radio name=sitesearch value="PUT YOUR DOMAIN NAME HERE" checked> PUT YOUR DOMAIN NAME HERE <br>
ladykhAuthor Commented:
Thanks for your help.
you're welcome!

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 6
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now