简体   繁体   English

C# 后 JSON 数据到 REST ZDB974238714CA8DE634A7CE1D083A14Z

[英]C# post JSON data to REST API

I am new to REST API JSON but have been able to consume a few API with asp.net. I am new to REST API JSON but have been able to consume a few API with asp.net. Here is the problem I am currently facing.这是我目前面临的问题。

I have been able to send JSON data to an API using this method.我已经能够使用这种方法将 JSON 数据发送到 API。

public void PostData()
{
    string sName = sysName.Text;
    string sDescrip = sysDescrip.Text;
    var httpclient = new HttpClient();
    httpclient.BaseAddress = new Uri("http://33.248.292.99:8094/bizzdesk/sysconfig/api/");
    var sys = new Bizsys(){name= sName, description= sDescrip};
    httpclient.PostAsJsonAsync("system", sys);
}

it work just fine.它工作得很好。

Now I modify the code in order to accommodate more values thus:现在我修改代码以适应更多的值:

var httpclient = new HttpClient();
// ArrayList paramList = new ArrayList();
httpclient.BaseAddress = new Uri("http://179.683.197.115:9091/tua-slte/api/organisations/");
var org = new Organisation() { id=1, name = "inno", abbreviation = "abx", type="school", sort = 7, isShow = true, createdBy=8, createdDate = "10/04/2017", editedBy = 11, editDate="11/04/2017"};
var bas = new Basic() { id=1, username = "inno", password = "123", firstName="Innocent", lastName="Ujata", email = "ea@bizz.co", mobile = "123456", photo="10201001", loginIp="127.0.0.1", loginDate="10/04/2017", locked=false, organisation = org, createdDate = "10/04/2017", editedBy = 11, editDate="11/04/2017", admin=true};
var org2 = new Organisation2() { id=1, name = "inno", abbreviation = "abx", type="school", sort = 7, isShow = true, createdBy=17, createdDate = "10/04/2017", editedBy = 09, editDate="11/04/2017"};
var hq = new HeadQuarter() { zonId=09, organisation = org2, zonCode = "123", zonName = "Abuja", zonAddress = "123456", zonCity = "Abuja", zonPostalCode = "120076", zonEmail = "answers", zonPhoneNumber = "0908765", zonFaxNumber = "1212", zonState = "FCT", createdBy=17, createdDate = "10/04/2017", editedBy = 11, editDate="11/04/2017", version=1};
var examp = new RootObject() {basic=bas, headQuarter=hq  };

var status = httpclient.PostAsJsonAsync("register", examp);
return status;

It keep returning this:它不断返回:

Id = 19, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"

I hard coded the data to see it work first before making it dynamic.我对数据进行了硬编码,以便在使其成为动态之前先查看它的工作情况。

I have tried using await async method too the result is the same.我也尝试过使用 await async 方法,结果是一样的。 all the questions and answers I have seen here are not similar.我在这里看到的所有问题和答案都不相似。

What does that mean, and what am I getting wrong?这是什么意思,我做错了什么?

You are calling an async method, but not awaiting it's results.您正在调用异步方法,但不等待它的结果。

Change:改变:

var status = httpclient.PostAsJsonAsync("register", examp);

to

var status = await httpclient.PostAsJsonAsync("register", examp);

Extension method PostAsJsonAsync returns Task<HttpResponseMessage> .扩展方法PostAsJsonAsync返回Task<HttpResponseMessage> You grab this task and see it's details.你抓住这个任务,看看它的细节。 If you want to return status code of completed request, you should await for task completion and then grab status code from response message:如果要返回已完成请求的状态码,则应等待任务完成,然后从响应消息中获取状态码:

 private async Task<HttpStatusCode> PostSomethingAsync()
 {
     ...
     var response = await httpclient.PostAsJsonAsync("register", examp);
     return response.StatusCode;
 }

Or you can synchronously wait for request completion:或者您可以同步等待请求完成:

 private HttpStatusCode PostSomething()
 {
     ...
     var response = httpclient.PostAsJsonAsync("register", examp).Result;
     return response.StatusCode;
 }    

That's cause you are not awaiting the call like那是因为你没有在等待电话

var status = await httpclient.PostAsJsonAsync("register", examp);

Or, use ConfigureAwait()或者,使用ConfigureAwait()

var status = httpclient.PostAsJsonAsync("register", examp).ConfigureAwait(false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM