简体   繁体   中英

HTTPClient POST method c#

I guess this has been asked a million times, but I cannot figure out how stuff works. My app is in c# against framework 4.0. I try to do a simple POST, but my POST doens't even get triggered, not from C#. It does get triggered by a simple PowerShell equivalent that looks like this :

$uri="http://localhost:50554/api/pinfo/?sendmessage=yes&message=yep&killprocess=yes&timeout=20"
Invoke-RestMethod -uri $uri -Method Post -Body @($apso | ConvertTo-Json) -ContentType "application/json; charset=utf-8"

Where $apso is an array of custom PSObjects. Hence, the POST method works and is available. So in C# I do:

// this needs to be POSTed
PInfo pi = new PInfo();
pi.computername="test";
pi.username="test";
pi.PID="1234";
List<PInfo> lpi=new List<PInfo>();
lpi.Add(pi);

//Invoke the POST
HttpClient client = new HttpClient();

client.BaseAddress = new Uri("http://localhost:50554/");
var a = client.PostAsync("api/pinfo/?sendmessage=yes&message=tralala&killprocess=no&timeout=20", new StringContent(lpi.ToString(), System.Text.Encoding.UTF8, "application/json"))
             .ContinueWith((postTask) => postTask.Result.EnsureSuccessStatusCode());

But that doesn't trigger the same POST? Any ideas what I am missing here?

BR, Ronald

Edit:: await gives me a compile error: The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.

If anyone can explain what that means, great! Really no idea.

But this also seems to solve it:

PInfo p = new PInfo();
p.username = "test";
p.computername = "test";
p.PID = "test";
List<PInfo> testlist = new List<PInfo>();
testlist.Add(p);

client.BaseAddress = new Uri ("http://localhost:50554");

client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

HttpResponseMessage result = client.PostAsJsonAsync(url, testlist).Result;

string resultContent = result.Content.ReadAsStringAsync().Result;

The await keyword can only be used in a method that is decorated with the async keyword eg:

private async void button_click(object sender, EventArgs e) {
    var foo = await SomeMethodAsync(...);
}

In this regard, use of await is a bit viral - it requires all calling methods up to the root to be marked async. As to why async exists see this SO question about the subject .

You've discovered the other way to use this API. These new style Async methods return a Task if there is a result or just a Task if there is no result. In this case, you could also do this:

Task<HttpResponseMessage> task = client.PostAsync("api/pinfo/?sendmessage=yes&message=tralala&killprocess=no&timeout=20", new StringContent(lpi.ToString(), System.Text.Encoding.UTF8, "application/json"));

HttpResponseMessage response = task.Result;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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