Python 使用身份验证凭据的简单C API Get请求?
我是C编程的初学者。我有使用Python的经验,这正是我试图用C语言复制的内容 导入请求,json api_用户=userexample@example.com api_key=keyexample url=https://api.example.com/ response=requests.geturl,auth=api\u user,api\u key json_response=response.json printjson\u响应 那代码是什么样子的?谢谢您可以使用HttpClient库与api服务器连接。 下面是api基本身份验证的示例代码。 最后一定要调用Dispose,否则您可能会遇到一些GC问题Python 使用身份验证凭据的简单C API Get请求?,python,c#,asp.net,api,request,Python,C#,Asp.net,Api,Request,我是C编程的初学者。我有使用Python的经验,这正是我试图用C语言复制的内容 导入请求,json api_用户=userexample@example.com api_key=keyexample url=https://api.example.com/ response=requests.geturl,auth=api\u user,api\u key json_response=response.json printjson\u响应 那代码是什么样子的?谢谢您可以使用HttpClient库
public static class Program
{
public static async Task Main(string[] args)
{
var apiUser = "userexample@example.com";
var apiKey = "keyexample";
var url = "https://api.example.com/";
var client = new HttpClient();
client.BaseAddress = new Uri("https://api.example.com/");
var authToken = Encoding.ASCII.GetBytes($"{apiUser}:{apiKey}");
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Basic", Convert.ToBase64String(authToken));
var response = await client.GetAsync(url);
var content = response.Content;
client.Dispose();
}
}
欢迎来到StackOverflow!请详细说明您在C中已经尝试了什么,以及为什么它不起作用