API在C#中获取请求
我有一项任务,必须编写一个程序,该程序在WPF GUI(C#)上显示您位置的日出和日落时间。 为了完成这项工作,我需要使用 https://sunrise-sunrise-sunset.org/api < /a>
描述说:“我们的是非常简单的REST API,您只需要向https // api.sunrise-sunset.org/json提出get请求。”
由于我是初学者,而且我以前从未使用过API,所以我不知道如何提出请求。
因此,我的问题是:甚至是什么是GET请求,我该如何使用Sunrise-Sunset API?
干杯!
I have a task where I have to write a program which displays you the sunrise and sunset time at your location on a WPF GUI (C#).
To make this work, I need to use the Sunrise Sunset API from https://sunrise-sunset.org/api
The description says: "Ours is a very simple REST api, you only have to do a GET request to https//api.sunrise-sunset.org/json."
Because of the fact that I am a beginner and I've never used an API before, I don't know how to make a GET Request.
So my questions are: What even is a GET Request and how can I make one to use the sunrise-sunset API?
Cheers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据API文档,您应在API URI中通过
LAT
和lng
指定地理坐标。在下面的示例中,我将它们设置为lat = 36.7201600&amp; lng = -4.4203400
。然后,使用
httpclient.getStringasync
方法,我将get请求发送到URI。它以字符串返回响应主体。接下来,我将字符串对我的dto对象进行了序列化(
sunrisesunsetdto
)。然后获得日出和日落特性。这是方法:
这是
sunrisesunsetdto
class:According to the API document, you should specify the geographic coordinate by
lat
andlng
in the API Uri. In the following example, I set themlat=36.7201600&lng=-4.4203400
.Then, using
HttpClient.GetStringAsync
method, I send a GET request to the Uri. It returns the response body as a string.Next, I deserialize the string to my DTO object (here
SunriseSunsetDto
). and then get sunrise and sunset properties.Here is the methods:
And here is the
SunriseSunsetDto
class:因此,超文本传输协议(HTTP)是一件事,这是使我们能够浏览互联网的原因,它定义了网站的实际内容(以及更多内容)是如何传输的。因此,例如,爱丽丝想访问位于
http://www.example.org
的某些内容,她将该地址输入到浏览器中>!喜欢魔术,她可以看到页面,但是如何?由于http。HTTP可与动词(也称为方法)一起使用,这些动词是帖子,获取,put,put和delete的,但是构成大部分流量的两个动词是获取和发布的(可能是按照该顺序的),但是那意味着什么?基本上,这是一种传达意图的方式,意味着我打算获得一些资源,无论是某些HTML,JavaScript和CSS(网站)或一些JSON数据,或一些XML数据,或者确实有任何内容(您可以阅读更多信息动词表示在这里)。我们调用带有get动词a get request 的HTTP请求。
但是这是如何工作的呢?因此,在引擎盖下,HTTP是基于文本的协议,该文本中的第一件事始终是动词,其次是位置规范符,其次是HTTP版本要使用。 我们对日出 - 降低API的GET请求的第一行
因此,在HTTP标头之后, 。 HTTP标头基本上只是关于该请求的元数据,但是有一个是强制性的,许多网站也可以说:“嘿,您知道,我们需要一个授权标题作为您的请求的一部分”,但让我们只关注这个问题 ,这是 host host host heaster
强制性的是 这是从技术上讲,我们可以将TCP套接字打开与
api.sunrise-sunset.org
相关的IP地址,发送我们的文本,我们会得到一个响应(在此处进行一些简化),但这就是愚蠢,我们想自动化此过程,因此我们为其编写一些代码。但是首先让我们看一下返回的内容,特别是状态代码。 http定义了一些状态代码,您可以看到整个列表在这里。每个单独的状态代码都传达了有关我们的请求的信息,我不会详细介绍太多的细节,但是我会查看
200 OK
和404找不到
200 OK 意味着一切都很好。我们的请求经过了正确的处理,并按照我们告诉我们要做的事情进行了处理。在我们的情况下,我们还将获得我们要求的数据。
404找不到
是指我们在服务器上找不到的任何内容。这意味着我们必须查看我们的要求并更改一些内容。您可以通过转到不存在的页面来在浏览器中测试它,因此https://api.sunrise-sunset.org/banana
http请求中的响应也在文本格式从HTTP版本开始,然后是状态代码,然后是响应标头,最后是主体(或内容),并带有我们关心的数据。在我们的情况下,现在整个答案都是
让我们编写一些代码。
但是如何?
谷歌搜索
c#http
提出了 httpclient类,nateat。它甚至有一个例子,额外的整洁。
因此,让我们复制并粘贴该示例,修改请求URL,
现在我们得到了此示例,我们应该在控制台上查看JSON数据,是的!
但是,仅靠这确实不是很有帮助,我们将想将我们的JSON解析为一个对象,以便我们可以像任何旧的常规对象一样使用它。 Microsoft本身有一篇关于在这里
So, the Hypertext Transfer Protocol (HTTP) is a thing, it's the thing that allows us to browse the internet, it defines how the actual content of a website (and many more things) are transferred. So, say, Alice wants to access some content that's located at
http://www.example.org
, she'd enter that address into her browser and BAM! like magic she can see the page, but how? Because of HTTP.HTTP works with verbs (also called methods), those verbs are POST, GET, PUT, PATCH, and DELETE, but the two that make up a majority of traffic are GET and POST (probably in that order), but what does that mean? It's basically a way of conveying intent, GET means I intend to GET some resource, be it some HTML, JavaScript and CSS (a website) or some JSON data, or some XML data, or anything really (you can read more on what each verb means here). We call a HTTP request with the GET verb a GET request.
But how does that work? So, under the hood, HTTP is a text based protocol, and the first thing in that text is always the verb followed by a location specifier, followed by the HTTP version to use. So the first line of our GET request to the sunrise-sunset API is
After which come the HTTP headers. HTTP headers are basically just metadata about the request, there is one which is mandatory though, and many websites can also just say "hey, you know what, we need a authorization header as part of your request", but let's just focus on the mandatory one, it's the Host header, so
Knowing this, we could technically just open a TCP Socket to the IP address associated with
api.sunrise-sunset.org
, send our text, and we'd get a response (some oversimplification here), but that's dumb, we want to automate this process, so we'll write some code for it.But first lets take a look at what comes back, specifically the status code. HTTP defines a handful of status codes, you can see the entire list here. Each individual status code conveys something about our request, I won't go into too much detail, but I'll take a look at
200 OK
and404 Not Found
200 OK
means that everything went fine. Our request was properly processed and did what we told it to do. In our case we'll also get the data we requested back.404 Not Found
means that whatever we requested wasn't found on the server. This means we'll have to look at our request and change something. You can test this out in your browser by going to a page that doesn't exist, sohttps://api.sunrise-sunset.org/banana
The response from a HTTP request is also in a text format, starting with the HTTP version, followed by the status code, followed by response headers and finally the body (or content) with the data we care about. In our case, the entire answer is
Now let's write some code.
But how?
Googling
C# http
comes up with the documentation for the HttpClient class, neat.And it even has an example, extra neat.
So let's copy and paste that example, modifying the request URL, and we get this
Now we should see the JSON data on our console, yay!
But that alone really isn't very helpful, we'll want to parse (deserialize) our JSON into an object so we can work with it like any old regular object. Microsoft themselves have a great article about it here