如何将json字符串传递给C#中的client.postasync()方法? [英] How to pass the json string to client.postasync() method in C#?

查看:1231
本文介绍了如何将json字符串传递给C#中的client.postasync()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string json = JsonConvert.SerializeObject(dict)



这里是json我正在获得jason string,



现在我想将这个json字符串传递给http postAsync(url,myjsonString)



如何使用C#??

不使用async和await。 bez我的项目针对.net框架4.



谢谢

sushil



我尝试了什么:



string json = JsonConvert.SerializeObject(dicti,F​​ormatting.Indented);



var httpContent = new StringContent(json);



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

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);

推荐答案

您可能遇到的问题将出在结果中,而不是JSON内容的传递。如果您不想使用async / await运算符,则也不需要这样做。您可以编写以下代码来同步执行HTTP POST(但等待线程完成)。

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).
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>



请注意,如果您不考虑使用async / await,则必须使用 任务 [ ^ ]基于编程模型。在这种情况下,您必须等到捕获结果,然后继续编程其余的东西。对于防御性编程,请考虑检查任务的状态以查看其是否成功完成。阅读备注部分以获取更多信息。


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.


string json = JsonConvert.SerializeObject(dicti,F​​ormatting.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) )。结果;



工作正常....:)
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....:)


这篇关于如何将json字符串传递给C#中的client.postasync()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆