• c# HttpClient 获取cookie


    在响应的Headers中获取
    string[] Cookie = (string[])response.Headers.GetValues(“Set-Cookie”);
    CookieValue = Cookie[0].Split(‘;’)[0];

    文章参考
    https://blog.csdn.net/qq_39569480/article/details/122559682
    https://blog.csdn.net/qq_39569480/article/details/112608041
    https://blog.csdn.net/qq_39569480/article/details/108996039

    代码参考

     				using(HttpClient client = new HttpClient()) 非注入方式 
                    {
                        string responseBody = string.Empty;
                        client.DefaultRequestHeaders.Add("Method", "Post");
                        var content = new StringContent(parameter, Encoding.UTF8, ContentType);
                        if (!string.IsNullOrEmpty(AuthType))
                        {
                            switch (AuthType)
                            {
                                //Bearer认证      如果接口未加密省略此行
                                case "Bearer": client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token); break;
                                case "Cookie": client.DefaultRequestHeaders.Add("Cookie", token); break;
    
                            } 
    
                            //var authorizationBased64 = "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes($"{userName}:{password}"));//basic认证方式   用户名:密码 然后base64加密
                            //client.DefaultRequestHeaders.Add("Authorization", authorizationBased64);
                        }
                        HttpResponseMessage response = await client.PostAsync(Url, content);
                        //response.EnsureSuccessStatusCode();
                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            responseBody = response.Content.ReadAsStringAsync().Result;
                            dynamic res = JsonConvert.DeserializeObject<dynamic>(responseBody);
                            string CookieValue = "";
                            if (AuthType == "Cookie"&& Url.EndsWith("login")) {
                                string[] Cookie = (string[])response.Headers.GetValues("Set-Cookie");
                                CookieValue = Cookie[0].Split(';')[0];
                            }
                            return new { ok = true, isTryException = false, cookie = CookieValue, data = res };
                        }
                        else
                        {
                            responseBody = response.Content.ReadAsStringAsync().Result;
                            
                            return new { ok = false, isTryException = false, cookie = "", data = responseBody };
                        }
                    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
  • 相关阅读:
    第十九章 ES6语法
    树莓派4B开发之四树莓派3B安装Miniconda
    【LeetCode:1402. 做菜顺序 | 动态规划 + 贪心】
    [剑指Offer] 三种方法求解找出数组中出现次数超过一半的数字
    Vue父子组件传参
    27_Scala功能函数
    HTML期末学生大作业-使用HTML+CSS技术仿传智博客网站
    A-level经济例题解析及练习Price Control
    苹果macOS电脑版 植物大战僵尸游戏
    Python全栈开发【基础-03】编程语言的分类
  • 原文地址:https://blog.csdn.net/qq_39569480/article/details/127422468