locked
Unable to call WEBAPI Service by using HttpClient. RRS feed

  • Question

  • User-1668195447 posted

    Hi Am creating sample WebApi service. When i am trying to consume that service by using HttpClient ,am getting "Not Found" Error.

    Its working on GetAsync method. but not working on PostAsync,PutAsync,DeletAsync.

    My service code is

     Sample objsample = new Sample();
          
            // GET api/values
    
            public HttpResponseMessage Get()
            {
                var data=objsample.GetString();
                HttpResponseMessage obj = Request.CreateResponse(HttpStatusCode.OK, data);
                return obj;
               
            }
    
            // POST api/values
            [HttpPost]
            public HttpResponseMessage Post(string value)
            {
                var data = objsample.PostString(value);
                HttpResponseMessage obj = Request.CreateResponse(HttpStatusCode.OK, data);
                return obj;
            }
    
            // PUT api/values/5
            [HttpPut]
            public HttpResponseMessage Put(string value)
            {
                var data = objsample.PutString(value);
                HttpResponseMessage obj = Request.CreateResponse(HttpStatusCode.OK, data);
                return obj;
            }
    
            // DELETE api/values/5
            [HttpDelete]
            public HttpResponseMessage Delete(string id)
            {
                var data = objsample.PutString(id);
                HttpResponseMessage obj = Request.CreateResponse(HttpStatusCode.OK, data);
                return obj;
            }

    My Client Code

     HttpClient client = new HttpClient();
                client.BaseAddress = new Uri("http://localhost:45355/api/");
    
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));
               // HttpResponseMessage response = client.GetAsync("Values").Result;
    
                StringContent content = new System.Net.Http.StringContent("abc", Encoding.UTF8, "application/json");
    // "abc"--String Value(input) //HttpResponseMessage response = client.PostAsync("values", content).Result; HttpResponseMessage response = client.DeleteAsync("values").Result; if (response.IsSuccessStatusCode) { var s = response.Content.ReadAsStringAsync(); string _resukt = s.Result; }

    Could you please help me on that.How to call /pass string value to service by using PutAsync,PostAsync,DeleteAsync.

    Thanks,

    Sundar.

    Monday, May 18, 2015 4:43 AM

Answers

  • User-782957977 posted

    Please change like this

    //Delete Call
    
    using (var client = new HttpClient())
                 {
                     client.BaseAddress = new Uri("http://localhost:45355/");
                     var response = client.DeleteAsync("api/values/1").Result;
                     if (response.IsSuccessStatusCode)
                     {
                         string responseString = response.Content.ReadAsStringAsync().Result;
                     }
                 }

    Put Call

    using (var client = new HttpClient())
                 {
                     client.BaseAddress = new Uri("http://localhost:45355/");
                     var response = client.PutAsJsonAsync("api/values", "Test").Result;
                     if (response.IsSuccessStatusCode)
                     {
                         string responseString = response.Content.ReadAsStringAsync().Result;
                     }
                 }

    Post Call

     using (var client = new HttpClient())
                 {
                     client.BaseAddress = new Uri("http://localhost:45355/");
                     var response = client.PostAsJsonAsync("api/values", "Test").Result;
                     if (response.IsSuccessStatusCode)
                     {
                         string responseString = response.Content.ReadAsStringAsync().Result;
                     }
                 }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 18, 2015 10:13 PM
  • User1644755831 posted

    Hello Sundar1987,

    Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.

    You probably need to add reference to newtonsoft.json assembly.  you can install it using nuget. https://www.nuget.org/packages/newtonsoft.json/

    Install-Package Newtonsoft.Json

    Hope this helps.

    With Regards,

    Krunal Parekh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 27, 2015 9:49 PM

All replies

  • User-782957977 posted

    Please change like this

    //Delete Call
    
    using (var client = new HttpClient())
                 {
                     client.BaseAddress = new Uri("http://localhost:45355/");
                     var response = client.DeleteAsync("api/values/1").Result;
                     if (response.IsSuccessStatusCode)
                     {
                         string responseString = response.Content.ReadAsStringAsync().Result;
                     }
                 }

    Put Call

    using (var client = new HttpClient())
                 {
                     client.BaseAddress = new Uri("http://localhost:45355/");
                     var response = client.PutAsJsonAsync("api/values", "Test").Result;
                     if (response.IsSuccessStatusCode)
                     {
                         string responseString = response.Content.ReadAsStringAsync().Result;
                     }
                 }

    Post Call

     using (var client = new HttpClient())
                 {
                     client.BaseAddress = new Uri("http://localhost:45355/");
                     var response = client.PostAsJsonAsync("api/values", "Test").Result;
                     if (response.IsSuccessStatusCode)
                     {
                         string responseString = response.Content.ReadAsStringAsync().Result;
                     }
                 }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 18, 2015 10:13 PM
  • User-1668195447 posted

    Hi Santhoshje,

    Thanks for your reply.

    i tried with above code what you have given.

    Delete part is working fine.

    when i use put and post code am getting the below error

    'System.Net.Http.HttpClient' does not contain a definition for 'PutAsJsonAsync' and no extension method 'PutAsJsonAsync' accepting a first argument of type 'System.Net.Http.HttpClient' could be found (are you missing a using directive or an assembly reference?)

    Then have added "System.Net.Http.Formatting" namespace,then above error got resolved.

    but am getting the below error,

    Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.

    Could you please help me how to pass the values to services?

    Thanks,

    Sundar.

    Tuesday, May 26, 2015 11:51 PM
  • User1644755831 posted

    Hello Sundar1987,

    Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.

    You probably need to add reference to newtonsoft.json assembly.  you can install it using nuget. https://www.nuget.org/packages/newtonsoft.json/

    Install-Package Newtonsoft.Json

    Hope this helps.

    With Regards,

    Krunal Parekh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 27, 2015 9:49 PM
  • User-782957977 posted

    Please add reference to System.Net.Http.Formatting; extension.

    Thursday, May 28, 2015 9:38 AM