使用此库(Google.Apis.Customsearch.v1, ver. 1.32.2.1146, in .Net VS2017 c# framework 4.7.1, from nuget)
,
偶尔,我收到以下错误:
...服务 xxxxxx 引发了异常: Google.GoogleApiException: Google.Apis.Requests.RequestErrorInvalid Value [400] 错误[消息[无效值]位置[ - ]原因[无效]域[全局]] 在Google.Apis.Requests.ClientServiceRequest 1.d__34.MoveNext() ---从生成异常的先前位置结束堆栈跟踪--- 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中 Google.Apis.Requests.ClientServiceRequest 1.Execute()in ...
在大多数情况下,一切都正常,代码没有任何变化。
使用的程序如下:
public DataTable RunSearch(string querySearch, string dateRestrict, string excludeTerms, string orTerms)
{
string apiKey = ConfigurationManager.AppSettings["gsApiKey"];
string searchEngineIdCX = ConfigurationManager.AppSettings["gsCX"];
DataTable dtRes = getDt(); // create a custom table
try
{
var customSearchService = new CustomsearchService(new BaseClientService.Initializer { ApiKey = apiKey });
var listRequest = customSearchService.Cse.List(querySearch);
listRequest.Cx = searchEngineIdCX;
listRequest.Googlehost = "google.it";
listRequest.DateRestrict = dateRestrict; // "d1"
listRequest.ExcludeTerms = excludeTerms;
listRequest.OrTerms = orTerms;
IList<Result> paging = new List<Result>();
int count = 0;
while (paging != null && dtRes.Rows.Count < 100) // max 100 free
{
listRequest.Start = count * 10 + 1; // every query gets 10 Results
paging = listRequest.Execute().Items; // Each lap is a query that scales from 100 available
if (paging != null)
{
foreach (var item in paging)
{
DataRow dr = dtRes.NewRow();
dr["Count"] = dtRes.Rows.Count + 1;
dr["Title"] = item.Title;
dr["Link"] = item.Link;
dr["Snippet"] = item.Snippet;
dtRes.Rows.Add(dr);
}
count++;
}
}
}
catch (Google.GoogleApiException gex)
{
string msgErr = "Error in " + this.GetType().ToString();
logger.Fatal(msgErr, gex);
throw;
}
catch (Exception ex)
{
string msgErr = "Error in " + this.GetType().ToString();
logger.Fatal(msgErr, ex);
throw;
}
return dtRes;
}
可能出现的错误是什么? 提前谢谢。