1 module HttpClientTest; 2 3 import hunt.http.client; 4 5 import hunt.logging.ConsoleLogger; 6 import hunt.Exceptions; 7 import hunt.util.DateTime; 8 import hunt.util.MimeType; 9 10 import std.stdio; 11 12 // string str = runGet("http://10.1.222.110/test.html"); 13 // enum string httpUrl = runGet("http://10.1.222.110:8080/index.html"); 14 // string str = runGet("http://127.0.0.1:8080/json"); 15 // string str = runGet("http://www.putao.com/"); 16 17 void main(string[] args) { 18 // testSimpleHttpClient(); 19 // testHttpClientWithCookie(); 20 // testHttpClientWithMultipart(); 21 // testWebSocketClient(); 22 // testHttpClientWithTLS(); 23 // testHttpClientWithMutualTLS(); 24 version (WITH_HUNT_TRACE) 25 testOpenTracing(); 26 } 27 28 void testSimpleHttpClient() { 29 30 HttpClientTest test = new HttpClientTest(); 31 try { 32 // test.testGet(); 33 // test.testGetHttps(); 34 // test.testAsynchronousGet(); 35 test.testPost(); 36 // test.testFormPost(); 37 38 } catch (Exception ex) { 39 warning(ex); 40 } 41 } 42 43 void testHttpClientWithCookie() { 44 string url = "http://127.0.0.1:8080/session/foo"; 45 HttpClient client = new HttpClient(); 46 scope (exit) { 47 client.close(); 48 } 49 50 client.useCookieStore(); 51 52 // post 53 FormBody formBody = new FormBody.Builder().add("age", 20).build(); 54 55 Response response = postForm(client, url, formBody); 56 57 if (response.haveBody()) { 58 warningf("response: %s", response.getBody().asString()); 59 } 60 61 // Cookie[] cookies = response.cookies(); 62 // foreach(c ; cookies) { 63 // trace(c.toString()); 64 // } 65 66 // get 67 RequestBuilder rb = new RequestBuilder().url(url); 68 // rb.cookies(cookies); 69 70 Request request = rb.build(); 71 response = client.newCall(request).execute(); 72 73 if (response !is null) { 74 tracef("status code: %d", response.getStatus()); 75 warning("response: ", response.getBody().asString()); 76 } 77 } 78 79 void testHttpClientWithMultipart() { 80 // Use the imgur image upload API as documented at https://api.imgur.com/endpoints/image 81 // string url = "http://127.0.0.1:8080/upload/file"; 82 string url = "http://10.1.222.110:8080/upload/file"; 83 HttpClient client = new HttpClient(); 84 scope (exit) { 85 client.close(); 86 } 87 // client.useCookieStore(); 88 89 // post 90 MultipartBody requestBody = new MultipartBody.Builder() // .setType(MultipartBody.FORM) 91 // .enableChunk() 92 .addFormDataPart("title", 93 "Putao Logo", MimeType.TEXT_PLAIN_VALUE) 94 .addFormDataPart("image", "favicon.ico", HttpBody.create("image/ico", "dub.json")) // HttpBody.createFromFile("image/ico", "dub.json")) 95 // HttpBody.createFromFile("image/ico", "resources/favicon.ico")) 96 .build(); 97 98 Response response = postForm(client, url, requestBody); 99 100 if (response.haveBody()) { 101 warningf("response: %s", response.getBody().asString()); 102 } 103 } 104 105 Response postForm(HttpClient client, string url, HttpBody content) { 106 Request request = new RequestBuilder().url(url) // .header("Authorization", "Basic cHV0YW86MjAxOQ==") 107 .authorization( 108 AuthenticationScheme.Basic, "cHV0YW86MjAxOQ==").post(content).build(); 109 110 Response response = client.newCall(request).execute(); 111 return response; 112 } 113 114 void testWebSocketClient() { 115 116 HttpClient client = new HttpClient(); 117 scope (exit) { 118 client.close(); 119 } 120 // 121 string url = "http://127.0.0.1:8080/ws1"; 122 Request request = new RequestBuilder().url(url) // .header("Authorization", "Basic cHV0YW86MjAxOQ==") 123 .authorization( 124 AuthenticationScheme.Basic, "cHV0YW86MjAxOQ==").build(); 125 126 // dfmt off 127 WebSocketConnection wsConn = client.newWebSocket(request, 128 new class AbstractWebSocketMessageHandler { 129 override void onOpen(WebSocketConnection connection) { 130 warning("Connection opened"); connection.sendText( 131 "Hello WebSocket. " ~ DateTime.getTimeAsGMT());} 132 133 override void onText(WebSocketConnection connection, string text) { 134 warningf("received (from %s): %s", connection.getRemoteAddress(), text); 135 } 136 } 137 ); 138 // dfmt on 139 // import core.thread; 140 // import core.time; 141 // Thread.sleep(5.seconds); 142 // wsConn.close(); 143 // client.close(); 144 } 145 146 void testHttpClientWithTLS() { 147 string url = "https://10.1.222.110:443/"; 148 // string url = "https://publicobject.com/helloworld.txt"; 149 // string url = "https://www.bing.com/"; 150 151 HttpClient client = new HttpClient(); 152 scope (exit) { 153 client.close(); 154 } 155 156 Request request = new RequestBuilder().url(url).build(); 157 Response response = client.newCall(request).execute(); 158 159 if (response !is null) { 160 tracef("status code: %d", response.getStatus()); 161 trace(response.getBody().asString()); 162 } 163 164 warning("done."); 165 } 166 167 void testHttpClientWithMutualTLS() { 168 // https://www.naschenweng.info/2018/02/01/java-mutual-ssl-authentication-2-way-ssl-authentication/ 169 // mutual TLS 170 string url = "https://10.1.222.110:443/"; 171 // string url = "https://publicobject.com/helloworld.txt"; 172 // string url = "https://www.bing.com/"; 173 174 HttpClient client = new HttpClient(); 175 scope (exit) { 176 client.close(); 177 } 178 179 Request request = new RequestBuilder().url(url).caCert("cert/ca.crt", "hunt2019") 180 .mutualTls("cert/client.crt", "cert/client.key", "hunt2019", "hunt2019").build(); 181 182 Response response = client.newCall(request).execute(); 183 184 if (response !is null) { 185 tracef("status code: %d", response.getStatus()); 186 trace(response.getBody().asString()); 187 } 188 189 warning("done."); 190 } 191 192 class HttpClientTest { 193 HttpClient client; 194 195 this() { 196 client = new HttpClient(); 197 } 198 199 // 200 void testGet() { 201 // string str = runGet("http://10.1.222.110/test.html"); 202 // string str = runGet("http://10.1.222.110:8080/index.html"); 203 string str = runGet("http://127.0.0.1:8080/json"); 204 // string str = runGet("http://www.putao.com/"); 205 trace(str); 206 207 trace("==============================="); 208 209 // str = runGet("http://10.1.222.110/index.html"); 210 // str = runGet("http://www.putao.com/"); 211 // trace(str); 212 } 213 214 // 215 void testGetHttps() { 216 217 string url = "https://10.1.222.110:440/"; 218 // string url = "https://publicobject.com/helloworld.txt"; 219 string str = runGet(url); 220 221 trace(str); 222 } 223 224 string runGet(string url) { 225 226 Request request = new RequestBuilder().url(url).build(); 227 Response response = client.newCall(request).execute(); 228 229 if (response !is null) { 230 tracef("status code: %d", response.getStatus()); 231 return response.getBody().asString(); 232 } 233 234 return ""; 235 } 236 237 // 238 void testAsynchronousGet() { 239 240 // string url = "https://10.1.222.110:6677/index"; 241 string url = "https://publicobject.com/helloworld.txt"; 242 Request request = new RequestBuilder().url(url).build(); 243 244 // dfmt off 245 client.newCall(request).enqueue(new class Callback { 246 void onFailure(Call call, IOException e) { 247 warning(e.toString());} 248 249 void onResponse(Call call, Response response) { 250 HttpBody responseBody = response.getBody(); if (!response.isSuccessful()) 251 throw new IOException("Unexpected code " ~ response.toString()); 252 253 HttpFields responseHeaders = response.headers(); foreach ( 254 HttpField header; responseHeaders) { 255 trace(header.getName() ~ ": " ~ header.getValue()); 256 } 257 258 trace(responseBody.asString());} 259 } 260 ); 261 // dfmt on 262 263 info("A request has been sent."); 264 } 265 266 void testPost() { 267 UrlEncoded encoder = new UrlEncoded; 268 encoder.put("email", "test@putao.com"); 269 encoder.put("password", "test"); 270 // string content = "email=test%40putao.com&password=test"; 271 string content = encoder.encode(); 272 string response = post("http://10.1.222.110:8080/testpost", 273 "application/x-www-form-urlencoded", content); 274 275 // string content = `{"type":"news", "offset": "0", "count": "20"}`; 276 // string url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=24_1IgJevIE2nBKEylZUX-eV1AEsPoFOu8Q_5_slFPPbw-Zh4wozxl6vS0DfBgbXDWD8nFu0j6_WVUAS5HvxjBLZBNAg4wzr7dplhfI7O0E9nHQtOdDbcTwZ2UzlPjEUhgEiLSiZ-0qiyPu0DOCXBIfAIAPTA"; 277 278 // string response = post(url, MimeType.APPLICATION_JSON_UTF_8.toString(), content); 279 280 trace(response); 281 } 282 283 string post(string url, string contentType, string content) { 284 HttpBody b = HttpBody.create(contentType, content); 285 286 Request request = new RequestBuilder().url(url).post(b).build(); 287 288 Response response = client.newCall(request).execute(); 289 HttpBody res = response.getBody(); 290 if (res is null) 291 return ""; 292 else 293 return res.asString(); 294 } 295 296 // 297 void testFormPost() { 298 FormBody form = new FormBody.Builder().add("sim", "ple").add("hey", 299 "there").add("help", "me").build(); 300 301 string response = postForm("http://10.1.11.164:8080/testpost", form); 302 // string response = postForm("http://10.1.222.110:8080/testpost", form); 303 trace(response); 304 } 305 306 string postForm(string url, HttpBody content) { 307 Request request = new RequestBuilder().url(url).header("Authorization", 308 "Basic cHV0YW86MjAxOQ==").post(content).build(); 309 310 Response response = client.newCall(request).execute(); 311 if (response.haveBody()) 312 return response.getBody().asString(); 313 return ""; 314 } 315 316 } 317 318 version (WITH_HUNT_TRACE) { 319 void testOpenTracing() { 320 import hunt.trace.HttpSender; 321 322 // string endpoint = "http://10.1.11.34:9411/api/v2/spans"; 323 string endpoint = "http://10.1.222.110:9411/api/v2/spans"; 324 httpSender().endpoint(endpoint); 325 326 // string url = "http://10.1.222.110/index.html"; 327 string url = "http://127.0.0.1:8080/plaintext"; 328 HttpClient client = new HttpClient(); 329 330 Request request = new RequestBuilder().url(url).localServiceName("HttpClientDemo").build(); 331 Response response = client.newCall(request).execute(); 332 333 if (response !is null) { 334 warningf("status code: %d", response.getStatus()); 335 // if(response.haveBody()) 336 // trace(response.getBody().asString()); 337 } else { 338 warning("no response"); 339 } 340 341 getchar(); 342 } 343 }