zoukankan      html  css  js  c++  java
  • 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"]}"}}

  • 相关阅读:
    安装activeMQ(解压版)
    java匿名发送邮件(不需要登录)
    poi excel导入整数数字自动加小数点解决
    读取excel表中的数据
    DES加密解密实现
    验证码实现(google)
    下载文件到电脑本地
    Properties条件查询(适用于StorageAPI操作数据库)
    java位运算
    python测试开发工具库汇总(转载)
  • 原文地址:https://www.cnblogs.com/chucklu/p/13637536.html
Copyright © 2011-2022 走看看