我有这样的休息api url https://example.com/ProductAPI/api/V1/getproduct。 这个api有api_key,用户名和密码。如何使用c#访问此api和getproduct详细信息。我试过这样
WebRequest request = WebRequest.Create(@"https://example.com/ProductAPI/api/V1/getproduct");
request.Method = "GET";
request.Headers.Add("api_key","5g13441f-6915-4a34-8a53-bcb4er88b554");
request.Headers["Authorization"] = "Basic" + Convert.ToBase64String(Encoding.Default.GetBytes("admin:123"));
request.ContentLength = 0;
request.ContentType = @"application/json; charset=utf-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string jsonResponse = string.Empty;
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
jsonResponse = sr.ReadToEnd();
Console.WriteLine(jsonResponse);
Console.ReadLine();
}
但它显示401未经授权的错误。请帮助解决此问题