下面提到的代码不起作用。我得到以下异常GetAsync
:
无效的 URI:无法确定 URI 的格式。
但是,如果我评论apiClient.BaseAddress
并指定完整的 URI,apiClient.GetAsync
那么它工作正常!
有人可以解释原因吗?
开发环境: VS2012、.NET 4.5、C#、ASP.NET WebForms
代码
private async Task WebAPICall()
{
using (var apiClient = new HttpClient())
{
apiClient.BaseAddress = new Uri("http://localhost:9000/");
apiClient.DefaultRequestHeaders.Accept.Clear();
apiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
apiClient.Timeout = new TimeSpan(0, 0, 30);
try
{
HttpResponseMessage apiResponseMsg = await apiClient.GetAsync(new Uri("api/products/" + txtProductID.Text.Trim()));
string strResponse = await apiResponseMsg.Content.ReadAsStringAsync();
Response.Write(strResponse);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
} //using (var apiClient = new HttpClient())
} //private async Task WebAPICall()