How to request via c#? Webrequest vs Httpclient

Asked

Viewed 915 times

1

I have a project in . Net Framework 4.6.1, and in my research I concluded that I can use the class Webrequest to request other web services via backend.

  1. I wish I could make requests using any http verb.
  2. Also be able to make use of SSL/TLS protocol when necessary.

  1. The Webrequest class is the only one available for this purpose?
  2. In addition to the above requirements, considering performance and security, this is the most appropriate way to establish connections through the . net platform?

Editing:

Consider the following code snippets:

Case 1:

public class MyServiceController : ApiController
{
    public void Post()
    {
        // ...
        using (var httpClient = new HttpClient())
        {
            var result = httpClient.GetAsync("https://answall.com");
        }
        // ...
    }
}

Case 2:

public class MyServiceController : ApiController
{
    public void Post()
    {
        // ...

        var request = WebRequest.Create("https://answall.com");
        request.Method = "GET";
        // ...

        using (var response = request.GetResponse())
        {
            // ...
        }
    }
}

1. If both of you can assist me with a request in C#, what would be the difference between them? What are the most indicated scenarios?

2. Both have support for any HTTP verb?

3. Both have SSL/TLS protocol support?

4. Which is more performative?

5. Some of them are obsolete and can offer me security risks in the requisition?

  • Take a look at the class HttpClient and see if it fits

  • @Kevinkouketsu thank you so much for the comment. If you can post a reply with a brief comparison between Webrequest and Httpclient I believe you can accept it now.

1 answer

0

Httpclient is the most current class to be used (as of 4.5), including in ASP.NET Core (which I use) it is possible to apply resilience behaviors in micro-services using Polly

  1. This class allows the use of calls to multiple servers and hosts at the same time and the amount you want.
  2. You can derive it to your own specialized classes.
  3. It was written using TAP (Task-based Asynchronous Pattern) can handle asynchronous requests, use of Await, manage pending requests, etc...
  4. Abstracted a lot of things, being much easier to implement and with fewer lines of code.
  5. Supports ssl

Macoratti made a comparison you can find by clicking here

I hope I’ve helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.