【发布时间】:2016-03-09 16:24:11
【问题描述】:
我创建了一个 Google 自定义搜索,我可以使用 JSON/Atom 自定义搜索 API 成功调用它,然后当我在 aspx 页面中有一个脚本标记时,将结果从它加载到一个 javascript 函数中:
<script src="https://www.googleapis.com/customsearch/v1?key={my key}&cx={my CX code};q=Shell&callback=hndlr"></script>
但是,我试图在 C# 中发出这个 GET 请求,但我似乎无法让它工作。我之前没有在 C# 中发出过 GET 请求,所以我确定我在某个地方犯了一个基本错误。我正在尝试的代码如下:
System.Net.HttpWebRequest hwb;
String url = "https://www.googleapis.com/customsearch/v1?key={my key}&cx={my cx code}&q=Shell";
hwb = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
HttpWebResponse resp = (HttpWebResponse)hwb.GetResponse();
在最后一行调用 GetResponse() 时返回以下错误:
“远程服务器返回错误:(400) Bad Request。”
任何帮助指出我哪里出错了 - 即使只是为了确认这个特定的 API 不能以这种方式使用!
【问题讨论】:
标签: c# json google-search