来自Google CSE请求的错误400

时间:2014-04-23 23:59:26

标签: c# google-search-api

我使用Visual C#Express 2010和Google自定义搜索API以及Newtonsoft.Json.dll。我收到了来自Google的400错误消息。我想我没有正确形成搜索字符串(mySearchString),但我看不到错误。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using Newtonsoft.Json;

namespace Test_Console1
{
    class Program
    {    
        //Google keys                                                   
        const string APIKey = "{key}";                                                  
        const string CSEKey = "{key}";
        //base url for the search query                                                 
        const string GoogleBaseURL = "https://www.googleapis.com/customsearch/v1?";

        public static void Main (string[] args)                                                 
        {                                                   
            string myQuery = "Action Motivation, Inc. South San Francisco";                                             
            int startResult = 0;                                                
            int numResults = 10;
            string result;
            result = submitSearch(makeSearchString(myQuery, startResult, numResults));
            Console.WriteLine(result);
            string dummy = Console.ReadLine();            
        }                                                   

        public static string makeSearchString(string myQuery, int startResult, int numResults)                                                  
        {                                                   
            //add keys
            string mySearchString = GoogleBaseURL + "key=" + APIKey + "&cx=" + CSEKey + "&q=";

            //add query string: replace space+plus sign pattern with just a plus sign                                       
            string[] keys = myQuery.Split(' '); 
            foreach(string key in keys)
            {
                mySearchString += key +"+"; //append keywords
            }

            //specify JSON response format
            mySearchString += "&alt=json";
            //specify starting result number
            mySearchString += "&start=" + startResult;                                              
            //specify number of results                                             
            mySearchString += "&num=" + numResults;                                             
            return mySearchString;                                              
        }
        public static string submitSearch(string mySearchString)                
        {
            //try
            //{
                Uri url = new Uri(mySearchString);                
                //url looks like this:
                //"https://www.googleapis.com/customsearch/v1?key=AZaaAaAaA0aaAA0ZAZA0aaAA00aaA0aaAaAa0aA&cx=012345678901234567890:aaaaaaaa0aa&q=Action+Motivation,+Inc.+South+San+Francisco+&alt=json&start=0&num=10"
                WebRequest myRequest = WebRequest.Create(url);
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                Stream myStream = myResponse.GetResponseStream ();
                StreamReader myReader = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
                string result = myReader.ToString();
                //JObject myJo = JObject.Parse(myReader.ReadToEnd());
                //int resultcount = (int)myJo.SelectToken("responseData.cursor.estimatedResultCount");
                myStream.Close();
                myReader.Close();
                myResponse.Close();
                return result;
            //}
            //catch (Exception e)
            //{
            //  //debug statement       
            //}         
            return null;
        }
    }
}

0 个答案:

没有答案