c# - Set request Content-Type on WinRT HttpClient -
I am developing a Windows 8.1 store app with C # and .NET Framework 4.5.1.
I am trying to make a POST on a REST API but I get an unsupported media type with this code:
public async works & lt; HttpResponseMessage & gt; Post (string url, string jsn content) {Uri Resource Yuri; Resources URI = Valid URI (URL); HTTP Client HTTP Client = New HTTP Client (); HTTPRPS Pensions Message Response = New HTTPRPS Pacemases (); HttpRequestHeaderCollection Header = httpClient.DefaultRequestHeaders; Try adding the User Agent to the Header Header (Header. User Agent, Trainee Perez Ad (_User Agent)). User Agent. Perseid (_user agent); // Add content-type and content-length header headers. accept. Add (New HttpMediaTypeWithQualityHeaderValue ("application / json")); Feedback = Wait httpClient.PostAsync (resource URI, new HTTP string content (JSN content)); Return feedback; }
If I change this line:
feedback = wait httpclient.PostAsync (resource URI, new HTTP string content (JSON communication));
with one:
response = httpClient.PostAsync (resource Yuri, new HTTP string content (string.optim));
This works does not get me 415 status codes.
jsoncontent
is the value:
{"UserName": "My name", "Provider": "Facebook", "ExternalAccessToken": "Access Token Excluded"}
Because I have not found any similar code on the Internet , And I only have 4 thoughts on this question; I will share this answer.
I have decided to change this problem with this code:
feedback = wait httpclient.PostAsync (resource URI, new HTTP string content (JSN content, Unicode encoding .Utf8, "App / Jason");
You can pass the "content-type" on the HttpStringContent
constructor. more information.
Comments
Post a Comment