HttpClient client = new HttpClient();
client.BaseAddress = uri;
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.PostAsJsonAsync(route, requestResource).Result;
无法使用Xamarin PCL并导致此错误:
ex = {System.TypeInitializationException: The type initializer for 'System.Net.Http.FormattingUtilities' threw an exception. ---> System.NotImplementedException: The method or operation is not implemented.
at System.Runtime.Serialization.XsdDataContractExporte...
答案 0 :(得分:1)
说实话,我没有使用过这种方法,但是没有实现它没有太多关于它的事情。
你可以这样发帖
var request = new HttpRequestMessage(HttpMethod.Post, route);
var hc = new StringContent(requestResource); // requestResource should be a string
hc.Headers.ContentType = new MediaTypeHeaderValue("application/json);
request.Content = hc;
var response = await httpClient.SendAsync(request);
答案 1 :(得分:0)
通常,在未实现请求的方法或操作时抛出异常。如果您正在调用的方法已经实现,请与API的开发人员联系。
答案 2 :(得分:0)
尝试更新Visual Studio。那对我有用。