Answered by:
Post Different data types through HttpClient

Question
-
I am developing a Store App in which i need to make a call to REST API through HTTPClient. But issue is all five contents are of string type and one is byte array and other is Image. I have tried many links but could not find a solution to this problem as I can't have Key Value pairs as post method only accepts of string string types. Can anybody provide a demo or a link to this problem?Monday, January 5, 2015 1:13 PM
Answers
-
sending a dictionary is bit unclear question. You want to send it as sending a http form? Then you can use this snippet:
public async Task PostFormEncode() { var client = new Windows.Web.Http.HttpClient(); var dictionary = new Dictionary<string, string>(); var formEncodeContent = new Windows.Web.Http.HttpFormUrlEncodedContent(dictionary); var message = new HttpRequestMessage(HttpMethod.Post, new Uri("http://localhost")); message.Content = formEncodeContent; var result = await client.SendRequestAsync(message); }
if you want to send just the dictionary as json for example you need to convert the Dictionary to json yourself. You can do this:
- install json.net nuget package
public async Task PostJson() { var client = new Windows.Web.Http.HttpClient(); var dictionary = new Dictionary<string, string>(); string json = JsonConvert.Serialize(dictionary); var message = new HttpRequestMessage(HttpMethod.Post, new Uri("http://localhost")); message.Content = new HttpStringContent(message); var result = await client.SendRequestAsync(message); }
Microsoft Certified Solutions Developer - Windows Store Apps Using C#
- Proposed as answer by Jamles HezModerator Thursday, January 8, 2015 6:48 AM
- Marked as answer by RohitrkKUmar Thursday, January 15, 2015 4:17 AM
Monday, January 5, 2015 1:55 PM
All replies
-
Hi RohitrkKUmar:
Can you please describe the way through which you are doing. I mean can you paste the piece of code here. or may been screen shot. and tell me where exactly are you facing problem. Displaying String value, Image or array value is never a problem.
Monday, January 5, 2015 1:23 PM -
sending a dictionary is bit unclear question. You want to send it as sending a http form? Then you can use this snippet:
public async Task PostFormEncode() { var client = new Windows.Web.Http.HttpClient(); var dictionary = new Dictionary<string, string>(); var formEncodeContent = new Windows.Web.Http.HttpFormUrlEncodedContent(dictionary); var message = new HttpRequestMessage(HttpMethod.Post, new Uri("http://localhost")); message.Content = formEncodeContent; var result = await client.SendRequestAsync(message); }
if you want to send just the dictionary as json for example you need to convert the Dictionary to json yourself. You can do this:
- install json.net nuget package
public async Task PostJson() { var client = new Windows.Web.Http.HttpClient(); var dictionary = new Dictionary<string, string>(); string json = JsonConvert.Serialize(dictionary); var message = new HttpRequestMessage(HttpMethod.Post, new Uri("http://localhost")); message.Content = new HttpStringContent(message); var result = await client.SendRequestAsync(message); }
Microsoft Certified Solutions Developer - Windows Store Apps Using C#
- Proposed as answer by Jamles HezModerator Thursday, January 8, 2015 6:48 AM
- Marked as answer by RohitrkKUmar Thursday, January 15, 2015 4:17 AM
Monday, January 5, 2015 1:55 PM