Answered by:
Get HttpClient with parameters

Question
-
User1630866477 posted
Hi all,
I have a WebApi that has a Get method with parameter username and password. Please how do I call this Get() method using HttpClient
Wednesday, December 30, 2015 2:24 PM
Answers
-
User-1902643333 posted
Hello:
1) Create your WebApi's class something like this following:
public class GetController : ApiController { private const string OUTPUT_FORMATION = "Your name is:{0},Password is:{1}"; public string Get(string name, string password) { return string.Format(OUTPUT_FORMATION, name, password); } }
2) Then create another console app, with the "HttpClient"'s asynchornized method like this following:
using System; using System.IO; using System.Net.Http; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { UriBuilder builder = new UriBuilder("http://localhost:6598/api/get"); builder.Query = "name='abc'&password='cde'"; //Create a query HttpClient client = new HttpClient(); var result = client.GetAsync(builder.Uri).Result; using (StreamReader sr = new StreamReader(result.Content.ReadAsStreamAsync().Result)) { Console.WriteLine(sr.ReadToEnd()); } } } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 31, 2015 6:13 AM -
User1724605321 posted
Hi KaptinKoda,
Please how do I call this Get() method using HttpClientCode below is for your reference:
using (var client = new HttpClient()) { client.BaseAddress = new Uri("http://localhost:9000/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // New code: HttpResponseMessage response = await client.GetAsync("api/products/1"); if (response.IsSuccessStatusCode) { Product product = await response.Content.ReadAsAsync>Product>(); Console.WriteLine("{0}\t${1}\t{2}", product.Name, product.Price, product.Category); } }
Please refer to below articles for details and demo :
http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client
Best Regards,
Nan Yu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 31, 2015 6:35 AM
All replies
-
User-1902643333 posted
Hello:
1) Create your WebApi's class something like this following:
public class GetController : ApiController { private const string OUTPUT_FORMATION = "Your name is:{0},Password is:{1}"; public string Get(string name, string password) { return string.Format(OUTPUT_FORMATION, name, password); } }
2) Then create another console app, with the "HttpClient"'s asynchornized method like this following:
using System; using System.IO; using System.Net.Http; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { UriBuilder builder = new UriBuilder("http://localhost:6598/api/get"); builder.Query = "name='abc'&password='cde'"; //Create a query HttpClient client = new HttpClient(); var result = client.GetAsync(builder.Uri).Result; using (StreamReader sr = new StreamReader(result.Content.ReadAsStreamAsync().Result)) { Console.WriteLine(sr.ReadToEnd()); } } } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 31, 2015 6:13 AM -
User1724605321 posted
Hi KaptinKoda,
Please how do I call this Get() method using HttpClientCode below is for your reference:
using (var client = new HttpClient()) { client.BaseAddress = new Uri("http://localhost:9000/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // New code: HttpResponseMessage response = await client.GetAsync("api/products/1"); if (response.IsSuccessStatusCode) { Product product = await response.Content.ReadAsAsync>Product>(); Console.WriteLine("{0}\t${1}\t{2}", product.Name, product.Price, product.Category); } }
Please refer to below articles for details and demo :
http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client
Best Regards,
Nan Yu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 31, 2015 6:35 AM -
User1630866477 posted
Thank you both for your help!!! Enough respect.
Thursday, December 31, 2015 11:56 AM