How to pass dataset to web api method

I've written a function to consume the web API from my windows application, Once I click the 'Submit' button from the "WinForm", I'll call the below function to get the response from my web API

public static string GetResponse(string apiUrl, System.Net.WebProxy proxy)
{
    try
    {
        System.Net.WebRequest webReq = System.Net.WebRequest.Create(apiUrl);
        webReq.Proxy = proxy;
        webReq.Method = "GET";
        webReq.ContentType = "application/json; charset=utf-8";
        System.Net.WebResponse webResp = webReq.GetResponse();
        string statusDesc = ((System.Net.HttpWebResponse)webResp).StatusDescription;
        if (statusDesc == "OK")
        {
            System.IO.Stream streamer = webResp.GetResponseStream();
            System.IO.StreamReader sReader = new System.IO.StreamReader(streamer);
            String sContent = sReader.ReadToEnd();
            System.Web.Script.Serialization.JavaScriptSerializer jsonContent = new System.Web.Script.Serialization.JavaScriptSerializer();
            object obj = jsonContent.Deserialize(sContent, typeof(object));
            return obj.ToString().Trim();
        }
        else
        {
            return string.Empty;
        }
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

The 'Submit' button event code present inside the windows application (WinForms)

System.Data.DataSet dsDetails = new System.Data.DataSet("Global");
dsDetails.ReadXml("D:\\Projects\\Log\\Logs.xml", System.Data.XmlReadMode.Auto);
string proxyUrl = "http://localhost:8285/";
WebProxy proxy = new WebProxy(proxyUrl, true);
string methodRoute = "Statement";
string apiUrl = "http://localhost:8285/" + methodRoute + "/" + dsDetails;
string outString = this.GetResponse(apiUrl, proxy);

The 'GetResponse' redirects to the API controller, and it calls the corresponding API method name "GetAccountStatement" as given below...

using System.Web.Http;
using System.Web.Http.Cors;
namespace MicroService.Example.Controllers
{
    [EnableCors("*", "*", "*")]
    public class AccountStatementController : ApiController
    {
        [Route("Statement")]
        [HttpPost()]
        public string GetAccountStatement([FromBody()] DataSet dsDetails)
        {
          ...
        }
    }
}

Can anyone please ensure whether the above API controller mechanism is right ?

How can I pass the list of objects like DataSet, jsonContent etc., to the web API controller from the winforms ?

Please help...

1 answer

  • answered 2018-07-12 04:15 Yoo Matsuo

    I think you should make a DTO to receive posted data on a controller instead of using DataSet because it's much easier.

    Anyway, how to post data with HttpWebRequest, look at https://stackoverflow.com/a/3279984/366049

    Also you can post data with WebClient like the following,

    using (WebClient client = new WebClient())
    {
        var reqparm = new System.Collections.Specialized.NameValueCollection();
        reqparm.Add("Prop1", "Hello");
        reqparm.Add("Prop2", 2);
        var responsebytes = client.UploadValues("http://your api url here", "POST", reqparm);
        var result = Encoding.UTF8.GetString(responsebytes);
    
        // do whatever you want with the result
    }
    
    // dto
    public class PostedData{
        public string Prop1{get;set;}
        public int Prop2{get;set;}
    }
    
    // in your controller
    [Route("Statement")]
    [HttpPost()]
    public string GetAccountStatement([FromBody()] PostedData data)
    {
      ...
    }