我一直在使用我的WinForm應用程序的ASP API。這是工作正常,但後來我開始使用命名空間的播放,並開始寫HttpClient Accept頭不起作用
沒有MediaTypeFormatter可從與媒體類型「text/html的」內容閱讀型「產品」 的對象。
我已經創建了新的解決方案,複製了所有的代碼,但它仍然沒有工作。響應格式是text/html,但具有相同標頭(Accept application/json)的curl命令正常工作。
這是我閱讀的內容之前,從API歌廳產品
public static async Task<ProductWithBool> GetProductByIdAsync(string id)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://eshop.ApiUpdatercentrum.tumam.cz/api/byznys/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Product product = null;
HttpResponseMessage response = await client.GetAsync("GetProduct?code=" + id);
if (response.IsSuccessStatusCode)
{
product = await response.Content.ReadAsAsync<Product>();
}
else
{
ErrorManager.AddError("Getting product failed");
return new ProductWithBool(null, 2);
}
if (product == null)
{
ErrorManager.AddError("Product not found");
return new ProductWithBool(null, 1);
}
return new ProductWithBool(product, 0);
}
您的應用程序*接受* json並不意味着服務器將*返回* json。問題出在API中,而不是你發佈的代碼 –