1
我正在開發一個Windows 8.1商店應用程序與C#和.NET Framework 4.5.1。在WinRT上設置請求內容類型HttpClient
我試圖做一個POST到一個REST API,但我得到這個代碼不支持的媒體類型:
public async Task<HttpResponseMessage> POST(string url, string jsonContent)
{
Uri resourceUri;
resourceUri = ValidateUri(url);
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = new HttpResponseMessage();
HttpRequestHeaderCollection headers = httpClient.DefaultRequestHeaders;
// Try to add user agent to headers.
if (headers.UserAgent.TryParseAdd(_userAgent))
headers.UserAgent.ParseAdd(_userAgent);
// Add Content-Type and Content-Length headers
headers.Accept.Add(new HttpMediaTypeWithQualityHeaderValue("application/json"));
response = await httpClient.PostAsync(resourceUri, new HttpStringContent(jsonContent));
return response;
}
如果我改變這一行:
response = await httpClient.PostAsync(resourceUri, new HttpStringContent(jsonContent));
有了這一個:
response = await httpClient.PostAsync(resourceUri, new HttpStringContent(string.Empty));
它的工作原理。我沒有得到415狀態碼。
jsonContent
值:
{"UserName":"My Name","Provider":"Facebook","ExternalAccessToken":"Access token omitted"}