简体   繁体   中英

How do I set the user property “-u” for an HttpWebRequest request

I can't figure out how to set the -u / --user property of the HttpWebRequest request. My goal here is to set the --user to an api key I have, similar to the command

curl https://api.stripe.com/v1/customers?limit=3 -u api_key***************:

I have tried setting the UserAgent propety instead, but it doesn't work:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
request.UserAgent = "api_key***************:";

I am trying to get a JSON response from a web API that requires an API key in the -u part. Thanks to anyone who can help.

request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes($"api_key***************:")));

Apparently you use the -u or -user command in curl to use Basic authentication. It's strange to me that there is an api key in the username field but since there is a colon in your example that is probably what the API expects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM