csharp 获取帖子删除Put C#Generic Class

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 获取帖子删除Put C#Generic Class相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
/*
Husk at rette namespace
Du kan saette et default URL ved at replace [DEFAULT URL] med dit url
*/
namespace [namespace].Common
{
    public class ApiClient <T>
    {
        private string baseUrl = "";
        private string requestUrl = "";

        private bool useDefaultCredentials = false;

        public ApiClient(string requestUrl, string baseUrl)
        {
            this.requestUrl = requestUrl;
            this.baseUrl = baseUrl;

        }
        public ApiClient(string requestUrl) : this(requestUrl, "[DEFAULT URL]")
        {}
        
        public void UseDefaultCredentials()
        {
            useDefaultCredentials = true;
        }


        private HttpClientHandler GetHandler()
        {
            HttpClientHandler clientHandler = new HttpClientHandler();
            clientHandler.UseDefaultCredentials = useDefaultCredentials;

            return clientHandler;
        }

        public async Task<IEnumerable<T>> Get()
        {
            using (HttpClient client = new HttpClient(GetHandler()))
            {
                client.BaseAddress = new Uri(baseUrl);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                try
                {
                    var result = client.GetAsync(requestUrl).Result;
                    if (result.IsSuccessStatusCode)
                    {
                        return await result.Content.ReadAsAsync<IEnumerable<T>>();
                    }

                    throw new UnSuccesfulRequest(result.StatusCode.ToString());
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }
        public async Task<T> Get(Object id)
        {
            using (HttpClient client = new HttpClient(GetHandler()))
            {
                client.BaseAddress = new Uri(baseUrl);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                try
                {
                    var result = client.GetAsync(requestUrl+"/"+id).Result;
                    if (result.IsSuccessStatusCode)
                    {
                        return await result.Content.ReadAsAsync<T>();
                    }

                    throw new UnSuccesfulRequest(result.StatusCode.ToString());
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }
        public async Task<T> Post(T Obj)
        {
            using (HttpClient client = new HttpClient(GetHandler()))
            {
                client.BaseAddress = new Uri(baseUrl);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                try
                {
                    var result = client.PostAsJsonAsync(requestUrl, Obj).Result;
                    if (result.IsSuccessStatusCode)
                    {
                        return await result.Content.ReadAsAsync<T>();
                    }
                    throw new UnSuccesfulRequest(result.StatusCode.ToString());
                }
                catch (Exception e)
                {
                    throw e;
                }
            } 
        }
        public async Task<T> Update(Object updateId, T Obj)
        {
            using (HttpClient client = new HttpClient(GetHandler()))
            {
                client.BaseAddress = new Uri(baseUrl);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                try
                {
                    var result = client.PutAsJsonAsync(requestUrl + "/" + updateId, Obj).Result;

                    if (result.IsSuccessStatusCode)
                    {
                        return Obj;

                    }
                    throw new UnSuccesfulRequest(result.StatusCode.ToString());
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            
        }

        public async Task<bool> Delete(Object updateId)
        {
            using (HttpClient client = new HttpClient(GetHandler()))
            {
                client.BaseAddress = new Uri(baseUrl);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                try
                {
                    var result = client.DeleteAsync(requestUrl + "/" + updateId).Result;
                    if (result.IsSuccessStatusCode)
                    {
                        return true;
                    }

                    throw new UnSuccesfulRequest(result.StatusCode.ToString());
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }

    }

    public class UnSuccesfulRequest : Exception
    {
        public UnSuccesfulRequest()
        {
        }

        public UnSuccesfulRequest(string message) : base(message)
        {
        }

        public UnSuccesfulRequest(string message, Exception innerException) : base(message, innerException)
        {
        }
    }
}

以上是关于csharp 获取帖子删除Put C#Generic Class的主要内容,如果未能解决你的问题,请参考以下文章

csharp C#数据库删除

csharp 使用PUT或DELETE覆盖html表单方法

csharp C# - 执行和删除集合中的元素

我需要更改哪些内容才能从 SEDE 获取已删除、锁定的帖子的计数?

C# Web Api 2 PUT 和 POST 请求“不支持”

csharp 在C#中获取内存使用情况