【问题标题】:How to make a GET REST API request to Google Custom Search API in C#如何在 C# 中向 Google 自定义搜索 API 发出 GET REST API 请求
【发布时间】:2016-03-09 16:24:11
【问题描述】:

我创建了一个 Google 自定义搜索,我可以使用 JSON/Atom 自定义搜索 API 成功调用它,然后当我在 aspx 页面中有一个脚本标记时,将结果从它加载到一个 javascript 函数中:

<script src="https://www.googleapis.com/customsearch/v1?key={my key}&amp;cx={my CX code};q=Shell&amp;callback=hndlr"></script>

但是,我试图在 C# 中发出这个 GET 请求,但我似乎无法让它工作。我之前没有在 C# 中发出过 GET 请求,所以我确定我在某个地方犯了一个基本错误。我正在尝试的代码如下:

System.Net.HttpWebRequest hwb;

String url = "https://www.googleapis.com/customsearch/v1?key={my key}&amp;cx={my cx code}&amp;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


    【解决方案1】:

    如果您在网站上拥有的正是您想要的,请将 URL 中的 &amp;amp; 序列替换为 &amp;

    【讨论】:

    • 是的,就是这样!我只需要另一双眼睛才能看到我的错误。谢谢马特!
    猜你喜欢
    • 2019-09-05
    • 1970-01-01
    • 2017-05-05
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    • 2017-06-02
    • 2023-02-05
    相关资源
    最近更新 更多