Converting curl flags to HttpClient
-L
HttpClient
should automatically follow redirects because HttpClientHandler.AllowAutoRedirect
defaults to true
.
--negotiate -u :
HttpClient
will handle negotiation if you supply its constructor with a HttpClientHandler
that supplies it with credentials. Since you're using default Windows credentials with -u :
, you can use the code from this answer:
var client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true });
-b ~/cookiejar.txt
HttpClient
can store cookies by supplying its constructor with an HttpClientHandler
with a CookieContainer
:
var client = new HttpClient(new HttpClientHandler() { CookieContainer = new CookieContainer() })
This depends on the HttpClientHandler
having UseCookies = true
, but it is true
by default.
Returning Contents
You can use HttpClient.GetAsync
and HttpResponseMessage.Content
to read the response of the HttpClient
var response = await client.GetAsync("");
var content = await response.Content.ReadAsStringAsync();
Everything Combined
Here's what constructing HttpClient
and making an equivalent request should look like if we explicitly set every value referenced above:
var client = new HttpClient(
new HttpClientHandler()
{
// -L
AllowAutoRedirect = true,
// --negotiate -u :
UseDefaultCredentials = true,
// -b ~/cookiejar.txt
CookieContainer = new CookieContainer(),
UseCookies = true
}
);
var response = await client.GetAsync("https://idp.domain.net/oauth2/authorize?scope=openid&response_type=code&redirect_uri=https://localhost:5001&client_id=client_id_here");
var content = await response.Content.ReadAsStringAsync();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…