c# API 使用桌面应用程序的客户端

c# API client using Desktop application

我必须创建一个客户端应用程序来连接到 API 到 post 并获取信息,
我从拥有 API 的公司那里得到的信息是:

API details for: xxxxx 
Integration: yyyyxxxx 
API URL: https://api.xyz.us/v123/ 
APP KEY: xxxxx12312xxxxx2123123xxxxx 
SECRET KEY: 11111111111111111xxxxxxxxxxx 

我想使用 c# 或其他语言来创建客户端,我找不到任何用于桌面应用程序的 c# 示例,我找到了一个 c# 控制台基本示例,好像没有人使用 APIs c#,

是否有客户端使用 C# 桌面应用程序连接到 API 的示例?

或者我可以用什么来完成这个任务?

谢谢

感谢所有帮助过的人。我在邮递员上找到了答案, 首先,我找到了一个 C# 桌面客户端应用程序示例。 此示例的问题在于它没有使用 API 密钥和密钥。 我找到了很多关于如何添加 API 密钥和密钥

的答案

但在 'code'

部分中,Postman 上的那个起作用了

request.Headers["授权"] = "基本 ZmRiODA0NjMxMTgwMWUzYWFkYTk4NjM2MjcyOTk3MDowYTU0N2I2NzliNWRkMjliN2I4NTFlMDBkY2Y2NjQzNzQ5OTIxYzZl";

完整的 C# 代码是:

using System;
using System.IO;
using System.Net;
using System.Text;

namespace restClient_0
{

    public enum httpVerb
    {
        GET,
        POST,
        PUT,
        DELETE
    }

    class RESTClient
    {
        public string endPoint { get; set; }
        public httpVerb httpMethod { get; set; }

        public RESTClient()
        {
            endPoint = "";
            httpMethod = httpVerb.GET;
        }

        public string makeRequest()
        {

            string strResponseValue = string.Empty;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endPoint);

            request.Method = httpMethod.ToString();
            request.Headers["Authorization"] = "Basic iODA0NjMxMTgwMWUzYWFkYTk4NjM2MjcyOTk3MDowYTU0N2I2NzliNWRkMjliN2I4NTFlMDBkY2Y2NjQzNzQ5OTIxYzZl";

            HttpWebResponse response = null;

            try
            {
                response = (HttpWebResponse)request.GetResponse();


                //Proecess the resppnse stream... (could be JSON, XML or HTML etc..._

                using (Stream responseStream = response.GetResponseStream())
                {
                    if (responseStream != null)
                    {
                        using (StreamReader reader = new StreamReader(responseStream))
                        {
                            strResponseValue = reader.ReadToEnd();
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                strResponseValue = "{\"errorMessages\":[\"" + ex.Message.ToString() + "\"],\"errors\":{}}";
            }
            finally
            {
                if (response != null)
                {
                    ((IDisposable)response).Dispose();
                }
            }

            return strResponseValue;
        }
    }
}

这是基于 OP 回答

片段的 HttpClient 示例
using System.Threading.Tasks;
using System.Net.Http;
using System.Text;
using System.Net.Http.Headers;
public class RESTClient
{
    private readonly HttpClient client = new HttpClient();

    public RESTClient(string baseAddress)
    {
        client.BaseAddress = new Uri(baseAddress);
    }

    public void SetAuthHeader(string parameter)
    {
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", parameter);
    }

    public async Task<string> MakeRequestAsync(HttpMethod method, string path, string postContent = "")
    {
        try
        {
            using (HttpContent content = new StringContent(postContent, Encoding.UTF8, "application/json"))
            using (HttpRequestMessage request = new HttpRequestMessage(method, path))
            {
                if (method == HttpMethod.Post || method == HttpMethod.Put) request.Content = content;
                using (HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false))
                {
                    response.EnsureSuccessStatusCode();
                    return await response.Content.ReadAsStringAsync().ConfigureAwait(false);
                }
            }
        }
        catch (Exception ex)
        {
            return "{\"errorMessages\":[\"" + ex.Message + "\"],\"errors\":{}}";
        }
    }
}

使用示例 (WinForms)

private RESTClient restClient;
private void Form1_Load(object sender, EventArgs e)
{
    restClient = new RESTClient("https://myapi.url/api");
    restClient.SetAuthHeader("iODA0NjMxMTgwMWUzYWFkYTk4NjM2MjcyOTk3MDowYTU0N2I2NzliNWRkMjliN2I4NTFlMDBkY2Y2NjQzNzQ5OTIxYzZl");
}

private async void button1_Click(object sender, EventArgs e)
{
    // GET
    string getJsonResult = await restClient.MakeRequestAsync(HttpMethod.Get, "path/to/method");

    // POST
    string postJsonResult = await restClient.MakeRequestAsync(HttpMethod.Post, "path/to/method", "{\"data\": \"Some Request Data\"}");


    // process data here
}