Call and consume Web API in winform using C#.net

I am beginner and creating winform application. In which i have to use API for Simple CRUD operation. My client had shared API with me and asked to send data in form of JSON.

API : http://blabla.com/blabla/api/login-valida

KEY : "HelloWorld"

Value : { "email": "user@gmail.com","password": "123456","time": "2015-09-22 10:15:20"}

Response : Login_id

How can i convert data to JSON, call API using POST method and get response?

EDIT Somewhere on stackoverflow i found this solution

public static void POST(string url, string jsonContent)
    {
        url="blabla.com/api/blala" + url;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(baseURL);
        request.Method = "POST";

        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
        Byte[] byteArray = encoding.GetBytes(jsonContent);

        request.ContentLength = byteArray.Length;
        request.ContentType = @"application/json";

        using (Stream dataStream = request.GetRequestStream())
        {
            dataStream.Write(byteArray, 0, byteArray.Length);
        }
        long length = 0;
        try
        {
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                length = response.ContentLength;

            }
        }
        catch
        {
            throw;
        }
    }
//on my login button click 
    private void btnLogin_Click(object sender, EventArgs e)
    {
        CallAPI.POST("login-validate", "{ \"email\":" + txtUserName.Text + " ,\"password\":" + txtPassword.Text + ",\"time\": " + DateTime.Now.ToString("yyyy-MM-dd h:mm tt") + "}");
    }

I got exception that says "The remote server returned an error: (404) Not Found."

Call (Consume) Web API in Windows Forms Application using C#, Net MVC. The Web API 2 Controller method will be called using WebClient class in Windows Forms Application to populate DataGridView  Creating a new Windows Forms Application, then drag Label, TextBox and Button controls from the Visual Studio Toolboxto your form designer. You can create a simple UI that allows you to select a file, then upload it to web server via web api in c#.

Just use the following library.

https://www.nuget.org/packages/RestSharp

GitHub Project: https://github.com/restsharp/RestSharp

Sample Code::

    public Customer GetCustomerDetailsByCustomerId(int id)
    {
        var client = new RestClient("http://localhost:3000/Api/GetCustomerDetailsByCustomerId/" + id);
        var request = new RestRequest(Method.GET);
        request.AddHeader("X-Token-Key", "dsds-sdsdsds-swrwerfd-dfdfd");
        IRestResponse response = client.Execute(request);
        var content = response.Content; // raw content as string
        dynamic json = JsonConvert.DeserializeObject(content);
        JObject customerObjJson = jsonData.CustomerObj;
        var customerObj = customerObjJson.ToObject<Customer>();
        return customerObj;
    }

Consume Web API In Winform For File Handling, In this article, we will see in detail how to consume Web API in AspNet.WebApi.​Client installed for Winform application using NuGet Packages. NET Web Application. In upload file button click, we call the method as upload and pass the Web API URL to upload the file to the web server file path. The Web API 2 Controller method will fetch records from database using Entity Framework in ASP.Net MVC. The Web API 2 Controller method will be called using WebClient class in Windows Forms Application to populate DataGridView control. Download. Download Free Files API. Download Free Files API.

Consume Web API in .NET using HttpClient, .NET 4.5 includes HttpClient class to overcome the limitation of WebClient. Let's see how to consume Web API using HttpClient in the console application. Manager -> Package Manager Console and execute following command. Subscribe to TutorialsTeacher email list and get latest updates, tips & tricks on C#​, .Net  You can use JToken to parse content response from web api. To call the Web API using JWT ( Json Web Token ) authorization you need to add Authorization to header as shown below. client.AddDefaultHeader("Authorization", string.Format("Bearer {0}", _token));

Call a Web API From a .NET Client (C#) - ASP.NET , This tutorial shows how to call a web API from a .NET 4.x application. Call a Web API From a .NET Client (C#) 11/24/2017; 7 minutes to read +11; In this article. by Mike Wasson and Rick Anderson. Download Completed Project. Download instructions. This tutorial shows how to call a web API from a .NET application, using System.Net.Http.HttpClient. In this tutorial, a client app is written that consumes the following

How to Call and Consume Web API in Winform using C#, This tutorial will show you how to call and read response from web api using c# code windows forms application. In the API Controllers project I Created an API in ASP.NET Core, now I have to consume the API.For this I have to create a new project. So create a new empty project by using the ASP.NET Core Web Application (.NET Core) template, and name the project as ‘APIConsume’.

Calling a Web API From C# and Calling a Web API , NET application, using HttpClient. In this tutorial, we will write an app that consumes the following web API. Action, HTTP method  By taking a path of Web development, you find yourself in the need of dealing with external APIs (Application Programming Interface) sooner or later. In this article, my goal is to make the most comprehensive list of ways to consume RESTful APIs in your C# projects and show you how to do that on some simple examples.

Comments
  • Beautiful answer.