下面提到的代码无法正常工作。在
请问有人能解释一下原因吗? DEV环境: VS2012,.NET 4.5,C#,ASP.NET WebForms 代码:
GetAsync
中我收到以下异常:
然而,如果我注释掉无效的URI:无法确定URI的格式。
apiClient.BaseAddress
并在apiClient.GetAsync
中指定完整的URI,则可以正常工作!请问有人能解释一下原因吗? DEV环境: 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()
var uri = new Uri("api/products/" + "productId");
(我也明白可能很痛苦地清楚问题所在 - 但“最小化”是重要的部分)。 - Alexei Levenkov