Wednesday, 29 July 2020

How to HTTP Client Cookie c#?

How to HTTP Client Cookie c#?

string baseClientUri = "https://test.com/";
                var cookieContainer = new CookieContainer();
                using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
                using (var client = new HttpClient(handler))
                {
                    client.BaseAddress = new Uri(baseClientUri);
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    client.DefaultRequestHeaders.Add("X-Requested-With", "XMLHttpRequest");
                    cookieContainer.Add(client.BaseAddress, new Cookie("SSOCookie", cookie));
                    var body = "";
                    string postBody = JsonConvert.SerializeObject(body);
                    System.Net.ServicePointManager.ServerCertificateValidationCallback += delegate { return true; };
                    HttpContent content = new StringContent(postBody, Encoding.UTF8, "application/json");
                    var response = client.PostAsync("QADBApi/AutoGatherRotorAssembly", content).Result;
                    if (response != null && response.ReasonPhrase == "OK")
                    {
                        var authToken = response.Content.ReadAsStringAsync().Result;
                        if (authToken == "true")
                        {
                        }
                    }
                }

No comments:

Post a Comment