Click here to Skip to main content
16,020,669 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
string json = JsonConvert.SerializeObject(dict)

here in json i am getting jason string,

now i want to pass this json string to http postAsync(url, myjsonString)

how to do this in using C#??
without using async and await. bez my project targeted to .net framework 4.

thanks
sushil

What I have tried:

string json = JsonConvert.SerializeObject(dicti, Formatting.Indented);

var httpContent = new StringContent(json);

var httpResponce = Helper.Client.PostAsync(path, httpContent);
Posted
Updated 22-Jul-16 0:22am
v2

The problem that you might be facing will be in the result, not the passing of JSON content. If you do not want to use async/await operators, there is no need to do that either. You can write the following code to perform the HTTP POST synchronously (but waiting for the thread to complete).
C#
string json = JsonConvert.SerializeObject(dicti, Formatting.Indented);
var httpContent = new StringContent(json);

// PostAsync returns a Task<httpresponsemessage>
var httpResponce = Helper.Client.PostAsync(path, httpContent).Result;
</httpresponsemessage>

Note that if you don't consider using the async/await, you will have to work with the Task[^] based programming model. In that case, you will have to wait until the result is captured and then continue programming the rest of the stuff. For defensive programming, consider checking the status of the task to see if it completed with success or not. Read the remarks section for more information.
 
Share this answer
 
Comments
Member 11859517 22-Jul-16 6:27am    
thanks afzaal :)
string json = JsonConvert.SerializeObject(dicti, Formatting.Indented);

var buffer = System.Text.Encoding.UTF8.GetBytes(json);

var byteContent = new ByteArrayContent(buffer);

byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

var httpResponce = Helper.Client.PostAsync(path, byteContent).Result;

its working fine....:)
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 22-Jul-16 6:34am    
You should select my post as Answer instead of adding a new post as Solution with the same content and suggestion.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900