4

http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client

I have been using this tutorial to learn how to send and retrieve data from a web API. However is there another way without using Tasks and await? Sometimes my requests are taking a long time to return when others are relatively quick.

Are there any other tutorials that may help? What is the best way to call my web api client to login? I feel I shouldn't be using await and async for this sort of thing but could really use some help.

Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
user3427148
  • 41
  • 1
  • 1
  • 2
  • You should be using await and async (or some threading model) for any long running method. This prevents the UI from freezing up until the call returns. With that said, this question is not specific enough to give you a good answer. – John Koerner Mar 17 '14 at 00:51
  • Well my requests are general just to load small amounts of data into a datagridview or similar so they are not long running methods. How would I write an API call to get some data in a similar way to the tutorial but without await and async? – user3427148 Mar 17 '14 at 00:55
  • @user3427148 More than 30ms is considered long. Most Http requests fall into that category. It's painful at first but you just need to get used to using Async methods. – Darrel Miller Mar 20 '14 at 13:49
  • I think this is answer you are looking for. https://stackoverflow.com/questions/32716174/call-and-consume-web-api-in-winform-using-c-net – Mohsin Jan 24 '18 at 07:42

2 Answers2

4

just download microsoft http api library from nuget manager to your project and use httpClient

0

You could just use HttpClient if you like and it is supported even in older versions of .NET, and you would not need to worry about tasks or threading. HttpClient also allows you to get the result async but you would need to provide a callback to be called when the result is ready.

Have a look at http://msdn.microsoft.com/en-us/library/system.net.http.httpclient(v=vs.110).aspx

Having said that, I agree with John comments above that you should use await and async

Has AlTaiar
  • 4,052
  • 2
  • 36
  • 37