Igor Stephano
Igor Stephano

Reputation: 71

RestSharp with HTTP 2.0

I'm trying to make requests (GET/POST) using RestSharp with HTTP 2.0

With the following code generates HTTP 1.1 requests. The server is configured to support HTTP 2.0.

        var restClient = new RestClient(URL);
        IRestRequest restRequest = new RestRequest(CONTEXT, Method.GET);
        restRequest.AddHeader("Accept", "application/json");
        restRequest.AddHeader("Content-Type", "application/json");
        IRestResponse restResponse = restClient.Execute(restRequest);

Any thoughts on how to set explicitly the HTTP version to 2.0?

Upvotes: 4

Views: 2740

Answers (1)

Michele
Michele

Reputation: 1274

I know that the question is old, but someone could be found my solution useful.

Actually (apr-2019) RestSharp doesn't support HTTP/2.0 request.

The only way I've found is use the standard System.Net.Http.HttpRequestMessage to send the REST request, in this way:

var http2Handler = new Http2Handler();
using (var httpClient = new HttpClient(http2Handler))
{
    var requestHttp = new HttpRequestMessage {
        RequestUri = new Uri(yourUrl),
        // ...
    };

    var responseHttp = httpClient.SendAsync(requestHttp).Result;
    // ... response elaboration
}

Where Http2Handler simply is:

public class Http2Handler : System.Net.Http.WinHttpHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
    {
        request.Version = new Version("2.0");
        return base.SendAsync(request, cancellationToken);
    }
}

Upvotes: 1

Related Questions