我尝试连接到在我的机器上本地运行的 API 以检索 Android 模拟器中的值。 我不断收到 404 错误。知道这里应该为 API 做什么吗?感谢您的帮助。
var test = "";
var url = "http://10.0.2.2:60002/api/values/" + Convert.ToInt32(Id);
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Basic", Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
$"{user}:{pass}")));
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
var response = await client.GetStringAsync(url);
test = response;
}
return test;