locked
call Web Api POST RRS feed

  • Question

  • User-1471881183 posted

    hello all,

    i have a web api to post data, its like below, i want to call and post data from windows service. may i know how to achieve this?

    // POST: api/PosttblEmployee
            [ResponseType(typeof(tblEmployee))]
            [Route("api/PosttblEmployee")]
            public IHttpActionResult PosttblEmployee(tblEmployee tblEmployee)
            {
                try
                {
                    tblEmployee.addDate = Convert.ToDateTime(DateTime.Now.ToString());
                    tblEmployee.addUserId = 1;
                    var errors = ModelState
                                    .Where(x => x.Value.Errors.Count > 0)
                                    .Select(x => new { x.Key, x.Value.Errors })
                                    .ToArray();
                    if (!ModelState.IsValid)
                    {
                        //return BadRequest(ModelState);
                    }
    
                    db.tblEmployees.Add(tblEmployee);
                    db.SaveChanges();
                    return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Pass"));
                }
    	catch (Exception ex)
                {
                    return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Fail"));
                }
            }
    
    

    Wednesday, June 27, 2018 11:16 AM

Answers

  • User36583972 posted


    Hi winseealn,

    i have a web api to post data, its like below, i want to call and post data from windows service. may i know how to achieve this?

    The following methods for your reference.

    HttpClient:

       using (var client = new System.Net.Http.HttpClient())
                {
                    // HTTP POST
                    client.BaseAddress = new Uri("baseUrl");
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
      
                    var content = new StringContent(JsonConvert.SerializeObject(new tblEmployee()), Encoding.UTF8, "application/json");
                    var response = client.PostAsync("/api/PosttblEmployee", content).Result;
                    string s = await response.Content.ReadAsStringAsync();
                }

    HttpWebRequest:

         //using System.Net;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("your api address/api/PosttblEmployee");
                request.Method = "POST";
                request.KeepAlive = true;
                request.ContentType = "application/json; charset=utf-8";
               
                // write the data to the request stream         
                using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
                {
                    string PostData = new JavaScriptSerializer().Serialize(new tblEmployee());
                    writer.Write(PostData);
                }
    
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string myResponse = "";
                using (System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream()))
                {
                    myResponse = sr.ReadToEnd();
                }
    

    Best Regards,

    Yong Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 28, 2018 6:04 AM

All replies

  • User475983607 posted

    Use the HttpClient.  The following link explains the details.

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client

    Wednesday, June 27, 2018 11:21 AM
  • Wednesday, June 27, 2018 12:47 PM
  • User36583972 posted


    Hi winseealn,

    i have a web api to post data, its like below, i want to call and post data from windows service. may i know how to achieve this?

    The following methods for your reference.

    HttpClient:

       using (var client = new System.Net.Http.HttpClient())
                {
                    // HTTP POST
                    client.BaseAddress = new Uri("baseUrl");
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
      
                    var content = new StringContent(JsonConvert.SerializeObject(new tblEmployee()), Encoding.UTF8, "application/json");
                    var response = client.PostAsync("/api/PosttblEmployee", content).Result;
                    string s = await response.Content.ReadAsStringAsync();
                }

    HttpWebRequest:

         //using System.Net;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("your api address/api/PosttblEmployee");
                request.Method = "POST";
                request.KeepAlive = true;
                request.ContentType = "application/json; charset=utf-8";
               
                // write the data to the request stream         
                using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
                {
                    string PostData = new JavaScriptSerializer().Serialize(new tblEmployee());
                    writer.Write(PostData);
                }
    
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string myResponse = "";
                using (System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream()))
                {
                    myResponse = sr.ReadToEnd();
                }
    

    Best Regards,

    Yong Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 28, 2018 6:04 AM