예제 #1
0
  }//Search()

  /// <summary>doSpellingSuggestion</summary>
  public static string doSpellingSuggestion(string question)
  {
    // Create a Google Search object
    GoogleSearchService googleSearchService = new GoogleSearchService();
    String spellingSuggestion = googleSearchService.doSpellingSuggestion(LicenceKey[0], question);
    System.Console.WriteLine("{0}", spellingSuggestion);
    return(spellingSuggestion);
  }
예제 #2
0
  }//public static void Main(string[] argv)

  ///<summary>Search: Display the number of results.</summary>
  public static void Search
  (
   ref string        question,
   ref int           maxResults,
   ref string        exceptionMessage,
   ref StringBuilder sbResultElement
  )
  {
   int                  estimatedResult      = -1;
   HttpContext          httpContext          = HttpContext.Current;

   GoogleSearchService  googleSearchService  = null;
   GoogleSearchResult   googleSearchResult   = null;
   ResultElement[]      resultElements       = null;

   sbResultElement = new StringBuilder();
   
   //Create a Google Search object
   googleSearchService = new GoogleSearchService();
   try 
   {
    //Invoke the search method.
    googleSearchResult = googleSearchService.doGoogleSearch
    (
     LicenceKey[0], //license key.
     question,      //q: question.
     0,             //start: default is 0.
     maxResults,    //maxResults: maximum results, default is 10.
     false,         //filter: default is true.
     "",            //restrict:
     false,         //safeSearch: safe search, default is false.
     "",            //lr: Language restrict.
     "",            //ie: Input enconding.
     ""             //oe: Output enconding.
    );
    // Extract the estimated number of results for the search and display it
    estimatedResult = googleSearchResult.estimatedTotalResultsCount;
    
    // Returns an array of result elements. This corresponds to the actual list of search results.
    resultElements  = googleSearchResult.resultElements;
    
	System.Console.WriteLine("Estimated number of results: {0}", estimatedResult);
	if ( httpContext != null )
	{
	 httpContext.Response.Write
	 (
	  "Estimated number of results: " + 
	  System.Convert.ToString(estimatedResult) 
	 );
    }//if ( httpContext != null )
    foreach ( ResultElement resultElement in resultElements )
    {
	 System.Console.WriteLine
	 (
      "Summary:                            " + resultElement.summary                            + System.Environment.NewLine +
      "URL:                                " + resultElement.URL                                + System.Environment.NewLine +
      "Snippet:                            " + resultElement.snippet                            + System.Environment.NewLine +
      "Title:                              " + resultElement.title                              + System.Environment.NewLine +
      "CachedSize:                         " + resultElement.cachedSize                         + System.Environment.NewLine +
      "RelatedInformationPresent:          " + resultElement.relatedInformationPresent          + System.Environment.NewLine +
      "HostName:                           " + resultElement.hostName                           + System.Environment.NewLine +
      "DirectoryTitle:                     " + resultElement.directoryTitle                     + System.Environment.NewLine +
      "DirectoryCategory FullViewableName: " + resultElement.directoryCategory.fullViewableName + System.Environment.NewLine +
      "DirectoryCategory SpecialEncoding:  " + resultElement.directoryCategory.specialEncoding
     );

	 if ( httpContext != null )
	 {
	  httpContext.Response.Write
	  (
	   System.Environment.NewLine +
       "Summary:                            " + resultElement.summary                            + System.Environment.NewLine +
       "URL:                                " + resultElement.URL                                + System.Environment.NewLine +
       "Snippet:                            " + resultElement.snippet                            + System.Environment.NewLine +
       "Title:                              " + resultElement.title                              + System.Environment.NewLine +
       "CachedSize:                         " + resultElement.cachedSize                         + System.Environment.NewLine +
       "RelatedInformationPresent:          " + resultElement.relatedInformationPresent          + System.Environment.NewLine +
       "HostName:                           " + resultElement.hostName                           + System.Environment.NewLine +
       "DirectoryTitle:                     " + resultElement.directoryTitle                     + System.Environment.NewLine +
       "DirectoryCategory FullViewableName: " + resultElement.directoryCategory.fullViewableName + System.Environment.NewLine +
       "DirectoryCategory SpecialEncoding:  " + resultElement.directoryCategory.specialEncoding  + System.Environment.NewLine 
      );
     }//if ( httpContext != null )      

     sbResultElement.AppendFormat
     (
      UtilityURI.Anchor,
      resultElement.URL,
      resultElement.title
     );
     sbResultElement.Append( "<br/>" );
    }//foreach ( ResultElement resultElement in resultElements )
   }//try
   catch (System.Web.Services.Protocols.SoapException ex) 
   {
    System.Console.WriteLine(ex.Message);
   }//catch (System.Web.Services.Protocols.SoapException ex)
  }//Search()