T
T
TechNOIR2018-03-06 15:53:34
C++ / C#
TechNOIR, 2018-03-06 15:53:34

C#. Which component is better to interact with the API?

Good afternoon.
Which component is better to interact with API in C#?
I just started studying, I want to understand the mechanism of working with API in C#.
I did it in Powershell, but everything is a little different.
What is better to use? httpwebrequest?
Can you show an example of a GET request? Thanks in advance!

Answer the question

In order to leave comments, you need to log in

4 answer(s)
C
cicatrix, 2018-03-06
@cicatrix

What API? There are millions of them.
If you are interested in how to make a web request, then:

// using System.IO
// using System.Net
...
            WebRequest wrq = WebRequest.Create(url);
            string content;
            WebResponse response = wrq.GetResponse();
            using (StreamReader rdr = new StreamReader(response.GetResponseStream()))
            {
                content = rdr.ReadToEnd();
            } // using rdr

A
Alexander Yudakov, 2018-03-06
@AlexanderYudakov

https://docs.microsoft.com/en-us/dotnet/framework/...

M
mindgrow, 2018-03-06
@mindgrow

To work with the SOAP API in Visual Studio there is a ServiceReference
To work with the REST API RestClient. I have an example of how to use it on github.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace VkSdk
{
    public enum HttpVerb
    {
        GET,
        POST,
        PUT,
        DELETE
    }

    public class RestClient
    {

        #region CONSTRUCTOR
        public string EndPoint { get; set; }
        public HttpVerb Method { get; set; }
        public string ContentType { get; set; }
        public string PostData { get; set; }

        public RestClient()
        {
            EndPoint = "";
            Method = HttpVerb.GET;
            ContentType = "text/xml";
            PostData = "";
        }
        public RestClient(string endpoint)
        {
            EndPoint = endpoint;
            Method = HttpVerb.GET;
            ContentType = "text/xml";
            PostData = "";
        }
        public RestClient(string endpoint, HttpVerb method)
        {
            EndPoint = endpoint;
            Method = method;
            ContentType = "text/xml";
            PostData = "";
        }

        public RestClient(string endpoint, HttpVerb method, string postData)
        {
            EndPoint = endpoint;
            Method = method;
            ContentType = "text/xml";
            PostData = postData;
        }
        #endregion



        public string MakeRequest()
        {
            return MakeRequest("");
        }

        public string MakeRequest(string parameters)
        {
            var request = (HttpWebRequest)WebRequest.Create(EndPoint + parameters);

            request.Method = Method.ToString();
            request.ContentLength = 0;
            request.ContentType = ContentType;
            


            if (!string.IsNullOrEmpty(PostData) && Method == HttpVerb.POST)
            {
                var encoding = new UTF8Encoding();
                var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(PostData);
                request.ContentLength = bytes.Length;

                using (var writeStream = request.GetRequestStream())
                {
                    writeStream.Write(bytes, 0, bytes.Length);
                }
            }

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                var responseValue = string.Empty;

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
                    throw new ApplicationException(message);
                }

                // grab the response
                using (var responseStream = response.GetResponseStream())
                {
                    if (responseStream != null)
                        using (var reader = new StreamReader(responseStream))
                        {
                            responseValue = reader.ReadToEnd();
                        }
                }

                return responseValue;
            }
        }

        
    }
}

A
Artem Voronov, 2018-03-06
@newross

RestEase is a very useful tool.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question