简体   繁体   English

使用C#的HTTP请求

[英]HTTP requests using c#

I'm rather new to sending/receiving over networks/sockets/network streams and so on. 我是通过网络/套接字/网络流等进行发送/接收的新手。

I'm making an IRC program that can communicate with Twitch.tv. 我正在制作一个可以与Twitch.tv通信的IRC程序。 They have an API, and they have examples of all sorts of requests you would use to get different kinds of information. 它们具有API,并且具有用于获取各种信息的各种请求的示例。

https://github.com/justintv/Twitch-API/tree/master/v3_resources https://github.com/justintv/Twitch-API/tree/master/v3_resources

One example of their requests is this: 他们的请求的一个例子是:

curl -H 'Accept: application/vnd.twitchtv.v3+json' \
-X GET https://api.twitch.tv/kraken/chat/kraken_test_user

I have tried to do some research on requests, and I sort of understand some, but for the most part I could not find any resources that help make it click for me. 我已尝试对请求进行一些研究,但我对一些请求有所了解,但是在大多数情况下,我找不到任何资源可以帮助我点击它。

In the above example, what are the important parts of that request? 在上面的示例中,该请求的重要部分是什么? curl? 卷曲? -H? -H? Is that one big command, or is it two commands separated by the \\ at the end of the first line? 这是一个大命令,还是第一行末尾用\\分隔的两个命令?

Then, the biggest question, how to send requests like the one above using C#? 然后,最大的问题是,如何使用C#发送类似上述请求的请求?

EDIT 1: 编辑1:

I also know that I will be getting responses in JSON. 我也知道我将获得JSON响应。 Is there anything built in that assists with receiving/parsing JSON? 有内置的东西可以帮助接收/解析JSON吗?

And also using PUT to change some JSON? 还使用PUT更改一些JSON吗? (some things in the API allow PUT). (API中的某些功能允许PUT)。

For the first bit of the question, you asked what are the important parts 对于第一个问题,您询问了哪些重要部分

  1. It has an accept header of application/vnd.twitchtv.v3+json 它具有application/vnd.twitchtv.v3+jsonaccept标头
  2. It is a GET request 这是一个GET请求
  3. The api url: https://api.twitch.tv/kraken/chat/kraken_test_user api网址: https://api.twitch.tv/kraken/chat/kraken_test_userhttps://api.twitch.tv/kraken/chat/kraken_test_user

This request in c# could look like the following (could because there is more than one way to do it) C#中的此请求可能如下所示(可能是因为有多种方法可以实现)

private async Task<object> GetRequest(string url)
{
    var httpClient = new HttpClient();
    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.twitchtv.v3+json"));

    var response = await httpClient.GetAsync(url);
    var contents = await response.Content.ReadAsStringAsync();

    return contents;
}

Note that the files in the link you posted are to Mark Down files that Google describes as: 请注意,您发布的链接中的文件是Mark Down文件,Google描述为:

MD, or markdown document is a text file created using one of several possible dialects of the Markdown language. MD或Markdown文档是使用Markdown语言的几种可能的方言之一创建的文本文件。 MD files use plain text formatting but includes inline text symbols that define how to format the text, and is designed for authoring plain text documentation that can be easily converted to HTML. MD文件使用纯文本格式,但包含定义文本格式的内联文本符号,并且设计用于编写可轻松转换为HTML的纯文本文档。

curl -H 'Accept: application/vnd.twitchtv.v3+json' \
-X GET https://api.twitch.tv/kraken/chat/kraken_test_user

http://curl.haxx.se/docs/manpage.html explains what the curl command is that then has 2 switches, H and X. where quoting the link: http://curl.haxx.se/docs/manpage.html解释了curl命令是什么,它具有2个开关H和X。在其中引用了链接:

-H, --header -H,--header

(HTTP) Extra header to include in the request when sending HTTP to a server. (HTTP)将HTTP发送到服务器时要包含在请求中的额外标头。 You may specify any number of extra headers. 您可以指定任意数量的额外标题。 Note that if you should add a custom header that has the same name as one of the internal ones curl would use, your externally set header will be used instead of the internal one. 请注意,如果您要添加一个自定义标头,其名称与curl将使用的内部标头之一相同,则将使用外部设置的标头,而不是内部标头。 This allows you to make even trickier stuff than curl would normally do. 这使您可以制作比卷发通常更棘手的东西。 You should not replace internally set headers without knowing perfectly well what you're doing. 如果不完全了解自己在做什么,则不应替换内部设置的标题。 Remove an internal header by giving a replacement without content on the right side of the colon, as in: -H "Host:". 通过在冒号的右侧给出不包含内容的替换内容来删除内部标头,例如:-H“ Host:”。 If you send the custom header with no-value then its header must be terminated with a semicolon, such as -H "X-Custom-Header;" 如果您发送的自定义标头没有值,则其标头必须以分号终止,例如-H“ X-Custom-Header;”。 to send "X-Custom-Header:". 发送“ X-Custom-Header:”。

curl will make sure that each header you add/replace is sent with the proper end-of-line marker, you should thus not add that as a part of the header content: do not add newlines or carriage returns, they will only mess things up for you. curl将确保您添加/替换的每个标头都带有正确的行尾标记,因此您不应将其添加为标头内容的一部分:不要添加换行符或回车符,它们只会弄乱东西为您服务。

See also the -A, --user-agent and -e, --referer options. 另请参见-A,--user-agent和-e,--referer选项。

Starting in 7.37.0, you need --proxy-header to send custom headers intended for a proxy. 从7.37.0开始,您需要--proxy-header发送用于代理的自定义标头。

Example: 例:

# curl -H "X-First-Name: Joe" http://192.168.0.1/ #curl -H“ X-名字:Joe” http://192.168.0.1/

WARNING: headers set with this option will be set in all requests - even after redirects are followed, like when told with -L, --location. 警告:使用此选项设置的标头将在所有请求中设置-即使在执行重定向后也是如此,例如使用-L,--location时。 This can lead to the header being sent to other hosts than the original host, so sensitive headers should be used with caution combined with following redirects. 这可能导致将标头发送到原始主机以外的其他主机,因此,应谨慎使用敏感标头,并结合以下重定向。

This option can be used multiple times to add/replace/remove multiple headers. 可以多次使用此选项来添加/替换/删除多个标题。

The "\\" makes the next line be added to the first line. “ \\”使下一行添加到第一行。

-X, --request -X,-请求

(HTTP) Specifies a custom request method to use when communicating with the HTTP server. (HTTP)指定与HTTP服务器通信时要使用的自定义请求方法。 The specified request method will be used instead of the method otherwise used (which defaults to GET). 将使用指定的请求方法代替其他方法(默认为GET)。 Read the HTTP 1.1 specification for details and explanations. 阅读HTTP 1.1规范以获取详细信息和说明。 Common additional HTTP requests include PUT and DELETE, but related technologies like WebDAV offers PROPFIND, COPY, MOVE and more. 常见的其他HTTP请求包括PUT和DELETE,但是WebDAV等相关技术提供PROPFIND,COPY,MOVE等。

Normally you don't need this option. 通常,您不需要此选项。 All sorts of GET, HEAD, POST and PUT requests are rather invoked by using dedicated command line options. 各种GET,HEAD,POST和PUT请求都可以通过使用专用的命令行选项来调用。

This option only changes the actual word used in the HTTP request, it does not alter the way curl behaves. 此选项仅更改HTTP请求中使用的实际单词,不会更改curl的行为方式。 So for example if you want to make a proper HEAD request, using -X HEAD will not suffice. 因此,例如,如果您想发出适当的HEAD请求,则使用-X HEAD将无法满足要求。 You need to use the -I, --head option. 您需要使用-I,--head选项。

The method string you set with -X will be used for all requests, which if you for example use -L, --location may cause unintended side-effects when curl doesn't change request method according to the HTTP 30x response codes - and similar. 使用-X设置的方法字符串将用于所有请求,例如,如果您使用-L,则在curl不根据HTTP 30x响应代码更改请求方法时,--location可能会导致意外的副作用-和类似。

(FTP) Specifies a custom FTP command to use instead of LIST when doing file lists with FTP. (FTP)指定使用FTP进行文件列表时要使用的自定义FTP命令而不是LIST。

(POP3) Specifies a custom POP3 command to use instead of LIST or RETR. (POP3)指定要使用的自定义POP3命令,而不是LIST或RETR。 (Added in 7.26.0) (在7.26.0中添加)

(IMAP) Specifies a custom IMAP command to use instead of LIST. (IMAP)指定要使用的自定义IMAP命令而不是LIST。 (Added in 7.30.0) (在7.30.0中添加)

(SMTP) Specifies a custom SMTP command to use instead of HELP or VRFY. (SMTP)指定要使用的自定义SMTP命令,而不是HELP或VRFY。 (Added in 7.34.0) (在7.34.0中添加)

If this option is used several times, the last one will be used. 如果多次使用此选项,则将使用最后一个。

In C#, there is a WebRequest class that https://msdn.microsoft.com/en-CA/library/456dfw4f(v=vs.110).aspx has a good example of how to use to get data from a given URL. 在C#中,有一个WebRequest类, https ://msdn.microsoft.com/zh-cn/library/456dfw4f( v = vs.110).aspx很好地说明了如何使用它从给定URL获取数据。

As for handling JSON, please look into http://www.newtonsoft.com/json which is a rather common library used for parsing JSON responses. 至于处理JSON,请查看http://www.newtonsoft.com/json ,这是用于解析JSON响应的相当常用的库。 PUT would be the HTTP verb like GET or POST used to tell the server how to process a request. PUT是HTTP动词,例如GET或POST,用于告诉服务器如何处理请求。 I'd suggest in the future be careful about posting a rather broad set of questions here as I could see this being something that a class could spend an hour covering somewhere that I doubt your intention is getting someone else to do your homework, right? 我建议以后在这里发布一系列相当广泛的问题时要小心,因为我看到这是一个班级可能要花一个小时讨论某个地方的事情,我怀疑您的意图是让别人去做作业,对吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM