Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

java - Difference between timeout() in WebClient request and the timeout in HttpClient

I was trying to invoke a service, and wanted to set a timeout for the same. I found two ways by which I can do so.

  1. By setting the Read and Write timeout in HttpClient globally and then using it ClientHttpConnector.
HttpClient httpClient = HttpClient.create()
                .tcpConfiguration(client ->
                        client.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)
                        .doOnConnected(c -> c
                                .addHandlerLast(new ReadTimeoutHandler(10))
                                .addHandlerLast(new WriteTimeoutHandler(10))));
         
        ClientHttpConnector conn = new ReactorClientHttpConnector(httpClient.wiretap(true));       
 
        return WebClient.builder()
                .baseUrl("http://localhost:8080")
                .clientConnector(conn)
                .build();
  1. By using the timeout() in the request.
return webClient.get()
        .uri("/someUri")
        .retrieve()
        .bodyToFlux(Foo.class)
        .timeout(Duration.ofMillis(10_000));

What're the major differences between both the implementations and also the performance impacts?

Also what is the difference between Connect, Read and Write Timeouts in the first implementation with HttpClient?

question from:https://stackoverflow.com/questions/65713476/difference-between-timeout-in-webclient-request-and-the-timeout-in-httpclient

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

HtttClient is the newer version with additional features as compared to WebClient. Its not a matter difference of timeout method rather its a rather a difference of whole Client system. To be precise WebClient makes you code less than HttpClient.

WebClient

The System.Net.WebClient class in .NET provides a high-level abstraction on top of HttpWebRequest. WebClient is just a wrapper around HttpWebRequest, so uses HttpWebRequest internally. Thus WebClient is a bit slow compared to HttpWebRequest, but requires you to write much less code. You can use WebClient for simple ways to connect to and work with HTTP services. It is generally a better choice than HttpWebRequest unless you need to leverage the additional features that HttpWebRequest provides.

httpClient

HttpClient was introduced in .NET Framework 4.5. For developers using .NET 4.5 or later, it is the preferred way to consume HTTP requests unless you have a specific reason not to use it. In essence, HttpClient combines the flexibility of HttpWebRequest and the simplicity of WebClient, giving you the best of both the worlds.

The HttpWebRequest class provides a lot of control over the request/response object. However, you should be aware that HttpClient was never designed to be a replacement for WebClient. You should use HttpWebRequest instead of HttpClient whenever you need the additional features that HttpWebRequest provides. Further, unlike WebClient, HttpClient lacks support for progress reporting and custom URI schemes.

Although HttpClient doesn’t support FTP, mocking and testing HttpClient is easier. All I/O bound methods in HttpClient are asynchronous, and you can use the same HttpClient instance to make concurrent requests as well.

Here is a link that might help you understand it much efficiently:https://www.infoworld.com/article/3198673/when-to-use-webclient-vs-httpclient-vs-httpwebrequest.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...