|
7 | 7 | import org.apache.commons.httpclient.HttpClient;
|
8 | 8 | import org.apache.commons.httpclient.HttpMethod;
|
9 | 9 | import org.apache.commons.httpclient.methods.GetMethod;
|
| 10 | +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; |
| 11 | +import org.apache.hc.client5.http.impl.classic.HttpClients; |
10 | 12 | import org.apache.http.HttpHost;
|
11 | 13 | import org.apache.http.client.methods.HttpGet;
|
12 | 14 | import org.apache.http.impl.client.DefaultHttpClient;
|
| 15 | +import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; |
| 16 | +import org.apache.http.impl.nio.client.HttpAsyncClients; |
13 | 17 | import org.apache.http.message.BasicHttpRequest;
|
| 18 | +import org.apache.http.nio.client.methods.HttpAsyncMethods; |
| 19 | +import org.apache.http.nio.protocol.HttpAsyncRequestProducer; |
14 | 20 | import org.springframework.web.bind.annotation.PostMapping;
|
15 | 21 | import org.springframework.web.bind.annotation.RequestMapping;
|
16 | 22 | import org.springframework.web.bind.annotation.RequestParam;
|
@@ -89,4 +95,60 @@ public String okHttp3(@RequestParam(value = "url") final String url) {
|
89 | 95 | client.connectionPool().evictAll();
|
90 | 96 | return "ok";
|
91 | 97 | }
|
| 98 | + |
| 99 | + @PostMapping("/apache-httpclient5") |
| 100 | + public String apacheHttpClient5( |
| 101 | + @RequestParam(value = "url", required = false) final String url, |
| 102 | + @RequestParam(value = "urlHandler", required = false) final String urlHandler, |
| 103 | + @RequestParam(value = "host", required = false) final String host) { |
| 104 | + CloseableHttpClient client = HttpClients.createDefault(); |
| 105 | + try { |
| 106 | + if (host != null) { |
| 107 | + final org.apache.hc.core5.http.HttpHost httpHost = |
| 108 | + new org.apache.hc.core5.http.HttpHost(host); |
| 109 | + final org.apache.hc.client5.http.classic.methods.HttpGet request = |
| 110 | + new org.apache.hc.client5.http.classic.methods.HttpGet("/"); |
| 111 | + client.execute(httpHost, request); |
| 112 | + } else if (url != null) { |
| 113 | + final org.apache.hc.client5.http.classic.methods.HttpGet request = |
| 114 | + new org.apache.hc.client5.http.classic.methods.HttpGet(url); |
| 115 | + client.execute(request); |
| 116 | + } else if (urlHandler != null) { |
| 117 | + final org.apache.hc.client5.http.classic.methods.HttpGet request = |
| 118 | + new org.apache.hc.client5.http.classic.methods.HttpGet(urlHandler); |
| 119 | + client.execute(request, response -> null); |
| 120 | + } |
| 121 | + client.close(); |
| 122 | + } catch (Exception e) { |
| 123 | + } |
| 124 | + return "ok"; |
| 125 | + } |
| 126 | + |
| 127 | + @PostMapping("/apache-httpasyncclient") |
| 128 | + public String apacheHttpAsyncClient( |
| 129 | + @RequestParam(value = "url", required = false) final String url, |
| 130 | + @RequestParam(value = "host", required = false) final String host, |
| 131 | + @RequestParam(value = "urlProducer", required = false) final String urlProducer) { |
| 132 | + final CloseableHttpAsyncClient client = HttpAsyncClients.createDefault(); |
| 133 | + client.start(); |
| 134 | + try { |
| 135 | + if (host != null) { |
| 136 | + final HttpHost httpHost = new HttpHost(host); |
| 137 | + client.execute(httpHost, new HttpGet("/"), null); |
| 138 | + } else if (url != null) { |
| 139 | + final HttpGet request = new HttpGet(url); |
| 140 | + client.execute(request, null); |
| 141 | + } else if (urlProducer != null) { |
| 142 | + final HttpAsyncRequestProducer producer = HttpAsyncMethods.create(new HttpGet(urlProducer)); |
| 143 | + client.execute(producer, null, null); |
| 144 | + } |
| 145 | + } catch (Exception e) { |
| 146 | + } finally { |
| 147 | + try { |
| 148 | + client.close(); |
| 149 | + } catch (Exception e) { |
| 150 | + } |
| 151 | + } |
| 152 | + return "ok"; |
| 153 | + } |
92 | 154 | }
|
0 commit comments