简体   繁体   中英

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. They have an API, and they have examples of all sorts of requests you would use to get different kinds of information.

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? 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#?

EDIT 1:

I also know that I will be getting responses in JSON. Is there anything built in that assists with receiving/parsing JSON?

And also using PUT to change some JSON? (some things in the API allow 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
  2. It is a GET request
  3. The api url: https://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)

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:

MD, or markdown document is a text file created using one of several possible dialects of the Markdown language. 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.

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:

-H, --header

(HTTP) Extra header to include in the request when sending HTTP to a server. 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. 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:". If you send the custom header with no-value then its header must be terminated with a semicolon, such as -H "X-Custom-Header;" to send "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.

See also the -A, --user-agent and -e, --referer options.

Starting in 7.37.0, you need --proxy-header to send custom headers intended for a proxy.

Example:

# curl -H "X-First-Name: 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. 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

(HTTP) Specifies a custom request method to use when communicating with the HTTP server. The specified request method will be used instead of the method otherwise used (which defaults to GET). Read the HTTP 1.1 specification for details and explanations. Common additional HTTP requests include PUT and DELETE, but related technologies like WebDAV offers PROPFIND, COPY, MOVE and more.

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.

This option only changes the actual word used in the HTTP request, it does not alter the way curl behaves. So for example if you want to make a proper HEAD request, using -X HEAD will not suffice. You need to use the -I, --head option.

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.

(FTP) Specifies a custom FTP command to use instead of LIST when doing file lists with FTP.

(POP3) Specifies a custom POP3 command to use instead of LIST or RETR. (Added in 7.26.0)

(IMAP) Specifies a custom IMAP command to use instead of LIST. (Added in 7.30.0)

(SMTP) Specifies a custom SMTP command to use instead of HELP or VRFY. (Added in 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.

As for handling JSON, please look into http://www.newtonsoft.com/json which is a rather common library used for parsing JSON responses. PUT would be the HTTP verb like GET or POST used to tell the server how to process a request. 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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