How to send Ajax request, using HttpClient

How to send Ajax request, using HttpClient

Well, Actually you can't send real AJAX request using HttpClient, but you can simulate it by adding XMLHttpRequest to request header like this:

client.DefaultRequestHeaders.Add("X-Requested-With", "XMLHttpRequest");

Here is a Helper method which requires Microsoft.AspNet.WebApi.Client nuget package:

    private async TResponse CreateHttpRequestAsync<TRequest, TResponse>(Uri uri, TRequest request)
        where TRequest : class
        where TResponse : class, new()
    {
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Add("X-Requested-With", "XMLHttpRequest");
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _bearerToken);
            var response = await client.PostAsJsonAsync(uri, request);
            if (response.StatusCode == HttpStatusCode.OK)
            {
                var json = await response.Content.ReadAsStringAsync();
                return JsonConvert.DeserializeObject<TResponse>(json);
            }
            else if(response.StatusCode == HttpStatusCode.Unauthorized)
            {
                throw new UnauthorizedException();
            }
            else
            {
                throw new InvalidOperationException();
            }
        }

    }

 

POST http://localhost:8086/UK_60_Dev_Admin_Refactor/SelfRegistration/SelfRegistrationFormConfigurations HTTP/1.1
Host: localhost:8086
X-Requested-With: XMLHttpRequest
Cookie: __RequestVerificationToken_L1VLXzYwX0Rldl9BZG1pbl9SZWZhY3Rvcg2=L1r1hK6Veh32YofJUuQo0dBo4nm9jrVxB4MO44gq_AJXSbbSsGMU2-n3I0ppcIB5Q8cX3w2; AdminSession=lb5ymrsj0bic13j1icqmnslk; EdenredAdminSite=CA5A4E148A0ADF50AE0D44BABF0A6CB22613371D7EAE542C1FB6B643C25C99503D4C6FC03D45E56723BE65F049ECD846E356FB8B84326D1756096A63377CA8E5166F378C95ED0D20F7E77E54136F5ADF46D1266213B5628AF77CB588C14BBA991EED25642B6EC4F6C1AADBEDAFE92A8317DBC39768F4D77FDA6363D4EF591E937C55F8AFBFFC432895A62C39ADF029BCF1210D3B0ACCDAB0B79FB11B7D530CBAA1B2F036A6FA6F2EAED06198B16588546C242BD927430D01BF9C7BDDF499C8D5D591D6493106FA0BFC4830D9CE15C61483BDF27D10083EE398CB4C657CEAB3846DD220EF04AB0AB03D7D3AFD840B43702EAAD50E40F58F446F56C6FB885F6880777E3DAAC5AFCA9659A0C8817625DCA1D55B2BB688F0390C68DA8AF12E93446B19F7CCAB9C8423B27C3BA4E9
Content-Type: application/json
Content-Length: 520

{"tempModel":{"ActiveStatus":1,"FormDetail":"{\"MandatoryFields\":[{\"FieldName\":\"First name\",\"Tooltip\":\"first name\"},{\"FieldName\":\"Last name\",\"Tooltip\":\"last name\"},{\"FieldName\":\"Email Id\",\"Tooltip\":\"email\"},{\"FieldName\":\"Company\",\"Tooltip\":\"company\"},{\"FieldName\":\"secondary ID\",\"Tooltip\":\"mobile number\"}],\"OptionalFields\":[{\"Alias\":null,\"IsShow\":false,\"FieldName\":\"Participant ID\",\"Tooltip\":\"participant id\"}],\"EmailDomains\":[\"edenred.com\",\"google.com\"]}"}}

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(289)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2015-09-09 git stash的使用
点击右上角即可分享
微信分享提示