-2

I can call every webApi methods by ajax request but I can't do it by c#. this is a simple web api which every body can create it in asp.net mvc web api but I can't find any example about call and get it programmatically by c#.

  public class TestController : ApiController
{
    // GET: api/Test
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
}

please some body say me how can I call this web api method in c# and get these 2 values. how can we get value from a web service(RestFull-without reference) in c# ?

No Refunds No Returns
  • 8,092
  • 4
  • 32
  • 43
Iman Salehi
  • 908
  • 2
  • 14
  • 34
  • there are several concrete .NET classes you could use to facilitiate making a HTTP request, or you could even build your own HTTP client if you feel like really getting into the guts of the process. The possibilities endless. What have you researched and/or tried? Lots of examples and documentation online which you can find easily by googling it. – ADyson Oct 24 '17 at 15:11
  • 1
    Possible duplicate of [How to make HTTP POST web request](https://stackoverflow.com/questions/4015324/how-to-make-http-post-web-request) – adiga Oct 24 '17 at 16:07
  • [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/a/261593/3082296) – adiga Oct 24 '17 at 16:09

3 Answers3

2

To call WEB API in c# you can use the HttpClient class.

public class Client
{
   private readonly string baseUri;
   private static HttpClient _client;

    public Client(string baseUri)
    {
        this.baseUri = baseUri;

        _client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true })
        {
            BaseAddress = new Uri(baseUri)
        };

        _client.DefaultRequestHeaders.Accept.Clear();
        _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    }

    public async Task<SomeResource> GetResourceById(int Id)
    {
        var path = $"{baseUri}/Resources/{Id}";

        var response = await _client.GetAsync(path);

        return await response.Content.ReadAsAsync<SomeResource>();           
    }
Sudipto
  • 41
  • 7
2

the answer is this...all persons who give negative reaction can do this instead of negative reactions:

        string GET(string url)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        try
        {
            WebResponse response = request.GetResponse();
            using (Stream responseStream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                return reader.ReadToEnd();
            }
        }
        catch (WebException ex)
        {
            WebResponse errorResponse = ex.Response;
            using (Stream responseStream = errorResponse.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
                String errorText = reader.ReadToEnd();
                // log errorText
            }
            throw;
        }
    }

so easier !

Iman Salehi
  • 908
  • 2
  • 14
  • 34
0

Create an HttpClient (https://msdn.microsoft.com/en-us/library/system.net.http.httpclient(v=vs.118).aspx) and call its GetAsync() https://msdn.microsoft.com/en-us/library/hh158912(v=vs.118).aspx method. I'm assuming you have this running locally so your Uri is probably something like http://localhost/test/get depending on how you have Visual Studio configured when you press F5. Here's a complete example: https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client

No Refunds No Returns
  • 8,092
  • 4
  • 32
  • 43