Necesitarás crear un CookieContainer
para recibir los cookies, algo similar a:
CookieContainer cookies = new CookieContainer();
HttpClientHandler handler = new HttpClientHandler();
handler.CookieContainer = cookies;
(...etc...)
Intenta con este código, sustituyendo los datos privados que espera tu servidor por los valores correctos:
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net;
public class Program
{
public static void Main()
{
CookieContainer cookies = new CookieContainer();
HttpClientHandler handler = new HttpClientHandler();
handler.CookieContainer = cookies;
HttpClient client = new HttpClient(handler);
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://idp.comprobanteselectronicos.go.cr/auth/realms/rut-stag/protocol/openid-connect/token");
var formData = new List<KeyValuePair<string, string>>();
formData.Add(new KeyValuePair<string, string>("client_id", "api-stag"));
formData.Add(new KeyValuePair<string, string>("grant_type", "password"));
formData.Add(new KeyValuePair<string, string>("username", "[email protected]"));
formData.Add(new KeyValuePair<string, string>("password", "fsdfs34234"));
formData.Add(new KeyValuePair<string, string>("scope", ""));
request.Content = new FormUrlEncodedContent(formData);
HttpResponseMessage response = client.GetAsync("https://idp.comprobanteselectronicos.go.cr/auth/realms/rut-stag/protocol/openid-connect/token").Result;
Uri uri = new Uri("http://google.com");
IEnumerable<Cookie> responseCookies = cookies.GetCookies(uri);
foreach (Cookie cookie in responseCookies)
Console.WriteLine(cookie.Name + ": " + cookie.Value);
Console.WriteLine(response.ToString());
}
}
Si pruebas este código de aquí abajo, tendrás un ejemplo práctico que funciona.
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net;
public class Program
{
public static void Main()
{
CookieContainer cookies = new CookieContainer();
HttpClientHandler handler = new HttpClientHandler();
handler.CookieContainer = cookies;
HttpClient client = new HttpClient(handler);
HttpResponseMessage response = client.GetAsync("http://google.com").Result;
Uri uri = new Uri("http://google.com");
IEnumerable<Cookie> responseCookies = cookies.GetCookies(uri);
Console.WriteLine("Cookies");
foreach (Cookie cookie in responseCookies)
Console.WriteLine(cookie.Name + ": " + cookie.Value);
Console.WriteLine("Respuesta:");
Console.WriteLine(response.ToString());
}
}
La respuesta que te enviará a la consola será similar a esta:
Cookies
NID: 215=UKQ5UQ7uZVjuW9cI1iwCtTkYy7Alk69OW2iyUEAOUfaT_JgBzYZJUwr9NZLmnqXoZRlxTddxVQqvkT4omciSWjimkpARqXTi3grSWtQiRRXO9_u0tW3Fl3URd7Cd7W3lmhYqiwPdsxtj5fh5bDE6GRWcVMlKgfYYCZkl092eRA8
Respuesta:
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
Date: Mon, 17 May 2021 14:46:05 GMT
Cache-Control: max-age=0, private
P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
Server: gws
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
Set-Cookie: 1P_JAR=2021-05-17-14; expires=Wed, 16-Jun-2021 14:46:05 GMT; path=/; domain=.google.com; Secure
Set-Cookie: NID=215=UKQ5UQ7uZVjuW9cI1iwCtTkYy7Alk69OW2iyUEAOUfaT_JgBzYZJUwr9NZLmnqXoZRlxTddxVQqvkT4omciSWjimkpARqXTi3grSWtQiRRXO9_u0tW3Fl3URd7Cd7W3lmhYqiwPdsxtj5fh5bDE6GRWcVMlKgfYYCZkl092eRA8; expires=Tue, 16-Nov-2021 14:46:05 GMT; path=/; domain=.google.com; HttpOnly
Accept-Ranges: none
Vary: Accept-Encoding
Transfer-Encoding: chunked
Expires: -1
Content-Type: text/html; charset=ISO-8859-1
}
response
, comoheader
?