- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试连接到 Oxford Dictionaries API .当我将 python 与此代码一起使用时,我没有遇到任何问题:
import requests
import json
app_id = 'my id'
app_key = 'my key'
language = 'en'
word_id = 'Ace'
url = 'https://od-api.oxforddictionaries.com:443/api/v1/entries/' + language + '/' + word_id.lower()
r = requests.get(url, headers = {'app_id': app_id, 'app_key': app_key})
print("code {}\n".format(r.status_code))
但是在 c# 中,我收到错误 403 Authentication failed using the following code:
HttpWebRequest req = null;
string PrimeUrl = "https://od-api.oxforddictionaries.com:443/api/v1/entries/en/"
string uri = PrimeUrl+word ;
req = (HttpWebRequest)HttpWebRequest.Create(uri);
string credentials = String.Format("{0}:{1}", uid, pwd);
byte[] bytes = Encoding.ASCII.GetBytes(credentials);
string base64 = Convert.ToBase64String(bytes);
string authorization = String.Concat("Basic ", base64);
req.Headers.Add("Authorization", authorization);
req.Method = WebRequestMethods.Http.Get;
req.Accept = "application/json";
HttpWebResponse HWR_Response = (HttpWebResponse)req.GetResponse();
我仔细检查了 id 和 key 是否正确,也尝试了所有建议 here .
发布这个问题是我最后的选择。谢谢。
最佳答案
这是将您的开发人员凭据正确添加到请求的方法 - 它们是 header :
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
namespace OxfDictionary
{
class Program
{
static void Main(string[] args)
{
HttpWebRequest req = null;
string PrimeUrl = "https://od-api.oxforddictionaries.com:443/api/v1/entries/en/";
string uri = PrimeUrl + "robot";
req = (HttpWebRequest)HttpWebRequest.Create(uri);
//These are not network credentials, just custom headers
req.Headers.Add("app_id", "5a......3");
req.Headers.Add("app_key", "d12............1a0");
req.Method = WebRequestMethods.Http.Get;
req.Accept = "application/json";
using (HttpWebResponse HWR_Response = (HttpWebResponse)req.GetResponse())
using (Stream respStream = HWR_Response.GetResponseStream())
using (StreamReader sr = new StreamReader(respStream, Encoding.UTF8))
{
string theJson = sr.ReadToEnd();
Debug.WriteLine(theJson);
Console.WriteLine(theJson);
}
Console.ReadKey();
}
}
}
结果是:
{
"metadata": {
"provider": "Oxford University Press"
},
"results": [
{
"id": "robot",
"language": "en",
"lexicalEntries": [
{
"entries": [
{
"etymologies": [
"from Czech, from robota forced labour. The term was coined in K. Čapek's play R.U.R. Rossum's Universal Robots (1920)"
],
"grammaticalFeatures": [
{
"text": "Singular",
"type": "Number"
}
],
"senses": [
{
"definitions": [
"a machine capable of carrying out a complex series of actions automatically, especially one programmable by a computer:"
],
"domains": [
"Electronics"
],
"examples": [
{
"text": "a robot arm"
},
{
"text": "half of all American robots are making cars or trucks"
}
],
"id": "m_en_gb0714510.001",
"subsenses": [
{
"definitions": [
"(especially in science fiction) a machine resembling a human being and able to replicate certain human movements and functions automatically:"
],
"examples": [
{
"text": "the robot closed the door behind us"
}
],
"id": "m_en_gb0714510.002"
},
{
"definitions": [
"a person who behaves in a mechanical or unemotional manner:"
],
"examples": [
{
"text": "public servants are not expected to be mindless robots"
}
],
"id": "m_en_gb0714510.003"
}
]
},
{
"crossReferenceMarkers": [
"another term for crawler (in the computing sense)"
],
"crossReferences": [
{
"id": "crawler",
"text": "crawler",
"type": "see also"
}
],
"domains": [
"Computing"
],
"id": "m_en_gb0714510.004"
},
{
"definitions": [
"a set of automatic traffic lights:"
],
"domains": [
"Motoring"
],
"examples": [
{
"text": "waiting at a robot I caught the eye of a young woman"
}
],
"id": "m_en_gb0714510.005",
"regions": [
"South African"
]
}
]
}
],
"language": "en",
"lexicalCategory": "Noun",
"pronunciations": [
{
"audioFile": "http://audio.oxforddictionaries.com/en/mp3/robot_gb_1.mp3",
"dialects": [
"British English"
],
"phoneticNotation": "IPA",
"phoneticSpelling": "ˈrəʊbɒt"
}
],
"text": "robot"
}
],
"type": "headword",
"word": "robot"
}
]
}
关于c# - 将 HttpWebRequest 凭证传递给 Oxford API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42040044/
我正在使用 HttpWebRequest 登录页面并获取一些信息。然后,我使用该信息创建一个新的 HttpWebRequest 以获取更多信息。我不想使用 WebClient。 如何将使用第一个 Ht
我有一个包含指向某些文件的链接的页面。 我基本上需要访问页面的源代码来解析它并获取文件的所有超链接。 我的代码是这样的(我在网上很多地方都找到了一些代码..): "private static
我正在编写一个渐进式下载器作为可移植类库 (Profile=24)。它将支持以字节 block 的形式部分下载目标文件。 HttpClient 不可用,我将使用 HttpWebRequest,它具有用
我正在努力在 HttpWebRequest 之上构建一个流畅的 REST 客户端界面。/HttpWebResponse .NET 中的类型。到目前为止,一切都很好......但是我正在尝试开发一个可插
我有一个用 VB.NET 编写的应用程序( 不是 asp.net,它是一个 Windows 控制台应用程序)。我正在尝试调用一个 url(一个 html 页面)并将响应返回到一个字符串中。响应是直接的
我尝试使用 C# HTTPWebRequest 类登录 amazon.com,我可以登录但无法读取 header 中的多个 set-cookie 当服务器响应多个“Set-Cookie:”-heade
我正在测试 Web 核心 API,但收到 500 内部服务器错误。 我的 Controller 上的方法是; [Route("api/property")] public class Property
假设我正在检索一个 url,如下所示: string url = "http://www.somesite.com/somepage.html" HttpWebRequest req = (HttpW
当我使用 HttpWebRequest.Headers.Add("Cookie",value) 与 HttpWebRequest.CookieContainer 和结果从 HttpWebRequest
我已阅读以下 2 篇文章并尝试实现相同的文章。 我的代码是这样的,超时发生在这里 HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
我正在寻找一种使用 httpWebRequest 从 url 下载 excel 文件并以某种方式解析它的方法 - 这是否意味着将其转换为 .csv 文件,以便我可以简单地使用 TextFieldPar
当我尝试序列化 HttpWebRequest 时出现以下错误 Type 'System.Net.KnownHttpVerb' in Assembly 'System, Version=2.0.0.0,
我目前正在下载一个 HTML 页面,使用以下代码: Try Dim req As System.Net.HttpWebRequest = DirectCast(WebRequest.Creat
我读了这个 MSDN 喜欢它并运行它的例子。 http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.useragent.a
我有一个 ASP.NET MVC 操作,它通过 HttpWebRequest 将 GET 请求发送到另一台服务器。我想在新请求中包含原始操作请求中的所有 cookie。原始请求中的一些 System.
我正在尝试抓取具有用户身份验证的网站。我能够执行POST来发送登录信息并存储Cookie。但是,登录后,尝试访问 protected 页面时出现403错误。 $url = "https://some_
我正在使用 HttpWebRequest ,并且正在处理响应流。 HttpWebRequest是否有正确的处理方法? ,因为它不包含 close 或 dispose 方法? 最佳答案 如果该类有特殊的
我在REST服务中抛出一个错误,例如: throw new WebFaultException("bla bla bla", HttpStatusCode.HttpVersionNotSuppo
问题:此控制台应用调用 Azure 上托管的长时间运行的网页两次。我希望它只调用一次。 控制台应用程序因捕获的异常而失败:基础连接已关闭:接收时发生意外错误。 so question 如果我从 Chr
我试图弄清楚我的网络请求在最终到达最终内容之前被重定向了多少次。 我正在创建我的网络请求,如下所示: var httpRequest = (HttpWebRequest) WebRequest.Cre
我是一名优秀的程序员,十分优秀!