Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 来自Google CSE请求的错误400_C#_Google Search Api - Fatal编程技术网

C# 来自Google CSE请求的错误400

C# 来自Google CSE请求的错误400,c#,google-search-api,C#,Google Search Api,我正在使用Visual C#Express 2010和Google自定义搜索API以及Newtonsoft.Json.dll。我从谷歌那里得到了400个错误。我想我没有正确形成搜索字符串(mySearchString),但我看不到错误 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; using Newto

我正在使用Visual C#Express 2010和Google自定义搜索API以及Newtonsoft.Json.dll。我从谷歌那里得到了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;
        }
    }
}

你需要对你的查询字符串参数进行URL编码?我不确定应该包装哪些参数。或者你是说使用这样的工具?斯拉克斯:谢谢。我现在使用的是这种格式: