How to get HttpClient to pass credentials along with the request?

前端 未结 7 1698
南笙
南笙 2020-11-22 14:52

I have a web application (hosted in IIS) that talks to a Windows service. The Windows service is using the ASP.Net MVC Web API (self-hosted), and so can be communicated with

7条回答
  •  借酒劲吻你
    2020-11-22 15:35

    Ok so I took Joshoun code and made it generic. I am not sure if I should implement singleton pattern on SynchronousPost class. Maybe someone more knowledgeble can help.

    Implementation

    //I assume you have your own concrete type. In my case I have am using code first with a class called FileCategory

    FileCategory x = new FileCategory { CategoryName = "Some Bs"};
    SynchronousPosttest= new SynchronousPost();
    test.PostEntity(x, "/api/ApiFileCategories"); 
    

    Generic Class here. You can pass any type

     public class SynchronousPostwhere T :class
        {
            public SynchronousPost()
            {
                Client = new WebClient { UseDefaultCredentials = true };
            }
    
            public void PostEntity(T PostThis,string ApiControllerName)//The ApiController name should be "/api/MyName/"
            {
                //this just determines the root url. 
                Client.BaseAddress = string.Format(
             (
                System.Web.HttpContext.Current.Request.Url.Port != 80) ? "{0}://{1}:{2}" : "{0}://{1}",
                System.Web.HttpContext.Current.Request.Url.Scheme,
                System.Web.HttpContext.Current.Request.Url.Host,
                System.Web.HttpContext.Current.Request.Url.Port
               );
                Client.Headers.Add(HttpRequestHeader.ContentType, "application/json;charset=utf-8");
                Client.UploadData(
                                     ApiControllerName, "Post", 
                                     Encoding.UTF8.GetBytes
                                     (
                                        JsonConvert.SerializeObject(PostThis)
                                     )
                                 );  
            }
            private WebClient Client  { get; set; }
        }
    

    My Api classs looks like this, if you are curious

    public class ApiFileCategoriesController : ApiBaseController
    {
        public ApiFileCategoriesController(IMshIntranetUnitOfWork unitOfWork)
        {
            UnitOfWork = unitOfWork;
        }
    
        public IEnumerable GetFiles()
        {
            return UnitOfWork.FileCategories.GetAll().OrderBy(x=>x.CategoryName);
        }
        public FileCategory GetFile(int id)
        {
            return UnitOfWork.FileCategories.GetById(id);
        }
        //Post api/ApileFileCategories
    
        public HttpResponseMessage Post(FileCategory fileCategory)
        {
            UnitOfWork.FileCategories.Add(fileCategory);
            UnitOfWork.Commit(); 
            return new HttpResponseMessage();
        }
    }
    

    I am using ninject, and repo pattern with unit of work. Anyways, the generic class above really helps.

提交回复
热议问题